Skip to content

Commit

Permalink
8321314: Reinstate disabling the compiler's default active annotation…
Browse files Browse the repository at this point in the history
… processing

Reviewed-by: vromero
  • Loading branch information
jddarcy committed May 30, 2024
1 parent ec88c6a commit 79f4998
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1142,21 +1142,18 @@ public void initProcessAnnotations(Iterable<? extends Processor> processors,
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);
processAnnotations = procEnvImpl.atLeastOneProcessor();

if (processAnnotations) {
if (!explicitAnnotationProcessingRequested() &&
!optionsCheckingInitiallyDisabled) {
log.note(Notes.ImplicitAnnotationProcessing);
}
// Process annotations if processing is requested and there
// is at least one Processor available.
processAnnotations = procEnvImpl.atLeastOneProcessor() &&
explicitAnnotationProcessingRequested();

if (processAnnotations) {
options.put("parameters", "parameters");
reader.saveParameterNames = true;
keepComments = true;
Expand All @@ -1165,9 +1162,9 @@ public void initProcessAnnotations(Iterable<? extends Processor> processors,
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();
}
}

Expand Down
12 changes: 4 additions & 8 deletions test/langtools/tools/javac/6341866/T6341866.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ enum ImplicitType {

enum AnnoType {
NONE, // no annotation processing
SERVICE, // implicit annotation processing, via ServiceLoader
SPECIFY // explicit annotation processing
};

Expand Down Expand Up @@ -99,14 +98,14 @@ static boolean test(ImplicitType implicitType, AnnoType annoType) throws IOExcep
processorServices.delete();

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)
opts.add(implicitType.opt);

switch (annoType) {
case SERVICE:
createProcessorServices(Anno.class.getName());
break;
case SPECIFY:
opts.addAll(Arrays.asList("-processor", Anno.class.getName()));
break;
Expand Down Expand Up @@ -145,9 +144,6 @@ static boolean test(ImplicitType implicitType, AnnoType annoType) throws IOExcep
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;
Expand Down
4 changes: 4 additions & 0 deletions test/langtools/tools/javac/diags/examples.not-yet.txt
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,7 @@ 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
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
* questions.
*/

// key: compiler.warn.proc.use.proc.or.implicit
// key: compiler.note.implicit.annotation.processing
// options: -Xprefer:source

import p.SomeClass;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@

/*
* @test
* @bug 8310061 8315534
* @summary Verify a note is issued for implicit annotation processing
* @bug 8310061 8315534 8306819
* @summary Verify behavior around implicit annotation processing
*
* @library /tools/lib /tools/javac/lib
* @modules
Expand Down Expand Up @@ -59,19 +59,27 @@
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
*
* 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 -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 {
Expand Down Expand Up @@ -165,8 +173,8 @@ public void generateWarning(Path base, Path jarFile) {
.run(Expect.SUCCESS)
.writeAll();

checkForProcessorMessage(javacResult, true);
checkForCompilerNote(javacResult, true);
checkForProcessorMessage(javacResult, false);
checkForCompilerNote(javacResult, false);
}

@Test
Expand Down Expand Up @@ -239,7 +247,7 @@ public void lintOptions(Path base, Path jarFile) {
.run(Expect.SUCCESS)
.writeAll();

checkForProcessorMessage(javacResult, true);
checkForProcessorMessage(javacResult, false);
checkForCompilerNote(javacResult, false);
}

Expand All @@ -254,7 +262,7 @@ public void lintNone(Path base, Path jarFile) {
.run(Expect.SUCCESS)
.writeAll();

checkForProcessorMessage(javacResult, true);
checkForProcessorMessage(javacResult, false);
checkForCompilerNote(javacResult, false);
}

Expand Down Expand Up @@ -317,7 +325,7 @@ public void processorsViaAPI(Path base, Path jarFile) throws Exception {

task.call();

verifyMessages(out, compilerOut, true);
verifyMessages(out, compilerOut, false, false);
}

{
Expand All @@ -329,7 +337,7 @@ public void processorsViaAPI(Path base, Path jarFile) throws Exception {
task.setProcessors(List.of(processor));
task.call();

verifyMessages(out, compilerOut, false);
verifyMessages(out, compilerOut, false, true);
}

{
Expand All @@ -339,7 +347,7 @@ public void processorsViaAPI(Path base, Path jarFile) throws Exception {

task.analyze();

verifyMessages(out, compilerOut, true);
verifyMessages(out, compilerOut, false, false);
}

{
Expand All @@ -353,16 +361,21 @@ public void processorsViaAPI(Path base, Path jarFile) throws Exception {
task.setProcessors(List.of(processor));
task.analyze();

verifyMessages(out, compilerOut, false);
verifyMessages(out, compilerOut, false, true);
}
} 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");
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");
}

out.reset();
Expand Down

1 comment on commit 79f4998

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.