8306819: Consider disabling the compiler's default active annotation processing

Reviewed-by: vromero
This commit is contained in:
Joe Darcy 2023-10-06 21:45:58 +00:00
parent a4e9168bab
commit dc4bc4f084
5 changed files with 46 additions and 38 deletions

View File

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

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2006, 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2006, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -56,8 +56,8 @@ public class T6341866 {
enum ImplicitType { enum ImplicitType {
NONE(null), // don't use implicit compilation NONE(null), // don't use implicit compilation
OPT_UNSET(null), // implicit compilation, but no -implicit option OPT_UNSET(null), // implicit compilation, but no -implicit option
OPT_NONE("-implicit:none"), // implicit compilation wiith -implicit:none OPT_NONE("-implicit:none"), // implicit compilation with -implicit:none
OPT_CLASS("-implicit:class"); // implicit compilation wiith -implicit:class OPT_CLASS("-implicit:class"); // implicit compilation with -implicit:class
ImplicitType(String opt) { ImplicitType(String opt) {
this.opt = opt; this.opt = opt;
@ -67,7 +67,6 @@ public class T6341866 {
enum AnnoType { enum AnnoType {
NONE, // no annotation processing NONE, // no annotation processing
SERVICE, // implicit annotation processing, via ServiceLoader
SPECIFY // explicit annotation processing SPECIFY // explicit annotation processing
}; };
@ -99,14 +98,14 @@ public class T6341866 {
processorServices.delete(); processorServices.delete();
List<String> opts = new ArrayList<String>(); List<String> opts = new ArrayList<String>();
opts.addAll(Arrays.asList("-d", ".", "-sourcepath", testSrc, "-classpath", testClasses, "-Xlint:-options")); opts.addAll(Arrays.asList("-d", ".",
"-sourcepath", testSrc,
"-classpath", testClasses,
"-proc:full"));
if (implicitType.opt != null) if (implicitType.opt != null)
opts.add(implicitType.opt); opts.add(implicitType.opt);
switch (annoType) { switch (annoType) {
case SERVICE:
createProcessorServices(Anno.class.getName());
break;
case SPECIFY: case SPECIFY:
opts.addAll(Arrays.asList("-processor", Anno.class.getName())); opts.addAll(Arrays.asList("-processor", Anno.class.getName()));
break; break;
@ -145,9 +144,6 @@ public class T6341866 {
String expectKey = null; String expectKey = null;
if (implicitType == ImplicitType.OPT_UNSET) { if (implicitType == ImplicitType.OPT_UNSET) {
switch (annoType) { switch (annoType) {
case SERVICE:
expectKey = "compiler.warn.proc.use.proc.or.implicit";
break;
case SPECIFY: case SPECIFY:
expectKey = "compiler.warn.proc.use.implicit"; expectKey = "compiler.warn.proc.use.implicit";
break; break;

View File

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

View File

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

View File

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