487N/A/*
553N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
487N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
487N/A *
487N/A * This code is free software; you can redistribute it and/or modify it
487N/A * under the terms of the GNU General Public License version 2 only, as
487N/A * published by the Free Software Foundation.
487N/A *
487N/A * This code is distributed in the hope that it will be useful, but WITHOUT
487N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
487N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
487N/A * version 2 for more details (a copy is included in the LICENSE file that
487N/A * accompanied this code).
487N/A *
487N/A * You should have received a copy of the GNU General Public License version
487N/A * 2 along with this work; if not, write to the Free Software Foundation,
487N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
487N/A *
553N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
553N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
487N/A */
487N/A
487N/Aimport java.io.*;
487N/Aimport java.util.*;
487N/Aimport javax.annotation.processing.*;
487N/Aimport javax.lang.model.*;
487N/Aimport javax.lang.model.element.*;
487N/Aimport javax.lang.model.util.*;
487N/Aimport javax.tools.*;
487N/A
487N/Aimport com.sun.source.util.*;
487N/Aimport com.sun.tools.javac.code.BoundKind;
487N/Aimport com.sun.tools.javac.tree.JCTree.*;
487N/Aimport com.sun.tools.javac.tree.TreeScanner;
487N/Aimport com.sun.tools.javac.tree.*;
487N/Aimport com.sun.tools.javac.util.List;
487N/A
487N/A/**
487N/A * Test processor used to check test programs using the @Test, @DA, and @TA
487N/A * annotations.
487N/A *
487N/A * The processor looks for elements annotated with @Test, and analyzes the
487N/A * syntax trees for those elements. Within such trees, the processor looks
487N/A * for the DA annotations on decls and TA annotations on types.
487N/A * The value of these annotations should be a simple string rendition of
487N/A * the tree node to which it is attached.
487N/A * The expected number of annotations is given by the parameter to the
487N/A * @Test annotation itself.
487N/A */
487N/A@SupportedAnnotationTypes({"Test"})
487N/Apublic class TestProcessor extends AbstractProcessor {
487N/A public SourceVersion getSupportedSourceVersion() {
487N/A return SourceVersion.latest();
487N/A }
487N/A
487N/A /** Process trees for elements annotated with the @Test(n) annotation. */
487N/A public boolean process(Set<? extends TypeElement> annos, RoundEnvironment renv) {
487N/A if (renv.processingOver())
487N/A return true;
487N/A
487N/A Elements elements = processingEnv.getElementUtils();
487N/A Trees trees = Trees.instance(processingEnv);
487N/A
487N/A TypeElement testAnno = elements.getTypeElement("Test");
487N/A for (Element elem: renv.getElementsAnnotatedWith(testAnno)) {
487N/A System.err.println("ELEM: " + elem);
487N/A int count = getValue(getAnnoMirror(elem, testAnno), Integer.class);
487N/A System.err.println("count: " + count);
487N/A TreePath p = trees.getPath(elem);
487N/A JavaFileObject file = p.getCompilationUnit().getSourceFile();
487N/A JCTree tree = (JCTree) p.getLeaf();
487N/A System.err.println("tree: " + tree);
487N/A new TestScanner(file).check(tree, count);
487N/A }
487N/A return true;
487N/A }
487N/A
487N/A /** Get the AnnotationMirror on an element for a given annotation. */
487N/A AnnotationMirror getAnnoMirror(Element e, TypeElement anno) {
487N/A Types types = processingEnv.getTypeUtils();
487N/A for (AnnotationMirror m: e.getAnnotationMirrors()) {
487N/A if (types.isSameType(m.getAnnotationType(), anno.asType()))
487N/A return m;
487N/A }
487N/A return null;
487N/A }
487N/A
487N/A /** Get the value of the value element of an annotation mirror. */
487N/A <T> T getValue(AnnotationMirror m, Class<T> type) {
487N/A for (Map.Entry<? extends ExecutableElement,? extends AnnotationValue> e: m.getElementValues().entrySet()) {
487N/A ExecutableElement ee = e.getKey();
487N/A if (ee.getSimpleName().contentEquals("value")) {
487N/A AnnotationValue av = e.getValue();
487N/A return type.cast(av.getValue());
487N/A }
487N/A }
487N/A return null;
487N/A }
487N/A
487N/A /** Report an error to the annotation processing system. */
487N/A void error(String msg) {
487N/A Messager messager = processingEnv.getMessager();
487N/A messager.printMessage(Diagnostic.Kind.ERROR, msg);
487N/A }
487N/A
487N/A /** Report an error to the annotation processing system. */
487N/A void error(JavaFileObject file, JCTree tree, String msg) {
487N/A // need better API for reporting tree position errors to the messager
487N/A Messager messager = processingEnv.getMessager();
487N/A String text = file.getName() + ":" + getLine(file, tree) + ": " + msg;
487N/A messager.printMessage(Diagnostic.Kind.ERROR, text);
487N/A }
487N/A
487N/A /** Get the line number for the primary position for a tree.
487N/A * The code is intended to be simple, although not necessarily efficient.
487N/A * However, note that a file manager such as JavacFileManager is likely
487N/A * to cache the results of file.getCharContent, avoiding the need to read
487N/A * the bits from disk each time this method is called.
487N/A */
487N/A int getLine(JavaFileObject file, JCTree tree) {
487N/A try {
487N/A CharSequence cs = file.getCharContent(true);
487N/A int line = 1;
487N/A for (int i = 0; i < tree.pos; i++) {
487N/A if (cs.charAt(i) == '\n') // jtreg tests always use Unix line endings
487N/A line++;
487N/A }
487N/A return line;
487N/A } catch (IOException e) {
487N/A return -1;
487N/A }
487N/A }
487N/A
487N/A /** Scan a tree, looking for @DA and @TA annotations, and verifying that such
487N/A * annotations are attached to the expected tree node matching the string
487N/A * parameter of the annotation.
487N/A */
487N/A class TestScanner extends TreeScanner {
487N/A /** Create a scanner for a given file. */
487N/A TestScanner(JavaFileObject file) {
487N/A this.file = file;
487N/A }
487N/A
487N/A /** Check the annotations in a given tree. */
487N/A void check(JCTree tree, int expectCount) {
487N/A foundCount = 0;
487N/A scan(tree);
487N/A if (foundCount != expectCount)
487N/A error(file, tree, "Wrong number of annotations found: " + foundCount + ", expected: " + expectCount);
487N/A }
487N/A
487N/A /** Check @DA annotations on a class declaration. */
487N/A @Override
487N/A public void visitClassDef(JCClassDecl tree) {
487N/A super.visitClassDef(tree);
487N/A check(tree.mods.annotations, "DA", tree);
487N/A }
487N/A
487N/A /** Check @DA annotations on a method declaration. */
487N/A @Override
487N/A public void visitMethodDef(JCMethodDecl tree) {
487N/A super.visitMethodDef(tree);
487N/A check(tree.mods.annotations, "DA", tree);
487N/A }
487N/A
487N/A /** Check @DA annotations on a field, parameter or local variable declaration. */
487N/A @Override
487N/A public void visitVarDef(JCVariableDecl tree) {
487N/A super.visitVarDef(tree);
487N/A check(tree.mods.annotations, "DA", tree);
487N/A }
487N/A
487N/A /** Check @TA annotations on a type. */
487N/A public void visitAnnotatedType(JCAnnotatedType tree) {
487N/A super.visitAnnotatedType(tree);
487N/A check(tree.annotations, "TA", tree);
487N/A }
487N/A
487N/A /** Check to see if a list of annotations contains a named annotation, and
487N/A * if so, verify the annotation is expected by comparing the value of the
487N/A * annotation's argument against the string rendition of the reference tree
487N/A * node.
487N/A * @param annos the list of annotations to be checked
487N/A * @param name the name of the annotation to be checked
487N/A * @param tree the tree against which to compare the annotations's argument
487N/A */
487N/A void check(List<? extends JCAnnotation> annos, String name, JCTree tree) {
487N/A for (List<? extends JCAnnotation> l = annos; l.nonEmpty(); l = l.tail) {
487N/A JCAnnotation anno = l.head;
487N/A if (anno.annotationType.toString().equals(name) && (anno.args.size() == 1)) {
487N/A String expect = getStringValue(anno.args.head);
487N/A foundCount++;
487N/A System.err.println("found: " + name + " " + expect);
487N/A String found = new TypePrinter().print(tree);
487N/A if (!found.equals(expect))
487N/A error(file, anno, "Unexpected result: expected: \"" + expect + "\", found: \"" + found + "\"");
487N/A }
487N/A }
487N/A }
487N/A
487N/A /** Get the string value of an annotation argument, which is given by the
487N/A * expression <i>name</i>=<i>value</i>.
487N/A */
487N/A String getStringValue(JCExpression e) {
487N/A if (e.getTag() == JCTree.ASSIGN) {
487N/A JCAssign a = (JCAssign) e;
487N/A JCExpression rhs = a.rhs;
487N/A if (rhs.getTag() == JCTree.LITERAL) {
487N/A JCLiteral l = (JCLiteral) rhs;
487N/A return (String) l.value;
487N/A }
487N/A }
487N/A throw new IllegalArgumentException(e.toString());
487N/A }
487N/A
487N/A /** The file for the tree. Used to locate errors. */
487N/A JavaFileObject file;
487N/A /** The number of annotations that have been found. @see #check */
487N/A int foundCount;
487N/A }
487N/A
487N/A /** Convert a type or decl tree to a reference string used by the @DA and @TA annotations. */
487N/A class TypePrinter extends Visitor {
487N/A /** Convert a type or decl tree to a string. */
487N/A String print(JCTree tree) {
487N/A if (tree == null)
487N/A return null;
487N/A tree.accept(this);
487N/A return result;
487N/A }
487N/A
487N/A String print(List<? extends JCTree> list) {
487N/A return print(list, ", ");
487N/A }
487N/A
487N/A String print(List<? extends JCTree> list, String sep) {
487N/A StringBuilder sb = new StringBuilder();
487N/A if (list.nonEmpty()) {
487N/A sb.append(print(list.head));
487N/A for (List<? extends JCTree> l = list.tail; l.nonEmpty(); l = l.tail) {
487N/A sb.append(sep);
487N/A sb.append(print(l.head));
487N/A }
487N/A }
487N/A return sb.toString();
487N/A }
487N/A
487N/A @Override
487N/A public void visitClassDef(JCClassDecl tree) {
487N/A result = tree.name.toString();
487N/A }
487N/A
487N/A @Override
487N/A public void visitMethodDef(JCMethodDecl tree) {
487N/A result = tree.name.toString();
487N/A }
487N/A
487N/A @Override
487N/A public void visitVarDef(JCVariableDecl tree) {
487N/A tree.vartype.accept(this);
487N/A }
487N/A
487N/A @Override
487N/A public void visitAnnotatedType(JCAnnotatedType tree) {
487N/A tree.underlyingType.accept(this);
487N/A }
487N/A
487N/A @Override
487N/A public void visitTypeIdent(JCPrimitiveTypeTree tree) {
487N/A result = tree.toString();
487N/A }
487N/A
487N/A @Override
487N/A public void visitTypeArray(JCArrayTypeTree tree) {
487N/A result = print(tree.elemtype) + "[]";
487N/A }
487N/A
487N/A @Override
487N/A public void visitTypeApply(JCTypeApply tree) {
487N/A result = print(tree.clazz) + "<" + print(tree.arguments) + ">";
487N/A }
487N/A
487N/A @Override
487N/A public void visitTypeParameter(JCTypeParameter tree) {
487N/A if (tree.bounds.isEmpty())
487N/A result = tree.name.toString();
487N/A else
487N/A result = tree.name + " extends " + print(tree.bounds, "&");
487N/A }
487N/A
487N/A @Override
487N/A public void visitWildcard(JCWildcard tree) {
487N/A if (tree.kind.kind == BoundKind.UNBOUND)
487N/A result = tree.kind.toString();
487N/A else
487N/A result = tree.kind + " " + print(tree.inner);
487N/A }
487N/A
487N/A private String result;
487N/A }
487N/A}