test/tools/javac/treeannotests/TestProcessor.java

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 1521
71f35e4b93a5
parent 0
959103a6100f
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 import java.io.*;
aoqi@0 25 import java.util.*;
aoqi@0 26 import javax.annotation.processing.*;
aoqi@0 27 import javax.lang.model.*;
aoqi@0 28 import javax.lang.model.element.*;
aoqi@0 29 import javax.lang.model.util.*;
aoqi@0 30 import javax.tools.*;
aoqi@0 31
aoqi@0 32 import com.sun.source.util.*;
aoqi@0 33 import com.sun.tools.javac.code.BoundKind;
aoqi@0 34 import com.sun.tools.javac.tree.JCTree.*;
aoqi@0 35 import com.sun.tools.javac.tree.TreeScanner;
aoqi@0 36 import com.sun.tools.javac.tree.*;
aoqi@0 37 import com.sun.tools.javac.util.List;
aoqi@0 38
aoqi@0 39 /**
aoqi@0 40 * Test processor used to check test programs using the @Test, @DA, and @TA
aoqi@0 41 * annotations.
aoqi@0 42 *
aoqi@0 43 * The processor looks for elements annotated with @Test, and analyzes the
aoqi@0 44 * syntax trees for those elements. Within such trees, the processor looks
aoqi@0 45 * for the DA annotations on decls and TA annotations on types.
aoqi@0 46 * The value of these annotations should be a simple string rendition of
aoqi@0 47 * the tree node to which it is attached.
aoqi@0 48 * The expected number of annotations is given by the parameter to the
aoqi@0 49 * @Test annotation itself.
aoqi@0 50 */
aoqi@0 51 @SupportedAnnotationTypes({"Test"})
aoqi@0 52 public class TestProcessor extends AbstractProcessor {
aoqi@0 53 public SourceVersion getSupportedSourceVersion() {
aoqi@0 54 return SourceVersion.latest();
aoqi@0 55 }
aoqi@0 56
aoqi@0 57 /** Process trees for elements annotated with the @Test(n) annotation. */
aoqi@0 58 public boolean process(Set<? extends TypeElement> annos, RoundEnvironment renv) {
aoqi@0 59 if (renv.processingOver())
aoqi@0 60 return true;
aoqi@0 61
aoqi@0 62 Elements elements = processingEnv.getElementUtils();
aoqi@0 63 Trees trees = Trees.instance(processingEnv);
aoqi@0 64
aoqi@0 65 TypeElement testAnno = elements.getTypeElement("Test");
aoqi@0 66 for (Element elem: renv.getElementsAnnotatedWith(testAnno)) {
aoqi@0 67 System.err.println("ELEM: " + elem);
aoqi@0 68 int count = getValue(getAnnoMirror(elem, testAnno), Integer.class);
aoqi@0 69 System.err.println("count: " + count);
aoqi@0 70 TreePath p = trees.getPath(elem);
aoqi@0 71 JavaFileObject file = p.getCompilationUnit().getSourceFile();
aoqi@0 72 JCTree tree = (JCTree) p.getLeaf();
aoqi@0 73 System.err.println("tree: " + tree);
aoqi@0 74 new TestScanner(file).check(tree, count);
aoqi@0 75 }
aoqi@0 76 return true;
aoqi@0 77 }
aoqi@0 78
aoqi@0 79 /** Get the AnnotationMirror on an element for a given annotation. */
aoqi@0 80 AnnotationMirror getAnnoMirror(Element e, TypeElement anno) {
aoqi@0 81 Types types = processingEnv.getTypeUtils();
aoqi@0 82 for (AnnotationMirror m: e.getAnnotationMirrors()) {
aoqi@0 83 if (types.isSameType(m.getAnnotationType(), anno.asType()))
aoqi@0 84 return m;
aoqi@0 85 }
aoqi@0 86 return null;
aoqi@0 87 }
aoqi@0 88
aoqi@0 89 /** Get the value of the value element of an annotation mirror. */
aoqi@0 90 <T> T getValue(AnnotationMirror m, Class<T> type) {
aoqi@0 91 for (Map.Entry<? extends ExecutableElement,? extends AnnotationValue> e: m.getElementValues().entrySet()) {
aoqi@0 92 ExecutableElement ee = e.getKey();
aoqi@0 93 if (ee.getSimpleName().contentEquals("value")) {
aoqi@0 94 AnnotationValue av = e.getValue();
aoqi@0 95 return type.cast(av.getValue());
aoqi@0 96 }
aoqi@0 97 }
aoqi@0 98 return null;
aoqi@0 99 }
aoqi@0 100
aoqi@0 101 /** Report an error to the annotation processing system. */
aoqi@0 102 void error(String msg) {
aoqi@0 103 Messager messager = processingEnv.getMessager();
aoqi@0 104 messager.printMessage(Diagnostic.Kind.ERROR, msg);
aoqi@0 105 }
aoqi@0 106
aoqi@0 107 /** Report an error to the annotation processing system. */
aoqi@0 108 void error(JavaFileObject file, JCTree tree, String msg) {
aoqi@0 109 // need better API for reporting tree position errors to the messager
aoqi@0 110 Messager messager = processingEnv.getMessager();
aoqi@0 111 String text = file.getName() + ":" + getLine(file, tree) + ": " + msg;
aoqi@0 112 messager.printMessage(Diagnostic.Kind.ERROR, text);
aoqi@0 113 }
aoqi@0 114
aoqi@0 115 /** Get the line number for the primary position for a tree.
aoqi@0 116 * The code is intended to be simple, although not necessarily efficient.
aoqi@0 117 * However, note that a file manager such as JavacFileManager is likely
aoqi@0 118 * to cache the results of file.getCharContent, avoiding the need to read
aoqi@0 119 * the bits from disk each time this method is called.
aoqi@0 120 */
aoqi@0 121 int getLine(JavaFileObject file, JCTree tree) {
aoqi@0 122 try {
aoqi@0 123 CharSequence cs = file.getCharContent(true);
aoqi@0 124 int line = 1;
aoqi@0 125 for (int i = 0; i < tree.pos; i++) {
aoqi@0 126 if (cs.charAt(i) == '\n') // jtreg tests always use Unix line endings
aoqi@0 127 line++;
aoqi@0 128 }
aoqi@0 129 return line;
aoqi@0 130 } catch (IOException e) {
aoqi@0 131 return -1;
aoqi@0 132 }
aoqi@0 133 }
aoqi@0 134
aoqi@0 135 /** Scan a tree, looking for @DA and @TA annotations, and verifying that such
aoqi@0 136 * annotations are attached to the expected tree node matching the string
aoqi@0 137 * parameter of the annotation.
aoqi@0 138 */
aoqi@0 139 class TestScanner extends TreeScanner {
aoqi@0 140 /** Create a scanner for a given file. */
aoqi@0 141 TestScanner(JavaFileObject file) {
aoqi@0 142 this.file = file;
aoqi@0 143 }
aoqi@0 144
aoqi@0 145 /** Check the annotations in a given tree. */
aoqi@0 146 void check(JCTree tree, int expectCount) {
aoqi@0 147 foundCount = 0;
aoqi@0 148 scan(tree);
aoqi@0 149 if (foundCount != expectCount)
aoqi@0 150 error(file, tree, "Wrong number of annotations found: " + foundCount + ", expected: " + expectCount);
aoqi@0 151 }
aoqi@0 152
aoqi@0 153 /** Check @DA annotations on a class declaration. */
aoqi@0 154 @Override
aoqi@0 155 public void visitClassDef(JCClassDecl tree) {
aoqi@0 156 super.visitClassDef(tree);
aoqi@0 157 check(tree.mods.annotations, "DA", tree);
aoqi@0 158 }
aoqi@0 159
aoqi@0 160 /** Check @DA annotations on a method declaration. */
aoqi@0 161 @Override
aoqi@0 162 public void visitMethodDef(JCMethodDecl tree) {
aoqi@0 163 super.visitMethodDef(tree);
aoqi@0 164 check(tree.mods.annotations, "DA", tree);
aoqi@0 165 }
aoqi@0 166
aoqi@0 167 /** Check @DA annotations on a field, parameter or local variable declaration. */
aoqi@0 168 @Override
aoqi@0 169 public void visitVarDef(JCVariableDecl tree) {
aoqi@0 170 super.visitVarDef(tree);
aoqi@0 171 check(tree.mods.annotations, "DA", tree);
aoqi@0 172 }
aoqi@0 173
aoqi@0 174 /** Check @TA annotations on a type. */
aoqi@0 175 public void visitAnnotatedType(JCAnnotatedType tree) {
aoqi@0 176 super.visitAnnotatedType(tree);
aoqi@0 177 check(tree.annotations, "TA", tree);
aoqi@0 178 }
aoqi@0 179
aoqi@0 180 /** Check to see if a list of annotations contains a named annotation, and
aoqi@0 181 * if so, verify the annotation is expected by comparing the value of the
aoqi@0 182 * annotation's argument against the string rendition of the reference tree
aoqi@0 183 * node.
aoqi@0 184 * @param annos the list of annotations to be checked
aoqi@0 185 * @param name the name of the annotation to be checked
aoqi@0 186 * @param tree the tree against which to compare the annotations's argument
aoqi@0 187 */
aoqi@0 188 void check(List<? extends JCAnnotation> annos, String name, JCTree tree) {
aoqi@0 189 for (List<? extends JCAnnotation> l = annos; l.nonEmpty(); l = l.tail) {
aoqi@0 190 JCAnnotation anno = l.head;
aoqi@0 191 if (anno.annotationType.toString().equals(name) && (anno.args.size() == 1)) {
aoqi@0 192 String expect = getStringValue(anno.args.head);
aoqi@0 193 foundCount++;
aoqi@0 194 System.err.println("found: " + name + " " + expect);
aoqi@0 195 String found = new TypePrinter().print(tree);
aoqi@0 196 if (!found.equals(expect))
aoqi@0 197 error(file, anno, "Unexpected result: expected: \"" + expect + "\", found: \"" + found + "\"");
aoqi@0 198 }
aoqi@0 199 }
aoqi@0 200 }
aoqi@0 201
aoqi@0 202 /** Get the string value of an annotation argument, which is given by the
aoqi@0 203 * expression <i>name</i>=<i>value</i>.
aoqi@0 204 */
aoqi@0 205 String getStringValue(JCExpression e) {
aoqi@0 206 if (e.hasTag(JCTree.Tag.ASSIGN)) {
aoqi@0 207 JCAssign a = (JCAssign) e;
aoqi@0 208 JCExpression rhs = a.rhs;
aoqi@0 209 if (rhs.hasTag(JCTree.Tag.LITERAL)) {
aoqi@0 210 JCLiteral l = (JCLiteral) rhs;
aoqi@0 211 return (String) l.value;
aoqi@0 212 }
aoqi@0 213 } else if (e.hasTag(JCTree.Tag.LITERAL)) {
aoqi@0 214 JCLiteral l = (JCLiteral) e;
aoqi@0 215 return (String) l.value;
aoqi@0 216 }
aoqi@0 217 throw new IllegalArgumentException(e.toString());
aoqi@0 218 }
aoqi@0 219
aoqi@0 220 /** The file for the tree. Used to locate errors. */
aoqi@0 221 JavaFileObject file;
aoqi@0 222 /** The number of annotations that have been found. @see #check */
aoqi@0 223 int foundCount;
aoqi@0 224 }
aoqi@0 225
aoqi@0 226 /** Convert a type or decl tree to a reference string used by the @DA and @TA annotations. */
aoqi@0 227 class TypePrinter extends Visitor {
aoqi@0 228 /** Convert a type or decl tree to a string. */
aoqi@0 229 String print(JCTree tree) {
aoqi@0 230 if (tree == null)
aoqi@0 231 return null;
aoqi@0 232 tree.accept(this);
aoqi@0 233 return result;
aoqi@0 234 }
aoqi@0 235
aoqi@0 236 String print(List<? extends JCTree> list) {
aoqi@0 237 return print(list, ", ");
aoqi@0 238 }
aoqi@0 239
aoqi@0 240 String print(List<? extends JCTree> list, String sep) {
aoqi@0 241 StringBuilder sb = new StringBuilder();
aoqi@0 242 if (list.nonEmpty()) {
aoqi@0 243 sb.append(print(list.head));
aoqi@0 244 for (List<? extends JCTree> l = list.tail; l.nonEmpty(); l = l.tail) {
aoqi@0 245 sb.append(sep);
aoqi@0 246 sb.append(print(l.head));
aoqi@0 247 }
aoqi@0 248 }
aoqi@0 249 return sb.toString();
aoqi@0 250 }
aoqi@0 251
aoqi@0 252 @Override
aoqi@0 253 public void visitClassDef(JCClassDecl tree) {
aoqi@0 254 result = tree.name.toString();
aoqi@0 255 }
aoqi@0 256
aoqi@0 257 @Override
aoqi@0 258 public void visitMethodDef(JCMethodDecl tree) {
aoqi@0 259 result = tree.name.toString();
aoqi@0 260 }
aoqi@0 261
aoqi@0 262 @Override
aoqi@0 263 public void visitVarDef(JCVariableDecl tree) {
aoqi@0 264 tree.vartype.accept(this);
aoqi@0 265 }
aoqi@0 266
aoqi@0 267 @Override
aoqi@0 268 public void visitAnnotatedType(JCAnnotatedType tree) {
aoqi@0 269 tree.underlyingType.accept(this);
aoqi@0 270 }
aoqi@0 271
aoqi@0 272 @Override
aoqi@0 273 public void visitTypeIdent(JCPrimitiveTypeTree tree) {
aoqi@0 274 result = tree.toString();
aoqi@0 275 }
aoqi@0 276
aoqi@0 277 @Override
aoqi@0 278 public void visitTypeArray(JCArrayTypeTree tree) {
aoqi@0 279 result = print(tree.elemtype) + "[]";
aoqi@0 280 }
aoqi@0 281
aoqi@0 282 @Override
aoqi@0 283 public void visitTypeApply(JCTypeApply tree) {
aoqi@0 284 result = print(tree.clazz) + "<" + print(tree.arguments) + ">";
aoqi@0 285 }
aoqi@0 286
aoqi@0 287 @Override
aoqi@0 288 public void visitTypeParameter(JCTypeParameter tree) {
aoqi@0 289 if (tree.bounds.isEmpty())
aoqi@0 290 result = tree.name.toString();
aoqi@0 291 else
aoqi@0 292 result = tree.name + " extends " + print(tree.bounds, "&");
aoqi@0 293 }
aoqi@0 294
aoqi@0 295 @Override
aoqi@0 296 public void visitWildcard(JCWildcard tree) {
aoqi@0 297 if (tree.kind.kind == BoundKind.UNBOUND)
aoqi@0 298 result = tree.kind.toString();
aoqi@0 299 else
aoqi@0 300 result = tree.kind + " " + print(tree.inner);
aoqi@0 301 }
aoqi@0 302
aoqi@0 303 private String result;
aoqi@0 304 }
aoqi@0 305 }

mercurial