|
23 | 23 |
|
24 | 24 | /* |
25 | 25 | * @test |
26 | | - * @bug 8310061 |
| 26 | + * @bug 8310061 8315534 |
27 | 27 | * @summary Verify a note is issued for implicit annotation processing |
28 | 28 | * |
29 | 29 | * @library /tools/lib /tools/javac/lib |
|
34 | 34 | * @run main TestNoteOnImplicitProcessing |
35 | 35 | */ |
36 | 36 |
|
37 | | -import java.io.RandomAccessFile; |
38 | | -import java.nio.ByteBuffer; |
39 | | -import java.nio.channels.FileChannel; |
40 | | -import java.nio.file.Files; |
| 37 | +import java.io.ByteArrayOutputStream; |
| 38 | +import java.io.PrintStream; |
| 39 | +import java.io.StringWriter; |
| 40 | +import java.net.URL; |
| 41 | +import java.net.URLClassLoader; |
| 42 | +import java.nio.charset.StandardCharsets; |
41 | 43 | import java.nio.file.Path; |
42 | 44 | import java.nio.file.Paths; |
43 | 45 | import java.util.List; |
44 | 46 |
|
45 | 47 | import javax.annotation.processing.Processor; |
| 48 | +import javax.tools.JavaCompiler; |
| 49 | +import javax.tools.JavaCompiler.CompilationTask; |
| 50 | +import javax.tools.JavaFileObject; |
| 51 | +import javax.tools.StandardJavaFileManager; |
| 52 | +import javax.tools.ToolProvider; |
46 | 53 |
|
47 | 54 | import toolbox.JavacTask; |
48 | 55 | import toolbox.Task; |
@@ -290,4 +297,89 @@ private void checkForCompilerNote(Task.Result javacResult, boolean expectedPrese |
290 | 297 | throw new RuntimeException("Expected note not printed"); |
291 | 298 | } |
292 | 299 | } |
| 300 | + |
| 301 | + @Test |
| 302 | + public void processorsViaAPI(Path base, Path jarFile) throws Exception { |
| 303 | + ClassLoader cl = new URLClassLoader(new URL[] {jarFile.toUri().toURL()}); |
| 304 | + Class<?> processorClass = Class.forName(processorName, true, cl); |
| 305 | + StringWriter compilerOut = new StringWriter(); |
| 306 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 307 | + JavaCompiler provider = ToolProvider.getSystemJavaCompiler(); |
| 308 | + PrintStream oldOut = System.out; |
| 309 | + |
| 310 | + try (StandardJavaFileManager jfm = provider.getStandardFileManager(null, null, null)) { |
| 311 | + System.setOut(new PrintStream(out, true, StandardCharsets.UTF_8)); |
| 312 | + Iterable<? extends JavaFileObject> inputFile = jfm.getJavaFileObjects("HelloWorldTest.java"); |
| 313 | + |
| 314 | + { |
| 315 | + List<String> options = List.of("-classpath", jarFile.toString(), "-XDrawDiagnostics"); |
| 316 | + CompilationTask task = provider.getTask(compilerOut, null, null, options, null, inputFile); |
| 317 | + |
| 318 | + task.call(); |
| 319 | + |
| 320 | + verifyMessages(out, compilerOut, true); |
| 321 | + } |
| 322 | + |
| 323 | + { |
| 324 | + List<String> options = List.of("-classpath", jarFile.toString(), "-XDrawDiagnostics"); |
| 325 | + CompilationTask task = provider.getTask(compilerOut, null, null, options, null, inputFile); |
| 326 | + Processor processor = |
| 327 | + (Processor) processorClass.getDeclaredConstructor().newInstance(); |
| 328 | + |
| 329 | + task.setProcessors(List.of(processor)); |
| 330 | + task.call(); |
| 331 | + |
| 332 | + verifyMessages(out, compilerOut, false); |
| 333 | + } |
| 334 | + |
| 335 | + { |
| 336 | + List<String> options = List.of("-classpath", jarFile.toString(), "-XDrawDiagnostics"); |
| 337 | + com.sun.source.util.JavacTask task = |
| 338 | + (com.sun.source.util.JavacTask) provider.getTask(compilerOut, null, null, options, null, inputFile); |
| 339 | + |
| 340 | + task.analyze(); |
| 341 | + |
| 342 | + verifyMessages(out, compilerOut, true); |
| 343 | + } |
| 344 | + |
| 345 | + { |
| 346 | + List<String> options = List.of("-classpath", jarFile.toString(), "-XDrawDiagnostics"); |
| 347 | + com.sun.source.util.JavacTask task = |
| 348 | + (com.sun.source.util.JavacTask) provider.getTask(compilerOut, null, null, options, null, inputFile); |
| 349 | + |
| 350 | + Processor processor = |
| 351 | + (Processor) processorClass.getDeclaredConstructor().newInstance(); |
| 352 | + |
| 353 | + task.setProcessors(List.of(processor)); |
| 354 | + task.analyze(); |
| 355 | + |
| 356 | + verifyMessages(out, compilerOut, false); |
| 357 | + } |
| 358 | + } finally { |
| 359 | + System.setOut(oldOut); |
| 360 | + } |
| 361 | + } |
| 362 | + |
| 363 | + private void verifyMessages(ByteArrayOutputStream out, StringWriter compilerOut, boolean expectedNotePresent) { |
| 364 | + if (!out.toString(StandardCharsets.UTF_8).contains("ImplicitProcTestProc run")) { |
| 365 | + throw new RuntimeException("Expected processor message not printed"); |
| 366 | + } |
| 367 | + |
| 368 | + out.reset(); |
| 369 | + |
| 370 | + boolean printed = compilerOut.toString().contains("- compiler.note.implicit.annotation.processing"); |
| 371 | + |
| 372 | + if (!expectedNotePresent && printed) { |
| 373 | + throw new RuntimeException("Unexpected note printed"); |
| 374 | + } |
| 375 | + |
| 376 | + if (expectedNotePresent && !printed) { |
| 377 | + throw new RuntimeException("Expected note not printed"); |
| 378 | + } |
| 379 | + |
| 380 | + StringBuffer compilerOutData = compilerOut.getBuffer(); |
| 381 | + |
| 382 | + compilerOutData.delete(0, compilerOutData.length()); |
| 383 | + } |
| 384 | + |
293 | 385 | } |
0 commit comments