author | jlahoda |
Fri, 23 Sep 2016 15:35:32 +0200 | |
changeset 41160 | 61082a97bbf0 |
parent 30730 | d3ce7619db2c |
permissions | -rw-r--r-- |
23810
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
1 |
/* |
41160
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
2 |
* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved. |
23810
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
4 |
* |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
8 |
* |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
13 |
* accompanied this code). |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
14 |
* |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
18 |
* |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
21 |
* questions. |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
22 |
*/ |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
23 |
|
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
24 |
/* |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
25 |
* @test |
41160
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
26 |
* @bug 8038455 8047347 |
23810
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
27 |
* @summary Verify that in-method ClassSymbols from one round do not affect ClassSymbols in |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
28 |
* following rounds. |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
29 |
* @library /tools/javac/lib |
30730
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
23810
diff
changeset
|
30 |
* @modules jdk.compiler |
23810
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
31 |
* @build JavacTestingAbstractProcessor OnDemandAttribution |
41160
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
32 |
* @clean Gen |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
33 |
* @compile/process -processor OnDemandAttribution OnDemandAttributionData.java |
23810
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
34 |
*/ |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
35 |
|
41160
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
36 |
import java.io.IOException; |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
37 |
import java.io.Writer; |
23810
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
38 |
import java.util.*; |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
39 |
import javax.annotation.processing.*; |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
40 |
import javax.lang.model.element.*; |
41160
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
41 |
import static javax.lang.model.util.ElementFilter.constructorsIn; |
23810
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
42 |
import com.sun.source.tree.*; |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
43 |
import com.sun.source.util.*; |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
44 |
|
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
45 |
public class OnDemandAttribution extends JavacTestingAbstractProcessor { |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
46 |
|
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
47 |
public OnDemandAttribution() { |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
48 |
} |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
49 |
|
41160
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
50 |
int round; |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
51 |
Set<String> roots = new HashSet<>(); |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
52 |
|
23810
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
53 |
public boolean process(Set<? extends TypeElement> annos,RoundEnvironment rEnv) { |
41160
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
54 |
for (Element root : rEnv.getRootElements()) { |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
55 |
while (root.getEnclosingElement().getKind() != ElementKind.PACKAGE) { |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
56 |
root = root.getEnclosingElement(); |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
57 |
} |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
58 |
roots.add(((TypeElement) root).getQualifiedName().toString()); |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
59 |
} |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
60 |
for (String root : roots) { |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
61 |
TypeElement currentClass = elements.getTypeElement(root); |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
62 |
ExecutableElement constr = constructorsIn(currentClass.getEnclosedElements()).get(0); |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
63 |
Trees trees = Trees.instance(processingEnv); |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
64 |
TreePath path = trees.getPath(constr); |
23810
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
65 |
|
41160
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
66 |
new TreePathScanner<Void, Void>() { |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
67 |
@Override public Void visitClass(ClassTree node, Void p) { |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
68 |
if (node.getSimpleName().contentEquals("Local")) { |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
69 |
//will also attribute the body on demand: |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
70 |
Element el = trees.getElement(getCurrentPath()); |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
71 |
Name binaryName = elements.getBinaryName((TypeElement) el); |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
72 |
if (!binaryName.contentEquals("OnDemandAttributionData$1Local")) { |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
73 |
throw new IllegalStateException("Incorrect binary name=" + binaryName); |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
74 |
} |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
75 |
} |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
76 |
return super.visitClass(node, p); |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
77 |
} |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
78 |
@Override public Void visitNewClass(NewClassTree node, Void p) { |
23810
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
79 |
Element el = trees.getElement(getCurrentPath()); |
41160
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
80 |
Name binaryName = elements.getBinaryName((TypeElement) el.getEnclosingElement()); |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
81 |
if (!binaryName.contentEquals("OnDemandAttributionData$1")) { |
23810
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
82 |
throw new IllegalStateException("Incorrect binary name=" + binaryName); |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
83 |
} |
41160
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
84 |
return super.visitNewClass(node, p); |
23810
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
85 |
} |
41160
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
86 |
@Override |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
87 |
public Void visitMethodInvocation(MethodInvocationTree node, Void p) { |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
88 |
if (trees.getElement(getCurrentPath()) == null) |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
89 |
throw new IllegalStateException("No element for: " + node); |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
90 |
return super.visitMethodInvocation(node, p); |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
91 |
} |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
92 |
@Override |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
93 |
public Void visitIdentifier(IdentifierTree node, Void p) { |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
94 |
if (trees.getElement(getCurrentPath()) == null) |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
95 |
throw new IllegalStateException("No element for: " + node); |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
96 |
return super.visitIdentifier(node, p); |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
97 |
} |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
98 |
}.scan(path, null); |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
99 |
} |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
100 |
|
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
101 |
if (round++ == 0) { |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
102 |
try (Writer out = filer.createSourceFile("Gen").openWriter()) { |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
103 |
out.write("class Gen { public static long C = 1; }"); |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
104 |
} catch (IOException ex) { |
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
105 |
throw new IllegalStateException(ex); |
23810
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
106 |
} |
41160
61082a97bbf0
8047347: com.sun.source.util.Trees breaks the compiler.
jlahoda
parents:
30730
diff
changeset
|
107 |
} |
23810
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
108 |
|
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
109 |
return true; |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
110 |
} |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
111 |
} |