Skip to content

Commit 94a74a0

Browse files
committed
8315534: Incorrect warnings about implicit annotation processing
Reviewed-by: darcy
1 parent 84425a6 commit 94a74a0

File tree

2 files changed

+100
-7
lines changed

2 files changed

+100
-7
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/main/JavaCompiler.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -915,8 +915,6 @@ public void compile(Collection<JavaFileObject> sourceFileObjects,
915915
taskListener.started(new TaskEvent(TaskEvent.Kind.COMPILATION));
916916
}
917917

918-
if (processors != null && processors.iterator().hasNext())
919-
explicitAnnotationProcessingRequested = true;
920918
// as a JavaCompiler can only be used once, throw an exception if
921919
// it has been used before.
922920
if (hasBeenUsed)
@@ -1143,6 +1141,9 @@ public List<JCCompilationUnit> enterTrees(List<JCCompilationUnit> roots) {
11431141
public void initProcessAnnotations(Iterable<? extends Processor> processors,
11441142
Collection<? extends JavaFileObject> initialFiles,
11451143
Collection<String> initialClassNames) {
1144+
if (processors != null && processors.iterator().hasNext())
1145+
explicitAnnotationProcessingRequested = true;
1146+
11461147
// Process annotations if processing is not disabled and there
11471148
// is at least one Processor available.
11481149
if (options.isSet(PROC, "none")) {

test/langtools/tools/javac/processing/options/TestNoteOnImplicitProcessing.java

Lines changed: 97 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
/*
2525
* @test
26-
* @bug 8310061
26+
* @bug 8310061 8315534
2727
* @summary Verify a note is issued for implicit annotation processing
2828
*
2929
* @library /tools/lib /tools/javac/lib
@@ -34,15 +34,22 @@
3434
* @run main TestNoteOnImplicitProcessing
3535
*/
3636

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;
4143
import java.nio.file.Path;
4244
import java.nio.file.Paths;
4345
import java.util.List;
4446

4547
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;
4653

4754
import toolbox.JavacTask;
4855
import toolbox.Task;
@@ -290,4 +297,89 @@ private void checkForCompilerNote(Task.Result javacResult, boolean expectedPrese
290297
throw new RuntimeException("Expected note not printed");
291298
}
292299
}
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+
293385
}

0 commit comments

Comments
 (0)