19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 import java.io.*;
25 import javax.annotation.processing.*;
26 import javax.lang.model.*;
27 import javax.lang.model.element.*;
28 import javax.lang.model.type.*;
29 import javax.lang.model.util.*;
30 import java.util.*;
31 import com.sun.source.tree.*;
32 import com.sun.source.util.*;
33 import static javax.tools.Diagnostic.Kind.*;
34
35 /**
36 * Using the tree API, retrieve element representations of anonymous
37 * classes and verify their names are as specified.
38 */
39 @SupportedAnnotationTypes("*")
40 public class TestAnonSourceNames extends AbstractProcessor {
41
42 public boolean process(Set<? extends TypeElement> annotations,
43 RoundEnvironment roundEnv) {
44 if (!roundEnv.processingOver()) {
45 Trees trees = Trees.instance(processingEnv);
46
47 for(Element rootElement : roundEnv.getRootElements()) {
48 TreePath treePath = trees.getPath(rootElement);
49
50 (new ClassTreeScanner(trees)).
51 scan(trees.getTree(rootElement),
52 treePath.getCompilationUnit());
53 }
54 }
55 return true;
56 }
57
58 class ClassTreeScanner extends TreeScanner<Void, CompilationUnitTree> {
59 private Trees trees;
60
67 Element element = trees.getElement(trees.getPath(cu, node));
68 if (element == null) {
69 processingEnv.getMessager().printMessage(ERROR,
70 "No element retrieved for node named ''" +
71 node.getSimpleName() + "''.");
72 } else {
73
74 System.out.println("\nVisiting class ``" + element.getSimpleName() +
75 "'' of kind " + element.getKind());
76 if (element instanceof TypeElement) {
77 TypeElement typeElement = (TypeElement) element;
78 String s = typeElement.getQualifiedName().toString();
79 System.out.println("\tqualified name:" + s);
80 } else {
81 throw new RuntimeException("TypeElement not gotten from ClassTree.");
82 }
83 }
84 return super.visitClass(node, cu);
85 }
86 }
87
88 @Override
89 public SourceVersion getSupportedSourceVersion() {
90 return SourceVersion.latest();
91 }
92 }
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 import java.io.*;
25 import javax.annotation.processing.*;
26 import javax.lang.model.*;
27 import javax.lang.model.element.*;
28 import javax.lang.model.type.*;
29 import javax.lang.model.util.*;
30 import java.util.*;
31 import com.sun.source.tree.*;
32 import com.sun.source.util.*;
33 import static javax.tools.Diagnostic.Kind.*;
34
35 /**
36 * Using the tree API, retrieve element representations of anonymous
37 * classes and verify their names are as specified.
38 */
39 public class TestAnonSourceNames extends JavacTestingAbstractProcessor {
40
41 public boolean process(Set<? extends TypeElement> annotations,
42 RoundEnvironment roundEnv) {
43 if (!roundEnv.processingOver()) {
44 Trees trees = Trees.instance(processingEnv);
45
46 for(Element rootElement : roundEnv.getRootElements()) {
47 TreePath treePath = trees.getPath(rootElement);
48
49 (new ClassTreeScanner(trees)).
50 scan(trees.getTree(rootElement),
51 treePath.getCompilationUnit());
52 }
53 }
54 return true;
55 }
56
57 class ClassTreeScanner extends TreeScanner<Void, CompilationUnitTree> {
58 private Trees trees;
59
66 Element element = trees.getElement(trees.getPath(cu, node));
67 if (element == null) {
68 processingEnv.getMessager().printMessage(ERROR,
69 "No element retrieved for node named ''" +
70 node.getSimpleName() + "''.");
71 } else {
72
73 System.out.println("\nVisiting class ``" + element.getSimpleName() +
74 "'' of kind " + element.getKind());
75 if (element instanceof TypeElement) {
76 TypeElement typeElement = (TypeElement) element;
77 String s = typeElement.getQualifiedName().toString();
78 System.out.println("\tqualified name:" + s);
79 } else {
80 throw new RuntimeException("TypeElement not gotten from ClassTree.");
81 }
82 }
83 return super.visitClass(node, cu);
84 }
85 }
86 }
|