From 94a74a0a450cad6080302bc1db15a1a564940bdd Mon Sep 17 00:00:00 2001 From: Jan Lahoda Date: Mon, 4 Sep 2023 10:06:20 +0000 Subject: [PATCH] 8315534: Incorrect warnings about implicit annotation processing Reviewed-by: darcy --- .../sun/tools/javac/main/JavaCompiler.java | 5 +- .../options/TestNoteOnImplicitProcessing.java | 102 +++++++++++++++++- 2 files changed, 100 insertions(+), 7 deletions(-) diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/main/JavaCompiler.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/main/JavaCompiler.java index b0ba6c81020..10f4d609c68 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/main/JavaCompiler.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/main/JavaCompiler.java @@ -915,8 +915,6 @@ public class JavaCompiler { taskListener.started(new TaskEvent(TaskEvent.Kind.COMPILATION)); } - if (processors != null && processors.iterator().hasNext()) - explicitAnnotationProcessingRequested = true; // as a JavaCompiler can only be used once, throw an exception if // it has been used before. if (hasBeenUsed) @@ -1143,6 +1141,9 @@ public class JavaCompiler { public void initProcessAnnotations(Iterable processors, Collection initialFiles, Collection initialClassNames) { + if (processors != null && processors.iterator().hasNext()) + explicitAnnotationProcessingRequested = true; + // Process annotations if processing is not disabled and there // is at least one Processor available. if (options.isSet(PROC, "none")) { diff --git a/test/langtools/tools/javac/processing/options/TestNoteOnImplicitProcessing.java b/test/langtools/tools/javac/processing/options/TestNoteOnImplicitProcessing.java index 4daeb9e6260..81efbce05aa 100644 --- a/test/langtools/tools/javac/processing/options/TestNoteOnImplicitProcessing.java +++ b/test/langtools/tools/javac/processing/options/TestNoteOnImplicitProcessing.java @@ -23,7 +23,7 @@ /* * @test - * @bug 8310061 + * @bug 8310061 8315534 * @summary Verify a note is issued for implicit annotation processing * * @library /tools/lib /tools/javac/lib @@ -34,15 +34,22 @@ * @run main TestNoteOnImplicitProcessing */ -import java.io.RandomAccessFile; -import java.nio.ByteBuffer; -import java.nio.channels.FileChannel; -import java.nio.file.Files; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.io.StringWriter; +import java.net.URL; +import java.net.URLClassLoader; +import java.nio.charset.StandardCharsets; import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; import javax.annotation.processing.Processor; +import javax.tools.JavaCompiler; +import javax.tools.JavaCompiler.CompilationTask; +import javax.tools.JavaFileObject; +import javax.tools.StandardJavaFileManager; +import javax.tools.ToolProvider; import toolbox.JavacTask; import toolbox.Task; @@ -290,4 +297,89 @@ public class TestNoteOnImplicitProcessing extends TestRunner { throw new RuntimeException("Expected note not printed"); } } + + @Test + public void processorsViaAPI(Path base, Path jarFile) throws Exception { + ClassLoader cl = new URLClassLoader(new URL[] {jarFile.toUri().toURL()}); + Class processorClass = Class.forName(processorName, true, cl); + StringWriter compilerOut = new StringWriter(); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + JavaCompiler provider = ToolProvider.getSystemJavaCompiler(); + PrintStream oldOut = System.out; + + try (StandardJavaFileManager jfm = provider.getStandardFileManager(null, null, null)) { + System.setOut(new PrintStream(out, true, StandardCharsets.UTF_8)); + Iterable inputFile = jfm.getJavaFileObjects("HelloWorldTest.java"); + + { + List options = List.of("-classpath", jarFile.toString(), "-XDrawDiagnostics"); + CompilationTask task = provider.getTask(compilerOut, null, null, options, null, inputFile); + + task.call(); + + verifyMessages(out, compilerOut, true); + } + + { + List options = List.of("-classpath", jarFile.toString(), "-XDrawDiagnostics"); + CompilationTask task = provider.getTask(compilerOut, null, null, options, null, inputFile); + Processor processor = + (Processor) processorClass.getDeclaredConstructor().newInstance(); + + task.setProcessors(List.of(processor)); + task.call(); + + verifyMessages(out, compilerOut, false); + } + + { + List options = List.of("-classpath", jarFile.toString(), "-XDrawDiagnostics"); + com.sun.source.util.JavacTask task = + (com.sun.source.util.JavacTask) provider.getTask(compilerOut, null, null, options, null, inputFile); + + task.analyze(); + + verifyMessages(out, compilerOut, true); + } + + { + List options = List.of("-classpath", jarFile.toString(), "-XDrawDiagnostics"); + com.sun.source.util.JavacTask task = + (com.sun.source.util.JavacTask) provider.getTask(compilerOut, null, null, options, null, inputFile); + + Processor processor = + (Processor) processorClass.getDeclaredConstructor().newInstance(); + + task.setProcessors(List.of(processor)); + task.analyze(); + + verifyMessages(out, compilerOut, false); + } + } finally { + System.setOut(oldOut); + } + } + + private void verifyMessages(ByteArrayOutputStream out, StringWriter compilerOut, boolean expectedNotePresent) { + if (!out.toString(StandardCharsets.UTF_8).contains("ImplicitProcTestProc run")) { + throw new RuntimeException("Expected processor message not printed"); + } + + out.reset(); + + boolean printed = compilerOut.toString().contains("- compiler.note.implicit.annotation.processing"); + + if (!expectedNotePresent && printed) { + throw new RuntimeException("Unexpected note printed"); + } + + if (expectedNotePresent && !printed) { + throw new RuntimeException("Expected note not printed"); + } + + StringBuffer compilerOutData = compilerOut.getBuffer(); + + compilerOutData.delete(0, compilerOutData.length()); + } + }