author | jjg |
Wed, 10 Aug 2016 15:47:46 -0700 | |
changeset 40308 | 274367a99f98 |
parent 36526 | 3b41f1c69604 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
36526 | 2 |
* Copyright (c) 2006, 2016, 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 |
36526 | 28 |
* @modules jdk.compiler/com.sun.tools.javac.tree |
10 | 29 |
*/ |
30 |
||
31 |
import java.io.*; |
|
32 |
import java.lang.annotation.*; |
|
33 |
import java.util.*; |
|
34 |
import javax.annotation.processing.*; |
|
35 |
import javax.lang.model.SourceVersion; |
|
36 |
import javax.lang.model.element.*; |
|
37 |
import javax.lang.model.type.*; |
|
38 |
import javax.tools.*; |
|
36526 | 39 |
|
40 |
import com.sun.source.tree.*; |
|
41 |
import com.sun.source.util.*; |
|
10 | 42 |
import com.sun.tools.javac.tree.JCTree; |
43 |
import com.sun.tools.javac.tree.TreeInfo; |
|
44 |
||
45 |
@Anno |
|
46 |
@SupportedAnnotationTypes("*") |
|
47 |
public class TestTrees extends AbstractProcessor { |
|
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 |
||
36526 | 69 |
JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); |
10 | 70 |
|
71 |
DiagnosticListener<JavaFileObject> dl = new DiagnosticListener<JavaFileObject>() { |
|
72 |
public void report(Diagnostic d) { |
|
73 |
error(d.toString()); |
|
74 |
} |
|
75 |
}; |
|
76 |
||
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
23122
diff
changeset
|
77 |
try (StandardJavaFileManager fm = tool.getStandardFileManager(dl, null, null)) { |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
23122
diff
changeset
|
78 |
Iterable<? extends JavaFileObject> files = |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
23122
diff
changeset
|
79 |
fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrcDir, self + ".java"))); |
10 | 80 |
|
36526 | 81 |
Iterable<String> opts = Arrays.asList( |
40308
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
36526
diff
changeset
|
82 |
"--add-exports", "jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED", |
36526 | 83 |
"-d", ".", |
84 |
"-XDcompilePolicy=simple"); |
|
10 | 85 |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
23122
diff
changeset
|
86 |
System.err.println("simple compilation, no processing"); |
36526 | 87 |
JavacTask task = (JavacTask) tool.getTask(out, fm, dl, opts, null, files); |
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
23122
diff
changeset
|
88 |
task.setTaskListener(new MyTaskListener(task)); |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
23122
diff
changeset
|
89 |
if (!task.call()) |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
23122
diff
changeset
|
90 |
throw new AssertionError("compilation failed"); |
10 | 91 |
|
36526 | 92 |
opts = Arrays.asList( |
40308
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
36526
diff
changeset
|
93 |
"--add-exports", "jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED", |
36526 | 94 |
"-XDaccessInternalAPI", |
95 |
"-d", ".", |
|
96 |
"-processorpath", testClassDir, |
|
97 |
"-processor", self, |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
23122
diff
changeset
|
98 |
"-XDcompilePolicy=simple"); |
10 | 99 |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
23122
diff
changeset
|
100 |
System.err.println(); |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
23122
diff
changeset
|
101 |
System.err.println("compilation with processing"); |
36526 | 102 |
task = (JavacTask) tool.getTask(out, fm, dl,opts, null, files); |
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
23122
diff
changeset
|
103 |
if (!task.call()) |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
23122
diff
changeset
|
104 |
throw new AssertionError("compilation failed"); |
10 | 105 |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
23122
diff
changeset
|
106 |
if (errors > 0) |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
23122
diff
changeset
|
107 |
throw new AssertionError(errors + " errors occurred"); |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
23122
diff
changeset
|
108 |
} |
10 | 109 |
} |
110 |
||
111 |
void testElement(Trees trees, Element e) { |
|
112 |
trees.getClass(); |
|
113 |
e.getClass(); |
|
114 |
||
115 |
System.err.println("testElement: " + e); |
|
116 |
Tree tree = trees.getTree(e); |
|
117 |
//System.err.println(tree); |
|
118 |
||
119 |
if (TreeInfo.symbolFor((JCTree)tree) != e) |
|
120 |
error("bad result from getTree"); |
|
121 |
||
122 |
TreePath path = trees.getPath(e); |
|
123 |
if (path == null) { |
|
124 |
error("getPath returned null"); |
|
125 |
return; |
|
126 |
} |
|
127 |
if (path.getLeaf() != tree) |
|
128 |
error("bad result from getPath"); |
|
129 |
||
130 |
Element e2 = trees.getElement(path); |
|
131 |
if (e2 == null) { |
|
132 |
error("getElement returned null"); |
|
133 |
return; |
|
134 |
} |
|
135 |
if (e2 != e) |
|
136 |
error("bad result from getElement"); |
|
137 |
||
138 |
// The TypeMirror is not available yet when annotation processing; |
|
139 |
// it is set up later during ANALYSE. |
|
140 |
TypeMirror t = trees.getTypeMirror(path); |
|
141 |
if (t != null && t.getKind() == TypeKind.DECLARED && |
|
142 |
((DeclaredType)t).asElement() != e2) |
|
143 |
error("bad result from getTypeMirror"); |
|
144 |
||
145 |
for (AnnotationMirror m: e.getAnnotationMirrors()) { |
|
146 |
testAnnotation(trees, e, m); |
|
147 |
} |
|
148 |
} |
|
149 |
||
150 |
void testAnnotation(Trees trees, Element e, AnnotationMirror a) { |
|
151 |
System.err.println("testAnnotation: " + e + " " + a); |
|
152 |
Tree tree = trees.getTree(e, a); |
|
153 |
||
23122
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
154 |
if (tree.getKind() != Tree.Kind.ANNOTATION && tree.getKind() != Tree.Kind.TYPE_ANNOTATION) |
10 | 155 |
error("bad result from getTree"); |
156 |
||
157 |
TreePath path = trees.getPath(e, a); |
|
158 |
if (path.getLeaf() != tree) |
|
159 |
error("bad result from getPath"); |
|
160 |
} |
|
161 |
||
23122
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
162 |
void testAllDeclarations(Trees trees, CompilationUnitTree cut) { |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
163 |
new TreePathScanner<Void, Void>() { |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
164 |
@Override public Void scan(Tree tree, Void p) { |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
165 |
if (tree == null) return null; |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
166 |
switch (tree.getKind()) { |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
167 |
case METHOD: case CLASS: case VARIABLE: case TYPE_PARAMETER: |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
168 |
TreePath path = new TreePath(getCurrentPath(), tree); |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
169 |
Element el = trees.getElement(path); |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
170 |
if (el == null) { |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
171 |
error("null element"); |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
172 |
} else { |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
173 |
TreePath inferred = trees.getPath(el); |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
174 |
if (inferred == null) { |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
175 |
error("null path"); |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
176 |
} else { |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
177 |
if (inferred.getLeaf() != path.getLeaf()) |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
178 |
error("bad result from getPath"); |
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 |
if (trees.getTree(el) != path.getLeaf()) |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
181 |
error("bad result from getTree"); |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
182 |
for (AnnotationMirror m: el.getAnnotationMirrors()) { |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
183 |
testAnnotation(trees, el, m); |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
184 |
} |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
185 |
} |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
186 |
} |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
187 |
return super.scan(tree, p); |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
188 |
} |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
189 |
}.scan(cut, null); |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
190 |
} |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
191 |
|
10 | 192 |
void error(String msg) { |
193 |
if (messager != null) |
|
194 |
// annotation processing will happen in a separate instance/classloader |
|
195 |
// so pass the message back to the calling instance. |
|
196 |
messager.printMessage(Diagnostic.Kind.ERROR, msg); |
|
197 |
else { |
|
198 |
System.err.println(msg); |
|
199 |
errors++; |
|
200 |
} |
|
201 |
||
202 |
} |
|
203 |
||
204 |
Messager messager; |
|
205 |
int errors; |
|
206 |
||
207 |
||
208 |
public boolean process(Set<? extends TypeElement> annos, RoundEnvironment rEnv) { |
|
209 |
Trees trees = Trees.instance(processingEnv); |
|
210 |
messager = processingEnv.getMessager(); |
|
211 |
||
212 |
for (Element e: rEnv.getRootElements()) { |
|
213 |
testElement(trees, e); |
|
214 |
} |
|
215 |
||
216 |
for (TypeElement anno: annos) { |
|
217 |
Set<? extends Element> elts = rEnv.getElementsAnnotatedWith(anno); |
|
218 |
System.err.println("anno: " + anno); |
|
219 |
System.err.println("elts: " + elts); |
|
220 |
if (elts != null) { // 6397298, should return empty set |
|
221 |
for (Element e: rEnv.getElementsAnnotatedWith(anno)) |
|
222 |
testElement(trees, e); |
|
223 |
} |
|
224 |
} |
|
225 |
||
226 |
return true; |
|
227 |
} |
|
228 |
||
229 |
@Override |
|
230 |
public SourceVersion getSupportedSourceVersion() { |
|
231 |
return SourceVersion.latest(); |
|
232 |
} |
|
233 |
||
234 |
class MyTaskListener implements TaskListener { |
|
235 |
MyTaskListener(JavacTask task) { |
|
236 |
this.task = task; |
|
237 |
} |
|
238 |
||
239 |
public void started(TaskEvent e) { |
|
240 |
System.err.println("started " + e); |
|
241 |
} |
|
242 |
||
243 |
public void finished(TaskEvent e) { |
|
244 |
//System.err.println("finished " + e); |
|
245 |
switch (e.getKind()) { |
|
246 |
case ANALYZE: |
|
247 |
testElement(Trees.instance(task), e.getTypeElement()); |
|
23122
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
248 |
testAllDeclarations(Trees.instance(task), e.getCompilationUnit()); |
10 | 249 |
break; |
250 |
} |
|
251 |
} |
|
252 |
||
253 |
private final JavacTask task; |
|
254 |
} |
|
255 |
||
23122
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
256 |
public static class TestTypeParams<@Anno T extends CharSequence> { |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
257 |
public <@Anno T extends Object> TestTypeParams(T param) { } |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
258 |
public <@Anno T extends Number> void m(T param) { |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
259 |
int local; |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
260 |
try { |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
261 |
new String(); |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
262 |
} catch (Exception exc) { } |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
263 |
} |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
264 |
} |
10 | 265 |
} |
266 |
||
267 |
@Retention(RetentionPolicy.SOURCE) |
|
23122
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
268 |
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.TYPE_PARAMETER, |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
269 |
ElementType.FIELD, ElementType.LOCAL_VARIABLE}) |
10 | 270 |
@interface Anno { |
271 |
} |