author | avstepan |
Tue, 19 May 2015 16:04:14 +0400 | |
changeset 30655 | d83f50188ca9 |
parent 27319 | 030080f03e4f |
child 30730 | d3ce7619db2c |
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 |
||
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 |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
23122
diff
changeset
|
81 |
Iterable<String> opts = Arrays.asList("-d", ".", "-XDcompilePolicy=simple"); |
10 | 82 |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
23122
diff
changeset
|
83 |
System.err.println("simple compilation, no processing"); |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
23122
diff
changeset
|
84 |
JavacTask task = tool.getTask(out, fm, dl, opts, null, files); |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
23122
diff
changeset
|
85 |
task.setTaskListener(new MyTaskListener(task)); |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
23122
diff
changeset
|
86 |
if (!task.call()) |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
23122
diff
changeset
|
87 |
throw new AssertionError("compilation failed"); |
10 | 88 |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
23122
diff
changeset
|
89 |
opts = Arrays.asList("-d", ".", "-processorpath", testClassDir, "-processor", self, |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
23122
diff
changeset
|
90 |
"-XDcompilePolicy=simple"); |
10 | 91 |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
23122
diff
changeset
|
92 |
System.err.println(); |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
23122
diff
changeset
|
93 |
System.err.println("compilation with processing"); |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
23122
diff
changeset
|
94 |
task = tool.getTask(out, fm, dl,opts, null, files); |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
23122
diff
changeset
|
95 |
if (!task.call()) |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
23122
diff
changeset
|
96 |
throw new AssertionError("compilation failed"); |
10 | 97 |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
23122
diff
changeset
|
98 |
if (errors > 0) |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
23122
diff
changeset
|
99 |
throw new AssertionError(errors + " errors occurred"); |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
23122
diff
changeset
|
100 |
} |
10 | 101 |
} |
102 |
||
103 |
void testElement(Trees trees, Element e) { |
|
104 |
trees.getClass(); |
|
105 |
e.getClass(); |
|
106 |
||
107 |
System.err.println("testElement: " + e); |
|
108 |
Tree tree = trees.getTree(e); |
|
109 |
//System.err.println(tree); |
|
110 |
||
111 |
if (TreeInfo.symbolFor((JCTree)tree) != e) |
|
112 |
error("bad result from getTree"); |
|
113 |
||
114 |
TreePath path = trees.getPath(e); |
|
115 |
if (path == null) { |
|
116 |
error("getPath returned null"); |
|
117 |
return; |
|
118 |
} |
|
119 |
if (path.getLeaf() != tree) |
|
120 |
error("bad result from getPath"); |
|
121 |
||
122 |
Element e2 = trees.getElement(path); |
|
123 |
if (e2 == null) { |
|
124 |
error("getElement returned null"); |
|
125 |
return; |
|
126 |
} |
|
127 |
if (e2 != e) |
|
128 |
error("bad result from getElement"); |
|
129 |
||
130 |
// The TypeMirror is not available yet when annotation processing; |
|
131 |
// it is set up later during ANALYSE. |
|
132 |
TypeMirror t = trees.getTypeMirror(path); |
|
133 |
if (t != null && t.getKind() == TypeKind.DECLARED && |
|
134 |
((DeclaredType)t).asElement() != e2) |
|
135 |
error("bad result from getTypeMirror"); |
|
136 |
||
137 |
for (AnnotationMirror m: e.getAnnotationMirrors()) { |
|
138 |
testAnnotation(trees, e, m); |
|
139 |
} |
|
140 |
} |
|
141 |
||
142 |
void testAnnotation(Trees trees, Element e, AnnotationMirror a) { |
|
143 |
System.err.println("testAnnotation: " + e + " " + a); |
|
144 |
Tree tree = trees.getTree(e, a); |
|
145 |
||
23122
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
146 |
if (tree.getKind() != Tree.Kind.ANNOTATION && tree.getKind() != Tree.Kind.TYPE_ANNOTATION) |
10 | 147 |
error("bad result from getTree"); |
148 |
||
149 |
TreePath path = trees.getPath(e, a); |
|
150 |
if (path.getLeaf() != tree) |
|
151 |
error("bad result from getPath"); |
|
152 |
} |
|
153 |
||
23122
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
154 |
void testAllDeclarations(Trees trees, CompilationUnitTree cut) { |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
155 |
new TreePathScanner<Void, Void>() { |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
156 |
@Override public Void scan(Tree tree, Void p) { |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
157 |
if (tree == null) return null; |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
158 |
switch (tree.getKind()) { |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
159 |
case METHOD: case CLASS: case VARIABLE: case TYPE_PARAMETER: |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
160 |
TreePath path = new TreePath(getCurrentPath(), tree); |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
161 |
Element el = trees.getElement(path); |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
162 |
if (el == null) { |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
163 |
error("null element"); |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
164 |
} else { |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
165 |
TreePath inferred = trees.getPath(el); |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
166 |
if (inferred == null) { |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
167 |
error("null path"); |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
168 |
} else { |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
169 |
if (inferred.getLeaf() != path.getLeaf()) |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
170 |
error("bad result from getPath"); |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
171 |
} |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
172 |
if (trees.getTree(el) != path.getLeaf()) |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
173 |
error("bad result from getTree"); |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
174 |
for (AnnotationMirror m: el.getAnnotationMirrors()) { |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
175 |
testAnnotation(trees, el, m); |
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 |
} |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
179 |
return super.scan(tree, p); |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
180 |
} |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
181 |
}.scan(cut, null); |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
182 |
} |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
183 |
|
10 | 184 |
void error(String msg) { |
185 |
if (messager != null) |
|
186 |
// annotation processing will happen in a separate instance/classloader |
|
187 |
// so pass the message back to the calling instance. |
|
188 |
messager.printMessage(Diagnostic.Kind.ERROR, msg); |
|
189 |
else { |
|
190 |
System.err.println(msg); |
|
191 |
errors++; |
|
192 |
} |
|
193 |
||
194 |
} |
|
195 |
||
196 |
Messager messager; |
|
197 |
int errors; |
|
198 |
||
199 |
||
200 |
public boolean process(Set<? extends TypeElement> annos, RoundEnvironment rEnv) { |
|
201 |
Trees trees = Trees.instance(processingEnv); |
|
202 |
messager = processingEnv.getMessager(); |
|
203 |
||
204 |
for (Element e: rEnv.getRootElements()) { |
|
205 |
testElement(trees, e); |
|
206 |
} |
|
207 |
||
208 |
for (TypeElement anno: annos) { |
|
209 |
Set<? extends Element> elts = rEnv.getElementsAnnotatedWith(anno); |
|
210 |
System.err.println("anno: " + anno); |
|
211 |
System.err.println("elts: " + elts); |
|
212 |
if (elts != null) { // 6397298, should return empty set |
|
213 |
for (Element e: rEnv.getElementsAnnotatedWith(anno)) |
|
214 |
testElement(trees, e); |
|
215 |
} |
|
216 |
} |
|
217 |
||
218 |
return true; |
|
219 |
} |
|
220 |
||
221 |
@Override |
|
222 |
public SourceVersion getSupportedSourceVersion() { |
|
223 |
return SourceVersion.latest(); |
|
224 |
} |
|
225 |
||
226 |
class MyTaskListener implements TaskListener { |
|
227 |
MyTaskListener(JavacTask task) { |
|
228 |
this.task = task; |
|
229 |
} |
|
230 |
||
231 |
public void started(TaskEvent e) { |
|
232 |
System.err.println("started " + e); |
|
233 |
} |
|
234 |
||
235 |
public void finished(TaskEvent e) { |
|
236 |
//System.err.println("finished " + e); |
|
237 |
switch (e.getKind()) { |
|
238 |
case ANALYZE: |
|
239 |
testElement(Trees.instance(task), e.getTypeElement()); |
|
23122
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
240 |
testAllDeclarations(Trees.instance(task), e.getCompilationUnit()); |
10 | 241 |
break; |
242 |
} |
|
243 |
} |
|
244 |
||
245 |
private final JavacTask task; |
|
246 |
} |
|
247 |
||
23122
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
248 |
public static class TestTypeParams<@Anno T extends CharSequence> { |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
249 |
public <@Anno T extends Object> TestTypeParams(T param) { } |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
250 |
public <@Anno T extends Number> void m(T param) { |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
251 |
int local; |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
252 |
try { |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
253 |
new String(); |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
254 |
} catch (Exception exc) { } |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
255 |
} |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
256 |
} |
10 | 257 |
} |
258 |
||
259 |
@Retention(RetentionPolicy.SOURCE) |
|
23122
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
260 |
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.TYPE_PARAMETER, |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
5520
diff
changeset
|
261 |
ElementType.FIELD, ElementType.LOCAL_VARIABLE}) |
10 | 262 |
@interface Anno { |
263 |
} |