|
1 /* |
|
2 * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any questions. |
|
22 */ |
|
23 |
|
24 /* |
|
25 * @test |
|
26 * @bug 6346249 6392177 |
|
27 * @summary new Trees API |
|
28 */ |
|
29 |
|
30 import com.sun.source.tree.*; |
|
31 import com.sun.source.util.*; |
|
32 import java.io.*; |
|
33 import java.lang.annotation.*; |
|
34 import java.util.*; |
|
35 import javax.annotation.processing.*; |
|
36 import javax.lang.model.SourceVersion; |
|
37 import javax.lang.model.element.*; |
|
38 import javax.lang.model.type.*; |
|
39 import javax.tools.*; |
|
40 import com.sun.tools.javac.api.JavacTool; |
|
41 import com.sun.tools.javac.tree.JCTree; |
|
42 import com.sun.tools.javac.tree.TreeInfo; |
|
43 |
|
44 @Anno |
|
45 @SupportedAnnotationTypes("*") |
|
46 public class TestTrees extends AbstractProcessor { |
|
47 |
|
48 @Anno |
|
49 void annoMethod() { } |
|
50 |
|
51 @Anno |
|
52 int annoField; |
|
53 |
|
54 |
|
55 static final String testSrcDir = System.getProperty("test.src"); |
|
56 static final String testClassDir = System.getProperty("test.classes"); |
|
57 static final String self = TestTrees.class.getName(); |
|
58 static PrintWriter out = new PrintWriter(System.err, true); |
|
59 |
|
60 public static void main(String[] args) throws IOException { |
|
61 new TestTrees().run(); |
|
62 } |
|
63 |
|
64 void run() throws IOException { |
|
65 |
|
66 JavacTool tool = JavacTool.create(); |
|
67 |
|
68 DiagnosticListener<JavaFileObject> dl = new DiagnosticListener<JavaFileObject>() { |
|
69 public void report(Diagnostic d) { |
|
70 error(d.toString()); |
|
71 } |
|
72 }; |
|
73 |
|
74 StandardJavaFileManager fm = tool.getStandardFileManager(dl, null, null); |
|
75 Iterable<? extends JavaFileObject> files = |
|
76 fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrcDir, self + ".java"))); |
|
77 |
|
78 Iterable<String> opts = Arrays.asList("-d", "."); |
|
79 |
|
80 System.err.println("simple compilation, no processing"); |
|
81 JavacTask task = tool.getTask(out, fm, dl, opts, null, files); |
|
82 task.setTaskListener(new MyTaskListener(task)); |
|
83 if (!task.call()) |
|
84 throw new AssertionError("compilation failed"); |
|
85 |
|
86 opts = Arrays.asList("-d", ".", "-processorpath", testClassDir, "-processor", self); |
|
87 |
|
88 System.err.println(); |
|
89 System.err.println("compilation with processing"); |
|
90 task = tool.getTask(out, fm, dl,opts, null, files); |
|
91 if (!task.call()) |
|
92 throw new AssertionError("compilation failed"); |
|
93 |
|
94 if (errors > 0) |
|
95 throw new AssertionError(errors + " errors occurred"); |
|
96 } |
|
97 |
|
98 void testElement(Trees trees, Element e) { |
|
99 trees.getClass(); |
|
100 e.getClass(); |
|
101 |
|
102 System.err.println("testElement: " + e); |
|
103 Tree tree = trees.getTree(e); |
|
104 //System.err.println(tree); |
|
105 |
|
106 if (TreeInfo.symbolFor((JCTree)tree) != e) |
|
107 error("bad result from getTree"); |
|
108 |
|
109 TreePath path = trees.getPath(e); |
|
110 if (path == null) { |
|
111 error("getPath returned null"); |
|
112 return; |
|
113 } |
|
114 if (path.getLeaf() != tree) |
|
115 error("bad result from getPath"); |
|
116 |
|
117 Element e2 = trees.getElement(path); |
|
118 if (e2 == null) { |
|
119 error("getElement returned null"); |
|
120 return; |
|
121 } |
|
122 if (e2 != e) |
|
123 error("bad result from getElement"); |
|
124 |
|
125 // The TypeMirror is not available yet when annotation processing; |
|
126 // it is set up later during ANALYSE. |
|
127 TypeMirror t = trees.getTypeMirror(path); |
|
128 if (t != null && t.getKind() == TypeKind.DECLARED && |
|
129 ((DeclaredType)t).asElement() != e2) |
|
130 error("bad result from getTypeMirror"); |
|
131 |
|
132 for (AnnotationMirror m: e.getAnnotationMirrors()) { |
|
133 testAnnotation(trees, e, m); |
|
134 } |
|
135 } |
|
136 |
|
137 void testAnnotation(Trees trees, Element e, AnnotationMirror a) { |
|
138 System.err.println("testAnnotation: " + e + " " + a); |
|
139 Tree tree = trees.getTree(e, a); |
|
140 |
|
141 if (tree.getKind() != Tree.Kind.ANNOTATION) |
|
142 error("bad result from getTree"); |
|
143 |
|
144 TreePath path = trees.getPath(e, a); |
|
145 if (path.getLeaf() != tree) |
|
146 error("bad result from getPath"); |
|
147 } |
|
148 |
|
149 void error(String msg) { |
|
150 if (messager != null) |
|
151 // annotation processing will happen in a separate instance/classloader |
|
152 // so pass the message back to the calling instance. |
|
153 messager.printMessage(Diagnostic.Kind.ERROR, msg); |
|
154 else { |
|
155 System.err.println(msg); |
|
156 errors++; |
|
157 } |
|
158 |
|
159 } |
|
160 |
|
161 Messager messager; |
|
162 int errors; |
|
163 |
|
164 |
|
165 public boolean process(Set<? extends TypeElement> annos, RoundEnvironment rEnv) { |
|
166 Trees trees = Trees.instance(processingEnv); |
|
167 messager = processingEnv.getMessager(); |
|
168 |
|
169 for (Element e: rEnv.getRootElements()) { |
|
170 testElement(trees, e); |
|
171 } |
|
172 |
|
173 for (TypeElement anno: annos) { |
|
174 Set<? extends Element> elts = rEnv.getElementsAnnotatedWith(anno); |
|
175 System.err.println("anno: " + anno); |
|
176 System.err.println("elts: " + elts); |
|
177 if (elts != null) { // 6397298, should return empty set |
|
178 for (Element e: rEnv.getElementsAnnotatedWith(anno)) |
|
179 testElement(trees, e); |
|
180 } |
|
181 } |
|
182 |
|
183 return true; |
|
184 } |
|
185 |
|
186 @Override |
|
187 public SourceVersion getSupportedSourceVersion() { |
|
188 return SourceVersion.latest(); |
|
189 } |
|
190 |
|
191 class MyTaskListener implements TaskListener { |
|
192 MyTaskListener(JavacTask task) { |
|
193 this.task = task; |
|
194 } |
|
195 |
|
196 public void started(TaskEvent e) { |
|
197 System.err.println("started " + e); |
|
198 } |
|
199 |
|
200 public void finished(TaskEvent e) { |
|
201 //System.err.println("finished " + e); |
|
202 switch (e.getKind()) { |
|
203 case ANALYZE: |
|
204 testElement(Trees.instance(task), e.getTypeElement()); |
|
205 break; |
|
206 } |
|
207 } |
|
208 |
|
209 private final JavacTask task; |
|
210 } |
|
211 |
|
212 } |
|
213 |
|
214 @Retention(RetentionPolicy.SOURCE) |
|
215 @interface Anno { |
|
216 } |