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