author | duke |
Wed, 05 Jul 2017 23:01:50 +0200 | |
changeset 44228 | e46434c65a2b |
parent 41938 | 8e66bf10fcec |
permissions | -rw-r--r-- |
10 | 1 |
/* |
40308
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
36526
diff
changeset
|
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 |
|
26 |
* @bug 6403466 |
|
27 |
* @summary javac TaskListener should be informed when annotation processing occurs |
|
30730
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
27319
diff
changeset
|
28 |
* @modules jdk.compiler/com.sun.tools.javac.api |
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
27319
diff
changeset
|
29 |
* jdk.compiler/com.sun.tools.javac.file |
10 | 30 |
*/ |
31 |
||
32 |
import com.sun.source.util.*; |
|
33 |
import java.io.*; |
|
34 |
import java.lang.annotation.*; |
|
35 |
import java.util.*; |
|
36 |
import javax.annotation.processing.*; |
|
37 |
import javax.lang.model.*; |
|
38 |
import javax.lang.model.element.*; |
|
39 |
import javax.lang.model.type.*; |
|
40 |
import javax.lang.model.util.*; |
|
41 |
import javax.tools.*; |
|
42 |
import com.sun.tools.javac.api.JavacTool; |
|
43 |
||
44 |
@Wrap |
|
45 |
@SupportedAnnotationTypes("Wrap") |
|
46 |
public class T6403466 extends AbstractProcessor { |
|
47 |
||
48 |
static final String testSrcDir = System.getProperty("test.src"); |
|
49 |
static final String testClassDir = System.getProperty("test.classes"); |
|
50 |
static final String self = T6403466.class.getName(); |
|
51 |
static PrintWriter out = new PrintWriter(System.err, true); |
|
52 |
||
53 |
public static void main(String[] args) throws IOException { |
|
54 |
JavacTool tool = JavacTool.create(); |
|
55 |
||
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
7681
diff
changeset
|
56 |
try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) { |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
7681
diff
changeset
|
57 |
Iterable<? extends JavaFileObject> files = |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
7681
diff
changeset
|
58 |
fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrcDir, self + ".java"))); |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
7681
diff
changeset
|
59 |
|
36526 | 60 |
Iterable<String> options = Arrays.asList( |
41938
8e66bf10fcec
8167975: align javac --add-* modules options with launcher
jjg
parents:
40308
diff
changeset
|
61 |
"--add-exports", "jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED", |
40308
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
36526
diff
changeset
|
62 |
"--processor-path", testClassDir, |
36526 | 63 |
"-processor", self, |
64 |
"-s", ".", |
|
65 |
"-d", "."); |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
7681
diff
changeset
|
66 |
JavacTask task = tool.getTask(out, fm, null, options, null, files); |
10 | 67 |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
7681
diff
changeset
|
68 |
VerifyingTaskListener vtl = new VerifyingTaskListener(new File(testSrcDir, self + ".out")); |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
7681
diff
changeset
|
69 |
task.setTaskListener(vtl); |
10 | 70 |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
7681
diff
changeset
|
71 |
if (!task.call()) |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
7681
diff
changeset
|
72 |
throw new AssertionError("compilation failed"); |
10 | 73 |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
7681
diff
changeset
|
74 |
if (vtl.iter.hasNext() || vtl.errors) |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
7681
diff
changeset
|
75 |
throw new AssertionError("comparison against golden file failed."); |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
7681
diff
changeset
|
76 |
} |
10 | 77 |
} |
78 |
||
79 |
public boolean process(Set<? extends TypeElement> annos, RoundEnvironment rEnv) { |
|
4934 | 80 |
if (!rEnv.processingOver()) { |
81 |
Filer filer = processingEnv.getFiler(); |
|
82 |
for (TypeElement anno: annos) { |
|
83 |
Set<? extends Element> elts = rEnv.getElementsAnnotatedWith(anno); |
|
84 |
System.err.println("anno: " + anno); |
|
85 |
System.err.println("elts: " + elts); |
|
86 |
for (TypeElement te: ElementFilter.typesIn(elts)) { |
|
87 |
try { |
|
88 |
Writer out = filer.createSourceFile(te.getSimpleName() + "Wrapper").openWriter(); |
|
89 |
out.write("class " + te.getSimpleName() + "Wrapper { }"); |
|
90 |
out.close(); |
|
91 |
} catch (IOException ex) { |
|
92 |
ex.printStackTrace(); |
|
93 |
} |
|
10 | 94 |
} |
4934 | 95 |
|
10 | 96 |
} |
97 |
} |
|
98 |
return true; |
|
99 |
} |
|
4934 | 100 |
|
101 |
@Override |
|
102 |
public SourceVersion getSupportedSourceVersion() { |
|
103 |
return SourceVersion.latest(); |
|
104 |
} |
|
10 | 105 |
} |
106 |
||
107 |
@Retention(RetentionPolicy.SOURCE) |
|
108 |
@Target(ElementType.TYPE) |
|
109 |
@interface Wrap { |
|
110 |
} |
|
111 |
||
112 |
||
113 |
class VerifyingTaskListener implements TaskListener { |
|
114 |
VerifyingTaskListener(File ref) throws IOException { |
|
115 |
BufferedReader in = new BufferedReader(new FileReader(ref)); |
|
116 |
String line; |
|
117 |
List<String> lines = new ArrayList<String>(); |
|
118 |
while ((line = in.readLine()) != null) |
|
119 |
lines.add(line); |
|
120 |
in.close(); |
|
121 |
iter = lines.iterator(); |
|
122 |
} |
|
123 |
||
124 |
public void started(TaskEvent e) { |
|
125 |
check("Started " + toString(e)); |
|
126 |
} |
|
127 |
public void finished(TaskEvent e) { |
|
128 |
check("Finished " + toString(e)); |
|
129 |
} |
|
130 |
||
131 |
// similar to TaskEvent.toString(), but just prints basename of the file |
|
132 |
private String toString(TaskEvent e) { |
|
133 |
JavaFileObject file = e.getSourceFile(); |
|
134 |
return "TaskEvent[" |
|
135 |
+ e.getKind() + "," |
|
136 |
+ (file == null ? null : new File(file.toUri().getPath()).getName()) + "," |
|
137 |
// the compilation unit is identified by the file |
|
138 |
+ e.getTypeElement() + "]"; |
|
139 |
} |
|
140 |
||
141 |
private void check(String s) { |
|
142 |
System.out.println(s); // write a reference copy of expected output to stdout |
|
143 |
||
144 |
String ref = iter.hasNext() ? iter.next() : null; |
|
145 |
line++; |
|
146 |
if (!s.equals(ref)) { |
|
147 |
if (ref != null) |
|
148 |
System.err.println(line + ": expected: " + ref); |
|
149 |
System.err.println(line + ": found: " + s); |
|
150 |
errors = true; |
|
151 |
} |
|
152 |
} |
|
153 |
||
154 |
Iterator<String> iter; |
|
155 |
int line; |
|
156 |
boolean errors; |
|
157 |
} |