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