author | jlahoda |
Tue, 08 Apr 2014 23:33:50 +0200 | |
changeset 23811 | 4bbbcaad653f |
parent 23810 | b92eb80925f0 |
child 26100 | bb7dd001d190 |
permissions | -rw-r--r-- |
23810
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
1 |
/* |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
2 |
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
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 |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
26 |
* @bug 8038455 |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
27 |
* @summary Verify that annotation processor can overwrite source and class files it generated |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
28 |
* during previous compilations, and that the Symbols are updated appropriatelly. |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
29 |
* @library /tools/javac/lib/ |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
30 |
* @clean * |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
31 |
* @build OverwriteBetweenCompilations ToolBox JavacTestingAbstractProcessor |
23811 | 32 |
* @compile/ref=OverwriteBetweenCompilations_1.out -processor OverwriteBetweenCompilations -Apass=1 -parameters -XDrawDiagnostics OverwriteBetweenCompilationsSource.java |
33 |
* @compile/ref=OverwriteBetweenCompilations_2.out -processor OverwriteBetweenCompilations -Apass=2 -parameters -XDrawDiagnostics OverwriteBetweenCompilationsSource.java |
|
34 |
* @compile/ref=OverwriteBetweenCompilations_3.out -processor OverwriteBetweenCompilations -Apass=3 -parameters -XDrawDiagnostics OverwriteBetweenCompilationsSource.java |
|
23810
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
35 |
*/ |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
36 |
|
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
37 |
import java.io.*; |
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 |
|
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
40 |
import javax.annotation.processing.*; |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
41 |
import javax.lang.model.element.*; |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
42 |
import javax.tools.*; |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
43 |
|
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
44 |
import com.sun.tools.javac.processing.JavacProcessingEnvironment; |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
45 |
import com.sun.tools.javac.processing.PrintingProcessor.PrintingElementVisitor; |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
46 |
import com.sun.tools.javac.util.Log; |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
47 |
import com.sun.tools.javac.util.Log.WriterKind; |
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 |
@SupportedOptions("pass") |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
50 |
public class OverwriteBetweenCompilations extends JavacTestingAbstractProcessor { |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
51 |
int round = 1; |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
52 |
public boolean process(Set<? extends TypeElement> annotations, |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
53 |
RoundEnvironment roundEnv) { |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
54 |
Log log = Log.instance(((JavacProcessingEnvironment) processingEnv).getContext()); |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
55 |
PrintWriter pw = log.getWriter(WriterKind.NOTICE); |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
56 |
|
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
57 |
pw.println("round: " + round); |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
58 |
|
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
59 |
TypeElement generatedSource = |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
60 |
processingEnv.getElementUtils().getTypeElement("GeneratedSource"); |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
61 |
|
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
62 |
if (generatedSource != null) { |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
63 |
new PrintingElementVisitor(pw, processingEnv.getElementUtils()).visit(generatedSource); |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
64 |
pw.flush(); |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
65 |
} |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
66 |
|
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
67 |
TypeElement generatedClass = |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
68 |
processingEnv.getElementUtils().getTypeElement("GeneratedClass"); |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
69 |
|
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
70 |
if (generatedClass != null) { |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
71 |
new PrintingElementVisitor(pw, processingEnv.getElementUtils()).visit(generatedClass); |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
72 |
pw.flush(); |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
73 |
} |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
74 |
|
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
75 |
int pass = Integer.parseInt(processingEnv.getOptions().get("pass")); |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
76 |
|
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
77 |
if (round++ == 1) { |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
78 |
try (Writer out = filer.createSourceFile("GeneratedSource").openWriter()) { |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
79 |
String code = pass != 2 ? GENERATED_INIT : GENERATED_UPDATE; |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
80 |
code = code.replace("NAME", "GeneratedSource"); |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
81 |
out.write(code); |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
82 |
} catch (IOException e) { |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
83 |
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, e.toString()); |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
84 |
} |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
85 |
try (OutputStream out = filer.createClassFile("GeneratedClass").openOutputStream()) { |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
86 |
String code = pass != 2 ? GENERATED_INIT : GENERATED_UPDATE; |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
87 |
code = code.replace("NAME", "GeneratedClass"); |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
88 |
ToolBox.JavaToolArgs args = |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
89 |
new ToolBox.JavaToolArgs().appendArgs("-parameters").setSources(code); |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
90 |
Map<String, byte[]> codeMap = ToolBox.compile(args); |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
91 |
out.write(codeMap.get("GeneratedClass")); |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
92 |
} catch (IOException | ToolBox.CommandExecutionException e) { |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
93 |
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, e.toString()); |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
94 |
} |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
95 |
} |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
96 |
|
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
97 |
return false; |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
98 |
} |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
99 |
|
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
100 |
//the initial generated class - "NAME" will be replaced with either "GeneratedSource" or |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
101 |
//"GeneratedClass" while generating the class: |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
102 |
private static final String GENERATED_INIT = |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
103 |
"@Deprecated\n" + |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
104 |
"public class NAME<T extends CharSequence> extends java.util.ArrayList<String>\n" + |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
105 |
" implements Runnable {\n" + |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
106 |
" public void test(int a) { }\n" + |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
107 |
" public void run() { }\n" + |
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 |
|
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
110 |
//generated class update- "NAME" will be replaced with either "GeneratedSource" or |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
111 |
//"GeneratedClass" while generating the class: |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
112 |
private static final String GENERATED_UPDATE = |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
113 |
"@javax.annotation.processing.SupportedAnnotationTypes(\"*\")\n" + |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
114 |
"public abstract class NAME<E extends Number> extends java.util.LinkedList<Number>" + |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
115 |
" implements Runnable, CharSequence {\n" + |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
116 |
" public void test(long a) { }\n" + |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
117 |
"}"; |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
diff
changeset
|
118 |
} |