Skip to content

Commit dc4bc4f

Browse files
committed
8306819: Consider disabling the compiler's default active annotation processing
Reviewed-by: vromero
1 parent a4e9168 commit dc4bc4f

File tree

5 files changed

+46
-38
lines changed

5 files changed

+46
-38
lines changed

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

+7-10
Original file line numberDiff line numberDiff line change
@@ -1144,21 +1144,18 @@ public void initProcessAnnotations(Iterable<? extends Processor> processors,
11441144
if (processors != null && processors.iterator().hasNext())
11451145
explicitAnnotationProcessingRequested = true;
11461146

1147-
// Process annotations if processing is not disabled and there
1148-
// is at least one Processor available.
11491147
if (options.isSet(PROC, "none")) {
11501148
processAnnotations = false;
11511149
} else if (procEnvImpl == null) {
11521150
procEnvImpl = JavacProcessingEnvironment.instance(context);
11531151
procEnvImpl.setProcessors(processors);
1154-
processAnnotations = procEnvImpl.atLeastOneProcessor();
11551152

1156-
if (processAnnotations) {
1157-
if (!explicitAnnotationProcessingRequested() &&
1158-
!optionsCheckingInitiallyDisabled) {
1159-
log.note(Notes.ImplicitAnnotationProcessing);
1160-
}
1153+
// Process annotations if processing is requested and there
1154+
// is at least one Processor available.
1155+
processAnnotations = procEnvImpl.atLeastOneProcessor() &&
1156+
explicitAnnotationProcessingRequested();
11611157

1158+
if (processAnnotations) {
11621159
options.put("parameters", "parameters");
11631160
reader.saveParameterNames = true;
11641161
keepComments = true;
@@ -1167,9 +1164,9 @@ public void initProcessAnnotations(Iterable<? extends Processor> processors,
11671164
taskListener.started(new TaskEvent(TaskEvent.Kind.ANNOTATION_PROCESSING));
11681165
deferredDiagnosticHandler = new Log.DeferredDiagnosticHandler(log);
11691166
procEnvImpl.getFiler().setInitialState(initialFiles, initialClassNames);
1170-
} else { // free resources
1171-
procEnvImpl.close();
11721167
}
1168+
} else { // free resources
1169+
procEnvImpl.close();
11731170
}
11741171
}
11751172

test/langtools/tools/javac/6341866/T6341866.java

+7-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2006, 2015, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2006, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -56,8 +56,8 @@ public class T6341866 {
5656
enum ImplicitType {
5757
NONE(null), // don't use implicit compilation
5858
OPT_UNSET(null), // implicit compilation, but no -implicit option
59-
OPT_NONE("-implicit:none"), // implicit compilation wiith -implicit:none
60-
OPT_CLASS("-implicit:class"); // implicit compilation wiith -implicit:class
59+
OPT_NONE("-implicit:none"), // implicit compilation with -implicit:none
60+
OPT_CLASS("-implicit:class"); // implicit compilation with -implicit:class
6161

6262
ImplicitType(String opt) {
6363
this.opt = opt;
@@ -67,7 +67,6 @@ enum ImplicitType {
6767

6868
enum AnnoType {
6969
NONE, // no annotation processing
70-
SERVICE, // implicit annotation processing, via ServiceLoader
7170
SPECIFY // explicit annotation processing
7271
};
7372

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

101100
List<String> opts = new ArrayList<String>();
102-
opts.addAll(Arrays.asList("-d", ".", "-sourcepath", testSrc, "-classpath", testClasses, "-Xlint:-options"));
101+
opts.addAll(Arrays.asList("-d", ".",
102+
"-sourcepath", testSrc,
103+
"-classpath", testClasses,
104+
"-proc:full"));
103105
if (implicitType.opt != null)
104106
opts.add(implicitType.opt);
105107

106108
switch (annoType) {
107-
case SERVICE:
108-
createProcessorServices(Anno.class.getName());
109-
break;
110109
case SPECIFY:
111110
opts.addAll(Arrays.asList("-processor", Anno.class.getName()));
112111
break;
@@ -145,9 +144,6 @@ static boolean test(ImplicitType implicitType, AnnoType annoType) throws IOExcep
145144
String expectKey = null;
146145
if (implicitType == ImplicitType.OPT_UNSET) {
147146
switch (annoType) {
148-
case SERVICE:
149-
expectKey = "compiler.warn.proc.use.proc.or.implicit";
150-
break;
151147
case SPECIFY:
152148
expectKey = "compiler.warn.proc.use.implicit";
153149
break;

test/langtools/tools/javac/diags/examples.not-yet.txt

+4
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,7 @@ compiler.err.annotation.unrecognized.attribute.name
218218

219219
# this one is transitional (waiting for FFM API to exit preview)
220220
compiler.warn.restricted.method
221+
222+
# Pending removal
223+
compiler.note.implicit.annotation.processing
224+
compiler.warn.proc.use.proc.or.implicit

test/langtools/tools/javac/diags/examples/ProcUseProcOrImplicit/ProcUseProcOrImplicit.java

-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
* questions.
2222
*/
2323

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

2826
import p.SomeClass;

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

+28-15
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323

2424
/*
2525
* @test
26-
* @bug 8310061 8315534
27-
* @summary Verify a note is issued for implicit annotation processing
26+
* @bug 8310061 8315534 8306819
27+
* @summary Verify behavior around implicit annotation processing
2828
*
2929
* @library /tools/lib /tools/javac/lib
3030
* @modules
@@ -59,19 +59,27 @@
5959
import toolbox.JarTask;
6060

6161
/*
62-
* Generates note and the processor runs:
62+
* Does not generates a note and the processor does not run:
6363
* $ javac -cp ImplicitProcTestProc.jar HelloWorldTest.java
6464
*
65-
* Does _not_ generate a note and the processor runs:
65+
* Does _not_ generate a note and the processor does run:
6666
* $ javac -processorpath ImplicitProcTestProc.jar HelloWorldTest.java
6767
* $ javac -cp ImplicitProcTestProc.jar -processor ImplicitProcTestProc.jar HelloWorldTest.java
6868
* $ javac -cp ImplicitProcTestProc.jar -proc:full HelloWorldTest.java
6969
* $ javac -cp ImplicitProcTestProc.jar -proc:only HelloWorldTest.java
70+
*
71+
* Does _not_ generate a note and the processor does _not_run:
7072
* $ javac -cp ImplicitProcTestProc.jar -Xlint:-options HelloWorldTest.java
7173
* $ javac -cp ImplicitProcTestProc.jar -Xlint:none HelloWorldTest.java
7274
*
7375
* Does _not_ generate a note and the processor _doesn't_ run.
7476
* $ javac -cp ImplicitProcTestProc.jar -proc:none HelloWorldTest.java
77+
*
78+
* (Previously, annotation processing was implicitly enabled and the
79+
* the class path was searched for processors. This test was
80+
* originally written to probe around a note warning of a potential
81+
* future policy change to disable such implicit processing, a policy
82+
* change now implemented and this test has been updated accordingly.)
7583
*/
7684

7785
public class TestNoteOnImplicitProcessing extends TestRunner {
@@ -165,8 +173,8 @@ public void generateWarning(Path base, Path jarFile) {
165173
.run(Expect.SUCCESS)
166174
.writeAll();
167175

168-
checkForProcessorMessage(javacResult, true);
169-
checkForCompilerNote(javacResult, true);
176+
checkForProcessorMessage(javacResult, false);
177+
checkForCompilerNote(javacResult, false);
170178
}
171179

172180
@Test
@@ -239,7 +247,7 @@ public void lintOptions(Path base, Path jarFile) {
239247
.run(Expect.SUCCESS)
240248
.writeAll();
241249

242-
checkForProcessorMessage(javacResult, true);
250+
checkForProcessorMessage(javacResult, false);
243251
checkForCompilerNote(javacResult, false);
244252
}
245253

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

257-
checkForProcessorMessage(javacResult, true);
265+
checkForProcessorMessage(javacResult, false);
258266
checkForCompilerNote(javacResult, false);
259267
}
260268

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

318326
task.call();
319327

320-
verifyMessages(out, compilerOut, true);
328+
verifyMessages(out, compilerOut, false, false);
321329
}
322330

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

332-
verifyMessages(out, compilerOut, false);
340+
verifyMessages(out, compilerOut, false, true);
333341
}
334342

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

340348
task.analyze();
341349

342-
verifyMessages(out, compilerOut, true);
350+
verifyMessages(out, compilerOut, false, false);
343351
}
344352

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

356-
verifyMessages(out, compilerOut, false);
364+
verifyMessages(out, compilerOut, false, true);
357365
}
358366
} finally {
359367
System.setOut(oldOut);
360368
}
361369
}
362370

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");
371+
private void verifyMessages(ByteArrayOutputStream out, StringWriter compilerOut, boolean expectedNotePresent,
372+
boolean processorRunExpected) {
373+
boolean processorRun = out.toString(StandardCharsets.UTF_8).contains("ImplicitProcTestProc run");
374+
375+
if (processorRun != processorRunExpected) {
376+
throw new RuntimeException(processorRunExpected ?
377+
"Expected processor message not printed" :
378+
"Unexpected processor message printed");
366379
}
367380

368381
out.reset();

0 commit comments

Comments
 (0)