author | jjg |
Wed, 29 Oct 2014 17:25:23 -0700 | |
changeset 27319 | 030080f03e4f |
parent 24898 | 88fa65d2ac87 |
child 30730 | d3ce7619db2c |
permissions | -rw-r--r-- |
12016 | 1 |
/* |
24898
88fa65d2ac87
8033414: javac Plugin to receive notification (before and) after the compilation.
jlahoda
parents:
12016
diff
changeset
|
2 |
* Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved. |
12016 | 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 |
* |
|
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. |
|
22 |
*/ |
|
23 |
||
24 |
/* |
|
25 |
* @test |
|
26 |
* @bug 7093891 |
|
27 |
* @summary support multiple task listeners |
|
28 |
*/ |
|
29 |
||
30 |
import java.io.File; |
|
31 |
import java.io.IOException; |
|
32 |
import java.io.PrintWriter; |
|
33 |
import java.io.StringWriter; |
|
34 |
import java.net.URI; |
|
35 |
import java.util.ArrayList; |
|
36 |
import java.util.Arrays; |
|
37 |
import java.util.EnumMap; |
|
38 |
import java.util.List; |
|
39 |
import java.util.Set; |
|
40 |
||
41 |
import javax.annotation.processing.AbstractProcessor; |
|
42 |
import javax.annotation.processing.RoundEnvironment; |
|
43 |
import javax.annotation.processing.SupportedAnnotationTypes; |
|
44 |
import javax.lang.model.element.TypeElement; |
|
45 |
import javax.tools.JavaFileObject; |
|
46 |
import javax.tools.SimpleJavaFileObject; |
|
47 |
import javax.tools.StandardJavaFileManager; |
|
48 |
import javax.tools.StandardLocation; |
|
49 |
import javax.tools.ToolProvider; |
|
50 |
||
51 |
import com.sun.source.util.JavacTask; |
|
52 |
import com.sun.source.util.TaskEvent; |
|
53 |
import com.sun.source.util.TaskListener; |
|
54 |
import com.sun.tools.javac.api.JavacTool; |
|
55 |
||
56 |
public class TestSimpleAddRemove { |
|
57 |
enum AddKind { |
|
58 |
SET_IN_TASK, |
|
59 |
ADD_IN_TASK, |
|
60 |
ADD_IN_PROCESSOR, |
|
61 |
ADD_IN_LISTENER; |
|
62 |
} |
|
63 |
||
64 |
enum RemoveKind { |
|
65 |
REMOVE_IN_TASK, |
|
66 |
REMOVE_IN_PROCESSOR, |
|
67 |
REMOVE_IN_LISTENER, |
|
68 |
} |
|
69 |
||
70 |
enum CompileKind { |
|
71 |
CALL { |
|
72 |
void run(JavacTask t) { |
|
73 |
if (!t.call()) throw new Error("compilation failed"); |
|
74 |
} |
|
75 |
}, |
|
76 |
GENERATE { |
|
77 |
void run(JavacTask t) throws IOException { |
|
78 |
t.generate(); |
|
79 |
} |
|
80 |
}; |
|
81 |
abstract void run(JavacTask t) throws IOException; |
|
82 |
} |
|
83 |
||
84 |
static class EventKindCounter extends EnumMap<TaskEvent.Kind, EventKindCounter.Count> { |
|
85 |
static class Count { |
|
86 |
int started; |
|
87 |
int finished; |
|
88 |
||
89 |
@Override |
|
90 |
public String toString() { |
|
91 |
return started + ":" + finished; |
|
92 |
} |
|
93 |
} |
|
94 |
||
95 |
EventKindCounter() { |
|
96 |
super(TaskEvent.Kind.class); |
|
97 |
} |
|
98 |
||
99 |
void inc(TaskEvent.Kind k, boolean started) { |
|
100 |
Count c = get(k); |
|
101 |
if (c == null) |
|
102 |
put(k, c = new Count()); |
|
103 |
||
104 |
if (started) |
|
105 |
c.started++; |
|
106 |
else |
|
107 |
c.finished++; |
|
108 |
} |
|
109 |
} |
|
110 |
||
111 |
static class TestListener implements TaskListener { |
|
112 |
EventKindCounter counter; |
|
113 |
||
114 |
TestListener(EventKindCounter c) { |
|
115 |
counter = c; |
|
116 |
} |
|
117 |
||
118 |
public void started(TaskEvent e) { |
|
119 |
counter.inc(e.getKind(), true); |
|
120 |
} |
|
121 |
||
122 |
public void finished(TaskEvent e) { |
|
123 |
counter.inc(e.getKind(), false); |
|
124 |
} |
|
125 |
} |
|
126 |
||
127 |
static void addInListener(final JavacTask task, final TaskEvent.Kind kind, final TaskListener listener) { |
|
128 |
task.addTaskListener(new TaskListener() { |
|
129 |
public void started(TaskEvent e) { |
|
130 |
if (e.getKind() == kind) { |
|
131 |
task.addTaskListener(listener); |
|
132 |
task.removeTaskListener(this); |
|
133 |
} |
|
134 |
} |
|
135 |
||
136 |
public void finished(TaskEvent e) { } |
|
137 |
}); |
|
138 |
} |
|
139 |
||
140 |
static void removeInListener(final JavacTask task, final TaskEvent.Kind kind, final TaskListener listener) { |
|
141 |
task.addTaskListener(new TaskListener() { |
|
142 |
public void started(TaskEvent e) { |
|
143 |
if (e.getKind() == kind) { |
|
144 |
task.removeTaskListener(listener); |
|
145 |
task.removeTaskListener(this); |
|
146 |
} |
|
147 |
} |
|
148 |
||
149 |
public void finished(TaskEvent e) { } |
|
150 |
}); |
|
151 |
} |
|
152 |
||
153 |
@SupportedAnnotationTypes("*") |
|
154 |
class TestProcessor extends AbstractProcessor { |
|
155 |
AddKind ak; |
|
156 |
RemoveKind rk; |
|
157 |
TaskListener listener; |
|
158 |
||
159 |
TestProcessor(AddKind ak, RemoveKind rk, TaskListener listener) { |
|
160 |
this.ak = ak; |
|
161 |
this.rk = rk; |
|
162 |
this.listener = listener; |
|
163 |
} |
|
164 |
||
165 |
int round = 0; |
|
166 |
||
167 |
@Override |
|
168 |
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
|
169 |
// System.err.println("TestProcessor.process " + roundEnv); |
|
170 |
JavacTask task = JavacTask.instance(processingEnv); |
|
171 |
if (++round == 1) { |
|
172 |
switch (ak) { |
|
173 |
case ADD_IN_PROCESSOR: |
|
174 |
task.addTaskListener(listener); |
|
175 |
break; |
|
176 |
case ADD_IN_LISTENER: |
|
177 |
addInListener(task, TaskEvent.Kind.ANALYZE, listener); |
|
178 |
break; |
|
179 |
} |
|
180 |
} else if (roundEnv.processingOver()) { |
|
181 |
switch (rk) { |
|
182 |
case REMOVE_IN_PROCESSOR: |
|
183 |
task.removeTaskListener(listener); |
|
184 |
break; |
|
185 |
case REMOVE_IN_LISTENER: |
|
186 |
removeInListener(task, TaskEvent.Kind.GENERATE, listener); |
|
187 |
break; |
|
188 |
} |
|
189 |
} |
|
190 |
return true; |
|
191 |
} |
|
192 |
} |
|
193 |
||
194 |
static class TestSource extends SimpleJavaFileObject { |
|
195 |
public TestSource() { |
|
196 |
super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); |
|
197 |
} |
|
198 |
||
199 |
@Override |
|
200 |
public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
|
201 |
return "class Test { }"; |
|
202 |
} |
|
203 |
} |
|
204 |
||
205 |
public static void main(String... args) throws Exception { |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
24898
diff
changeset
|
206 |
TestSimpleAddRemove t = new TestSimpleAddRemove(); |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
24898
diff
changeset
|
207 |
try { |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
24898
diff
changeset
|
208 |
t.run(); |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
24898
diff
changeset
|
209 |
} finally { |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
24898
diff
changeset
|
210 |
t.fm.close(); |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
24898
diff
changeset
|
211 |
} |
12016 | 212 |
} |
213 |
||
214 |
JavacTool tool = (JavacTool) ToolProvider.getSystemJavaCompiler(); |
|
215 |
StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null); |
|
216 |
||
217 |
void run() throws Exception { |
|
218 |
for (CompileKind ck: CompileKind.values()) { |
|
219 |
for (AddKind ak: AddKind.values()) { |
|
220 |
for (RemoveKind rk: RemoveKind.values()) { |
|
221 |
test(ck, ak, rk); |
|
222 |
} |
|
223 |
} |
|
224 |
} |
|
225 |
if (errors > 0) |
|
226 |
throw new Exception(errors + " errors occurred"); |
|
227 |
} |
|
228 |
||
229 |
void test(CompileKind ck, AddKind ak, RemoveKind rk) throws IOException { |
|
230 |
System.err.println("Test: " + ck + " " + ak + " " + rk); |
|
231 |
||
232 |
File tmpDir = new File(ck + "-" + ak + "-" + rk); |
|
233 |
tmpDir.mkdirs(); |
|
234 |
fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(tmpDir)); |
|
235 |
||
236 |
List<String> options = new ArrayList<String>(); |
|
237 |
Iterable<? extends JavaFileObject> files = Arrays.asList(new TestSource()); |
|
238 |
StringWriter sw = new StringWriter(); |
|
239 |
PrintWriter pw = new PrintWriter(sw); |
|
240 |
JavacTask task = tool.getTask(pw, fm, null, options, null, files); |
|
241 |
||
242 |
EventKindCounter ec = new EventKindCounter(); |
|
243 |
TaskListener listener = new TestListener(ec); |
|
244 |
boolean needProcessor = false; |
|
245 |
||
246 |
switch (ak) { |
|
247 |
case SET_IN_TASK: |
|
248 |
task.setTaskListener(listener); |
|
249 |
break; |
|
250 |
case ADD_IN_TASK: |
|
251 |
task.addTaskListener(listener); |
|
252 |
break; |
|
253 |
case ADD_IN_PROCESSOR: |
|
254 |
case ADD_IN_LISTENER: |
|
255 |
needProcessor = true; |
|
256 |
} |
|
257 |
||
258 |
switch (rk) { |
|
259 |
case REMOVE_IN_TASK: |
|
260 |
task.removeTaskListener(listener); |
|
261 |
break; |
|
262 |
case REMOVE_IN_PROCESSOR: |
|
263 |
case REMOVE_IN_LISTENER: |
|
264 |
needProcessor = true; |
|
265 |
} |
|
266 |
||
267 |
if (needProcessor) |
|
268 |
task.setProcessors(Arrays.asList(new TestProcessor(ak, rk, listener))); |
|
269 |
||
270 |
ck.run(task); |
|
271 |
System.err.println(ec); |
|
272 |
||
273 |
check(ck, ak, rk, ec); |
|
274 |
||
275 |
System.err.println(); |
|
276 |
} |
|
277 |
||
278 |
void check(CompileKind ck, AddKind ak, RemoveKind rk, EventKindCounter ec) { |
|
279 |
// All results should be independent of ck, so we can ignore that |
|
280 |
||
281 |
// Quick way to compare expected values of ec, by comparing ec.toString() |
|
282 |
String expect = ec.toString(); |
|
283 |
String found; |
|
284 |
||
285 |
switch (ak) { |
|
286 |
// Add/set in task should record all events until the listener is removed |
|
287 |
case SET_IN_TASK: |
|
288 |
case ADD_IN_TASK: |
|
289 |
switch (rk) { |
|
290 |
case REMOVE_IN_TASK: |
|
291 |
// Remove will succeed, meaning no events will be recorded |
|
292 |
found = "{}"; |
|
293 |
break; |
|
294 |
case REMOVE_IN_PROCESSOR: |
|
24898
88fa65d2ac87
8033414: javac Plugin to receive notification (before and) after the compilation.
jlahoda
parents:
12016
diff
changeset
|
295 |
if (ck == CompileKind.CALL) |
88fa65d2ac87
8033414: javac Plugin to receive notification (before and) after the compilation.
jlahoda
parents:
12016
diff
changeset
|
296 |
found = "{PARSE=1:1, ENTER=2:2, ANNOTATION_PROCESSING=1:0, ANNOTATION_PROCESSING_ROUND=2:1, COMPILATION=1:0}"; |
88fa65d2ac87
8033414: javac Plugin to receive notification (before and) after the compilation.
jlahoda
parents:
12016
diff
changeset
|
297 |
else |
88fa65d2ac87
8033414: javac Plugin to receive notification (before and) after the compilation.
jlahoda
parents:
12016
diff
changeset
|
298 |
found = "{PARSE=1:1, ENTER=2:2, ANNOTATION_PROCESSING=1:0, ANNOTATION_PROCESSING_ROUND=2:1}"; |
12016 | 299 |
break; |
300 |
case REMOVE_IN_LISTENER: |
|
24898
88fa65d2ac87
8033414: javac Plugin to receive notification (before and) after the compilation.
jlahoda
parents:
12016
diff
changeset
|
301 |
if (ck == CompileKind.CALL) |
88fa65d2ac87
8033414: javac Plugin to receive notification (before and) after the compilation.
jlahoda
parents:
12016
diff
changeset
|
302 |
found = "{PARSE=1:1, ENTER=3:3, ANALYZE=1:1, GENERATE=1:0, ANNOTATION_PROCESSING=1:1, ANNOTATION_PROCESSING_ROUND=2:2, COMPILATION=1:0}"; |
88fa65d2ac87
8033414: javac Plugin to receive notification (before and) after the compilation.
jlahoda
parents:
12016
diff
changeset
|
303 |
else |
88fa65d2ac87
8033414: javac Plugin to receive notification (before and) after the compilation.
jlahoda
parents:
12016
diff
changeset
|
304 |
found = "{PARSE=1:1, ENTER=3:3, ANALYZE=1:1, GENERATE=1:0, ANNOTATION_PROCESSING=1:1, ANNOTATION_PROCESSING_ROUND=2:2}"; |
12016 | 305 |
break; |
306 |
default: |
|
307 |
throw new IllegalStateException(); |
|
308 |
} |
|
309 |
break; |
|
310 |
||
311 |
// "Add in processor" should skip initial PARSE/ENTER events |
|
312 |
case ADD_IN_PROCESSOR: |
|
313 |
switch (rk) { |
|
314 |
// Remove will fail (too early), so events to end will be recorded |
|
315 |
case REMOVE_IN_TASK: |
|
24898
88fa65d2ac87
8033414: javac Plugin to receive notification (before and) after the compilation.
jlahoda
parents:
12016
diff
changeset
|
316 |
if (ck == CompileKind.CALL) |
88fa65d2ac87
8033414: javac Plugin to receive notification (before and) after the compilation.
jlahoda
parents:
12016
diff
changeset
|
317 |
found = "{ENTER=2:2, ANALYZE=1:1, GENERATE=1:1, ANNOTATION_PROCESSING=0:1, ANNOTATION_PROCESSING_ROUND=1:2, COMPILATION=0:1}"; |
88fa65d2ac87
8033414: javac Plugin to receive notification (before and) after the compilation.
jlahoda
parents:
12016
diff
changeset
|
318 |
else |
88fa65d2ac87
8033414: javac Plugin to receive notification (before and) after the compilation.
jlahoda
parents:
12016
diff
changeset
|
319 |
found = "{ENTER=2:2, ANALYZE=1:1, GENERATE=1:1, ANNOTATION_PROCESSING=0:1, ANNOTATION_PROCESSING_ROUND=1:2}"; |
12016 | 320 |
break; |
321 |
case REMOVE_IN_PROCESSOR: |
|
322 |
found = "{ENTER=1:1, ANNOTATION_PROCESSING_ROUND=1:1}"; |
|
323 |
break; |
|
324 |
case REMOVE_IN_LISTENER: |
|
325 |
found = "{ENTER=2:2, ANALYZE=1:1, GENERATE=1:0, ANNOTATION_PROCESSING=0:1, ANNOTATION_PROCESSING_ROUND=1:2}"; |
|
326 |
break; |
|
327 |
default: |
|
328 |
throw new IllegalStateException(); |
|
329 |
} |
|
330 |
break; |
|
331 |
||
332 |
// "Add in listener" will occur during "ANALYSE.started" event |
|
333 |
case ADD_IN_LISTENER: |
|
334 |
switch (rk) { |
|
335 |
// Remove will fail (too early, so events to end will be recorded |
|
336 |
case REMOVE_IN_TASK: |
|
337 |
case REMOVE_IN_PROCESSOR: |
|
24898
88fa65d2ac87
8033414: javac Plugin to receive notification (before and) after the compilation.
jlahoda
parents:
12016
diff
changeset
|
338 |
if (ck == CompileKind.CALL) |
88fa65d2ac87
8033414: javac Plugin to receive notification (before and) after the compilation.
jlahoda
parents:
12016
diff
changeset
|
339 |
found = "{ANALYZE=0:1, GENERATE=1:1, COMPILATION=0:1}"; |
88fa65d2ac87
8033414: javac Plugin to receive notification (before and) after the compilation.
jlahoda
parents:
12016
diff
changeset
|
340 |
else |
88fa65d2ac87
8033414: javac Plugin to receive notification (before and) after the compilation.
jlahoda
parents:
12016
diff
changeset
|
341 |
found = "{ANALYZE=0:1, GENERATE=1:1}"; |
12016 | 342 |
break; |
343 |
// Remove will succeed during "GENERATE.finished" event |
|
344 |
case REMOVE_IN_LISTENER: |
|
345 |
found = "{ANALYZE=0:1, GENERATE=1:0}"; |
|
346 |
break; |
|
347 |
default: |
|
348 |
throw new IllegalStateException(); |
|
349 |
} |
|
350 |
break; |
|
351 |
default: |
|
352 |
throw new IllegalStateException(); |
|
353 |
} |
|
354 |
||
355 |
if (!found.equals(expect)) { |
|
356 |
System.err.println("Expected: " + expect); |
|
357 |
System.err.println(" Found: " + found); |
|
358 |
error("unexpected value found"); |
|
359 |
} |
|
360 |
} |
|
361 |
||
362 |
int errors; |
|
363 |
||
364 |
void error(String message) { |
|
365 |
System.err.println("Error: " + message); |
|
366 |
errors++; |
|
367 |
} |
|
368 |
} |