author | jlahoda |
Fri, 21 Feb 2014 10:35:19 +0100 | |
changeset 23122 | 02c931d49ad2 |
parent 5520 | 86e4b9a9da40 |
child 27319 | 030080f03e4f |
permissions | -rw-r--r-- |
10 | 1 |
/* |
23122
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
2 |
* Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved. |
10 | 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 |
* |
|
5520 | 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. |
|
10 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
23122
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
26 |
* @bug 6346249 6392177 6411385 |
10 | 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 |
||
23122
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
54 |
@Anno |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
55 |
public TestTrees() { |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
56 |
} |
10 | 57 |
|
58 |
static final String testSrcDir = System.getProperty("test.src"); |
|
59 |
static final String testClassDir = System.getProperty("test.classes"); |
|
60 |
static final String self = TestTrees.class.getName(); |
|
61 |
static PrintWriter out = new PrintWriter(System.err, true); |
|
62 |
||
63 |
public static void main(String[] args) throws IOException { |
|
64 |
new TestTrees().run(); |
|
65 |
} |
|
66 |
||
67 |
void run() throws IOException { |
|
68 |
||
69 |
JavacTool tool = JavacTool.create(); |
|
70 |
||
71 |
DiagnosticListener<JavaFileObject> dl = new DiagnosticListener<JavaFileObject>() { |
|
72 |
public void report(Diagnostic d) { |
|
73 |
error(d.toString()); |
|
74 |
} |
|
75 |
}; |
|
76 |
||
77 |
StandardJavaFileManager fm = tool.getStandardFileManager(dl, null, null); |
|
78 |
Iterable<? extends JavaFileObject> files = |
|
79 |
fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrcDir, self + ".java"))); |
|
80 |
||
23122
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
81 |
Iterable<String> opts = Arrays.asList("-d", ".", "-XDcompilePolicy=simple"); |
10 | 82 |
|
83 |
System.err.println("simple compilation, no processing"); |
|
84 |
JavacTask task = tool.getTask(out, fm, dl, opts, null, files); |
|
85 |
task.setTaskListener(new MyTaskListener(task)); |
|
86 |
if (!task.call()) |
|
87 |
throw new AssertionError("compilation failed"); |
|
88 |
||
23122
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
89 |
opts = Arrays.asList("-d", ".", "-processorpath", testClassDir, "-processor", self, |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
90 |
"-XDcompilePolicy=simple"); |
10 | 91 |
|
92 |
System.err.println(); |
|
93 |
System.err.println("compilation with processing"); |
|
94 |
task = tool.getTask(out, fm, dl,opts, null, files); |
|
95 |
if (!task.call()) |
|
96 |
throw new AssertionError("compilation failed"); |
|
97 |
||
98 |
if (errors > 0) |
|
99 |
throw new AssertionError(errors + " errors occurred"); |
|
100 |
} |
|
101 |
||
102 |
void testElement(Trees trees, Element e) { |
|
103 |
trees.getClass(); |
|
104 |
e.getClass(); |
|
105 |
||
106 |
System.err.println("testElement: " + e); |
|
107 |
Tree tree = trees.getTree(e); |
|
108 |
//System.err.println(tree); |
|
109 |
||
110 |
if (TreeInfo.symbolFor((JCTree)tree) != e) |
|
111 |
error("bad result from getTree"); |
|
112 |
||
113 |
TreePath path = trees.getPath(e); |
|
114 |
if (path == null) { |
|
115 |
error("getPath returned null"); |
|
116 |
return; |
|
117 |
} |
|
118 |
if (path.getLeaf() != tree) |
|
119 |
error("bad result from getPath"); |
|
120 |
||
121 |
Element e2 = trees.getElement(path); |
|
122 |
if (e2 == null) { |
|
123 |
error("getElement returned null"); |
|
124 |
return; |
|
125 |
} |
|
126 |
if (e2 != e) |
|
127 |
error("bad result from getElement"); |
|
128 |
||
129 |
// The TypeMirror is not available yet when annotation processing; |
|
130 |
// it is set up later during ANALYSE. |
|
131 |
TypeMirror t = trees.getTypeMirror(path); |
|
132 |
if (t != null && t.getKind() == TypeKind.DECLARED && |
|
133 |
((DeclaredType)t).asElement() != e2) |
|
134 |
error("bad result from getTypeMirror"); |
|
135 |
||
136 |
for (AnnotationMirror m: e.getAnnotationMirrors()) { |
|
137 |
testAnnotation(trees, e, m); |
|
138 |
} |
|
139 |
} |
|
140 |
||
141 |
void testAnnotation(Trees trees, Element e, AnnotationMirror a) { |
|
142 |
System.err.println("testAnnotation: " + e + " " + a); |
|
143 |
Tree tree = trees.getTree(e, a); |
|
144 |
||
23122
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
145 |
if (tree.getKind() != Tree.Kind.ANNOTATION && tree.getKind() != Tree.Kind.TYPE_ANNOTATION) |
10 | 146 |
error("bad result from getTree"); |
147 |
||
148 |
TreePath path = trees.getPath(e, a); |
|
149 |
if (path.getLeaf() != tree) |
|
150 |
error("bad result from getPath"); |
|
151 |
} |
|
152 |
||
23122
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
153 |
void testAllDeclarations(Trees trees, CompilationUnitTree cut) { |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
154 |
new TreePathScanner<Void, Void>() { |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
155 |
@Override public Void scan(Tree tree, Void p) { |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
156 |
if (tree == null) return null; |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
157 |
switch (tree.getKind()) { |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
158 |
case METHOD: case CLASS: case VARIABLE: case TYPE_PARAMETER: |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
159 |
TreePath path = new TreePath(getCurrentPath(), tree); |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
160 |
Element el = trees.getElement(path); |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
161 |
if (el == null) { |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
162 |
error("null element"); |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
163 |
} else { |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
164 |
TreePath inferred = trees.getPath(el); |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
165 |
if (inferred == null) { |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
166 |
error("null path"); |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
167 |
} else { |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
168 |
if (inferred.getLeaf() != path.getLeaf()) |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
169 |
error("bad result from getPath"); |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
170 |
} |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
171 |
if (trees.getTree(el) != path.getLeaf()) |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
172 |
error("bad result from getTree"); |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
173 |
for (AnnotationMirror m: el.getAnnotationMirrors()) { |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
174 |
testAnnotation(trees, el, m); |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
175 |
} |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
176 |
} |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
177 |
} |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
178 |
return super.scan(tree, p); |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
179 |
} |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
180 |
}.scan(cut, null); |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
181 |
} |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
182 |
|
10 | 183 |
void error(String msg) { |
184 |
if (messager != null) |
|
185 |
// annotation processing will happen in a separate instance/classloader |
|
186 |
// so pass the message back to the calling instance. |
|
187 |
messager.printMessage(Diagnostic.Kind.ERROR, msg); |
|
188 |
else { |
|
189 |
System.err.println(msg); |
|
190 |
errors++; |
|
191 |
} |
|
192 |
||
193 |
} |
|
194 |
||
195 |
Messager messager; |
|
196 |
int errors; |
|
197 |
||
198 |
||
199 |
public boolean process(Set<? extends TypeElement> annos, RoundEnvironment rEnv) { |
|
200 |
Trees trees = Trees.instance(processingEnv); |
|
201 |
messager = processingEnv.getMessager(); |
|
202 |
||
203 |
for (Element e: rEnv.getRootElements()) { |
|
204 |
testElement(trees, e); |
|
205 |
} |
|
206 |
||
207 |
for (TypeElement anno: annos) { |
|
208 |
Set<? extends Element> elts = rEnv.getElementsAnnotatedWith(anno); |
|
209 |
System.err.println("anno: " + anno); |
|
210 |
System.err.println("elts: " + elts); |
|
211 |
if (elts != null) { // 6397298, should return empty set |
|
212 |
for (Element e: rEnv.getElementsAnnotatedWith(anno)) |
|
213 |
testElement(trees, e); |
|
214 |
} |
|
215 |
} |
|
216 |
||
217 |
return true; |
|
218 |
} |
|
219 |
||
220 |
@Override |
|
221 |
public SourceVersion getSupportedSourceVersion() { |
|
222 |
return SourceVersion.latest(); |
|
223 |
} |
|
224 |
||
225 |
class MyTaskListener implements TaskListener { |
|
226 |
MyTaskListener(JavacTask task) { |
|
227 |
this.task = task; |
|
228 |
} |
|
229 |
||
230 |
public void started(TaskEvent e) { |
|
231 |
System.err.println("started " + e); |
|
232 |
} |
|
233 |
||
234 |
public void finished(TaskEvent e) { |
|
235 |
//System.err.println("finished " + e); |
|
236 |
switch (e.getKind()) { |
|
237 |
case ANALYZE: |
|
238 |
testElement(Trees.instance(task), e.getTypeElement()); |
|
23122
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
239 |
testAllDeclarations(Trees.instance(task), e.getCompilationUnit()); |
10 | 240 |
break; |
241 |
} |
|
242 |
} |
|
243 |
||
244 |
private final JavacTask task; |
|
245 |
} |
|
246 |
||
23122
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
247 |
public static class TestTypeParams<@Anno T extends CharSequence> { |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
248 |
public <@Anno T extends Object> TestTypeParams(T param) { } |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
249 |
public <@Anno T extends Number> void m(T param) { |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
250 |
int local; |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
251 |
try { |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
252 |
new String(); |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
253 |
} catch (Exception exc) { } |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
254 |
} |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
255 |
} |
10 | 256 |
} |
257 |
||
258 |
@Retention(RetentionPolicy.SOURCE) |
|
23122
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
259 |
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.TYPE_PARAMETER, |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
260 |
ElementType.FIELD, ElementType.LOCAL_VARIABLE}) |
10 | 261 |
@interface Anno { |
262 |
} |