8321073: Defer policy of disabling annotation processing by default

Reviewed-by: vromero, jlahoda, mcimadamore
This commit is contained in:
Joe Darcy 2023-12-06 18:36:26 +00:00
parent dc9c77bebe
commit 3edc24a71d
5 changed files with 35 additions and 43 deletions
src/jdk.compiler/share/classes/com/sun/tools/javac/main
test/langtools/tools/javac

@ -1144,18 +1144,21 @@ public class JavaCompiler {
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")) {
processAnnotations = false;
} else if (procEnvImpl == null) {
procEnvImpl = JavacProcessingEnvironment.instance(context);
procEnvImpl.setProcessors(processors);
// Process annotations if processing is requested and there
// is at least one Processor available.
processAnnotations = procEnvImpl.atLeastOneProcessor() &&
explicitAnnotationProcessingRequested();
processAnnotations = procEnvImpl.atLeastOneProcessor();
if (processAnnotations) {
if (!explicitAnnotationProcessingRequested() &&
!optionsCheckingInitiallyDisabled) {
log.note(Notes.ImplicitAnnotationProcessing);
}
options.put("parameters", "parameters");
reader.saveParameterNames = true;
keepComments = true;
@ -1164,9 +1167,9 @@ public class JavaCompiler {
taskListener.started(new TaskEvent(TaskEvent.Kind.ANNOTATION_PROCESSING));
deferredDiagnosticHandler = new Log.DeferredDiagnosticHandler(log);
procEnvImpl.getFiler().setInitialState(initialFiles, initialClassNames);
} else { // free resources
procEnvImpl.close();
}
} else { // free resources
procEnvImpl.close();
}
}

@ -67,6 +67,7 @@ public class T6341866 {
enum AnnoType {
NONE, // no annotation processing
SERVICE, // implicit annotation processing, via ServiceLoader
SPECIFY // explicit annotation processing
};
@ -98,14 +99,14 @@ public class T6341866 {
processorServices.delete();
List<String> opts = new ArrayList<String>();
opts.addAll(Arrays.asList("-d", ".",
"-sourcepath", testSrc,
"-classpath", testClasses,
"-proc:full"));
opts.addAll(Arrays.asList("-d", ".", "-sourcepath", testSrc, "-classpath", testClasses, "-Xlint:-options"));
if (implicitType.opt != null)
opts.add(implicitType.opt);
switch (annoType) {
case SERVICE:
createProcessorServices(Anno.class.getName());
break;
case SPECIFY:
opts.addAll(Arrays.asList("-processor", Anno.class.getName()));
break;
@ -144,6 +145,9 @@ public class T6341866 {
String expectKey = null;
if (implicitType == ImplicitType.OPT_UNSET) {
switch (annoType) {
case SERVICE:
expectKey = "compiler.warn.proc.use.proc.or.implicit";
break;
case SPECIFY:
expectKey = "compiler.warn.proc.use.implicit";
break;

@ -217,7 +217,3 @@ compiler.err.annotation.unrecognized.attribute.name
# this one is transitional (waiting for FFM API to exit preview)
compiler.warn.restricted.method
# Pending removal
compiler.note.implicit.annotation.processing
compiler.warn.proc.use.proc.or.implicit

@ -21,6 +21,8 @@
* questions.
*/
// key: compiler.warn.proc.use.proc.or.implicit
// key: compiler.note.implicit.annotation.processing
// options: -Xprefer:source
import p.SomeClass;

@ -23,8 +23,8 @@
/*
* @test
* @bug 8310061 8315534 8306819
* @summary Verify behavior around implicit annotation processing
* @bug 8310061 8315534
* @summary Verify a note is issued for implicit annotation processing
*
* @library /tools/lib /tools/javac/lib
* @modules
@ -59,27 +59,19 @@ import toolbox.ToolBox;
import toolbox.JarTask;
/*
* Does not generates a note and the processor does not run:
* Generates note and the processor runs:
* $ javac -cp ImplicitProcTestProc.jar HelloWorldTest.java
*
* Does _not_ generate a note and the processor does run:
* Does _not_ generate a note and the processor runs:
* $ javac -processorpath ImplicitProcTestProc.jar HelloWorldTest.java
* $ javac -cp ImplicitProcTestProc.jar -processor ImplicitProcTestProc.jar HelloWorldTest.java
* $ javac -cp ImplicitProcTestProc.jar -proc:full HelloWorldTest.java
* $ javac -cp ImplicitProcTestProc.jar -proc:only HelloWorldTest.java
*
* Does _not_ generate a note and the processor does _not_run:
* $ javac -cp ImplicitProcTestProc.jar -Xlint:-options HelloWorldTest.java
* $ javac -cp ImplicitProcTestProc.jar -Xlint:none HelloWorldTest.java
*
* Does _not_ generate a note and the processor _doesn't_ run.
* $ javac -cp ImplicitProcTestProc.jar -proc:none HelloWorldTest.java
*
* (Previously, annotation processing was implicitly enabled and the
* the class path was searched for processors. This test was
* originally written to probe around a note warning of a potential
* future policy change to disable such implicit processing, a policy
* change now implemented and this test has been updated accordingly.)
*/
public class TestNoteOnImplicitProcessing extends TestRunner {
@ -173,8 +165,8 @@ public class TestNoteOnImplicitProcessing extends TestRunner {
.run(Expect.SUCCESS)
.writeAll();
checkForProcessorMessage(javacResult, false);
checkForCompilerNote(javacResult, false);
checkForProcessorMessage(javacResult, true);
checkForCompilerNote(javacResult, true);
}
@Test
@ -247,7 +239,7 @@ public class TestNoteOnImplicitProcessing extends TestRunner {
.run(Expect.SUCCESS)
.writeAll();
checkForProcessorMessage(javacResult, false);
checkForProcessorMessage(javacResult, true);
checkForCompilerNote(javacResult, false);
}
@ -262,7 +254,7 @@ public class TestNoteOnImplicitProcessing extends TestRunner {
.run(Expect.SUCCESS)
.writeAll();
checkForProcessorMessage(javacResult, false);
checkForProcessorMessage(javacResult, true);
checkForCompilerNote(javacResult, false);
}
@ -325,7 +317,7 @@ public class TestNoteOnImplicitProcessing extends TestRunner {
task.call();
verifyMessages(out, compilerOut, false, false);
verifyMessages(out, compilerOut, true);
}
{
@ -337,7 +329,7 @@ public class TestNoteOnImplicitProcessing extends TestRunner {
task.setProcessors(List.of(processor));
task.call();
verifyMessages(out, compilerOut, false, true);
verifyMessages(out, compilerOut, false);
}
{
@ -347,7 +339,7 @@ public class TestNoteOnImplicitProcessing extends TestRunner {
task.analyze();
verifyMessages(out, compilerOut, false, false);
verifyMessages(out, compilerOut, true);
}
{
@ -361,21 +353,16 @@ public class TestNoteOnImplicitProcessing extends TestRunner {
task.setProcessors(List.of(processor));
task.analyze();
verifyMessages(out, compilerOut, false, true);
verifyMessages(out, compilerOut, false);
}
} finally {
System.setOut(oldOut);
}
}
private void verifyMessages(ByteArrayOutputStream out, StringWriter compilerOut, boolean expectedNotePresent,
boolean processorRunExpected) {
boolean processorRun = out.toString(StandardCharsets.UTF_8).contains("ImplicitProcTestProc run");
if (processorRun != processorRunExpected) {
throw new RuntimeException(processorRunExpected ?
"Expected processor message not printed" :
"Unexpected processor message printed");
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();