Skip to content

Commit fa5ec62

Browse files
dbalekarchiecobbs
authored andcommitted
8378950: Repeated warnings when annotation processing is happening
Co-authored-by: Archie Cobbs <acobbs@openjdk.org> Reviewed-by: jlahoda
1 parent c18e3a3 commit fa5ec62

File tree

4 files changed

+103
-13
lines changed

4 files changed

+103
-13
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2025, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2026, 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
@@ -1104,7 +1104,7 @@ boolean unrecoverableError() {
11041104
return true;
11051105

11061106
return deferredDiagnosticHandler.getDiagnostics().stream()
1107-
.anyMatch(d -> (d.getKind() == Diagnostic.Kind.WARNING && werror) ||
1107+
.anyMatch(d -> (d.getKind() == Diagnostic.Kind.WARNING && werror && ACCEPT_NON_RECOVERABLE_LINTS.test(d)) ||
11081108
(d.getKind() == Diagnostic.Kind.ERROR && (fatalErrors || !d.isFlagSet(RECOVERABLE))));
11091109
}
11101110

@@ -1195,12 +1195,19 @@ void run(boolean lastRound, boolean errorStatus) {
11951195
}
11961196

11971197
void showDiagnostics(boolean showAll) {
1198-
deferredDiagnosticHandler.reportDeferredDiagnostics(showAll ? ACCEPT_ALL
1199-
: ACCEPT_NON_RECOVERABLE);
1198+
deferredDiagnosticHandler.reportDeferredDiagnostics(
1199+
ACCEPT_NON_RECOVERABLE_LINTS.and(showAll ? ACCEPT_ALL
1200+
: ACCEPT_NON_RECOVERABLE));
12001201
log.popDiagnosticHandler(deferredDiagnosticHandler);
12011202
compiler.setDeferredDiagnosticHandler(null);
12021203
}
12031204
//where:
1205+
private final Predicate<JCDiagnostic> ACCEPT_NON_RECOVERABLE_LINTS =
1206+
d -> !Optional.of(d)
1207+
.map(JCDiagnostic::getLintCategory)
1208+
.map(lc -> lc.annotationSuppression ||
1209+
lc == Lint.LintCategory.INCUBATING)
1210+
.orElse(false);
12041211
private final Predicate<JCDiagnostic> ACCEPT_NON_RECOVERABLE =
12051212
d -> d.getKind() != JCDiagnostic.Kind.ERROR ||
12061213
!d.isFlagSet(DiagnosticFlag.RECOVERABLE) ||

test/langtools/tools/javac/implicitCompile/APImplicitClassesWarnings.java

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

2424
/*
2525
* @test
26-
* @bug 8378740
26+
* @bug 8378740 8378950
2727
* @summary Verify warnings are properly suppressed for the combination of
2828
* annotation processing and implicit compilation
2929
* @library /tools/lib
@@ -96,9 +96,7 @@ public interface Implicit {}
9696

9797
List<String> expected = List.of(
9898
"Use.java:4:5: compiler.warn.has.been.deprecated.for.removal: test.Depr, test",
99-
"Use.java:4:5: compiler.warn.has.been.deprecated.for.removal: test.Depr, test",
100-
"Use.java:4:5: compiler.warn.has.been.deprecated.for.removal: test.Depr, test",
101-
"3 warnings"
99+
"1 warning"
102100
);
103101

104102
tb.checkEqual(expected, log);
@@ -143,6 +141,53 @@ public interface Implicit {}
143141
.writeAll();
144142
}
145143

144+
@Test
145+
public void testCorrectImport() throws Exception {
146+
Path src = base.resolve("src");
147+
Path classes = base.resolve("classes");
148+
tb.writeJavaFiles(src,
149+
"""
150+
package test;
151+
152+
@Deprecated(forRemoval=true)
153+
public class Depr {
154+
public static class Nested {}
155+
}
156+
""",
157+
"""
158+
package test;
159+
import test.Depr.Nested;
160+
public class Use {
161+
Implicit implicit;
162+
Nested nest;
163+
}
164+
""",
165+
"""
166+
package test;
167+
public interface Implicit {}
168+
""");
169+
Files.createDirectories(classes);
170+
171+
List<String> log = new JavacTask(tb)
172+
.options("-d", classes.toString(),
173+
"-XDrawDiagnostics",
174+
"-implicit:class",
175+
"-sourcepath", src.toString())
176+
.files(src.resolve("test").resolve("Depr.java"),
177+
src.resolve("test").resolve("Use.java"))
178+
.processors(new ProcessorImpl())
179+
.run()
180+
.writeAll()
181+
.getOutputLines(Task.OutputKind.DIRECT);
182+
183+
List<String> expected = List.of(
184+
"Use.java:2:12: compiler.warn.has.been.deprecated.for.removal: test.Depr, test",
185+
"1 warning"
186+
);
187+
188+
tb.checkEqual(expected, log);
189+
}
190+
146191
@SupportedAnnotationTypes("*")
147192
private static class ProcessorImpl extends AbstractProcessor {
148193
@Override

test/langtools/tools/javac/modules/IncubatingTest.java

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2026, 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
@@ -23,7 +23,7 @@
2323

2424
/*
2525
* @test
26-
* @bug 8171177 8187591
26+
* @bug 8171177 8187591 8378950
2727
* @summary Verify that ModuleResolution attribute flags are honored.
2828
* @library /tools/lib
2929
* @modules jdk.compiler/com.sun.tools.javac.api
@@ -46,10 +46,15 @@
4646
import java.util.HashMap;
4747
import java.util.List;
4848
import java.util.Map;
49-
49+
import java.util.Set;
5050
import java.lang.classfile.*;
5151
import java.lang.classfile.attribute.ModuleResolutionAttribute;
5252
import java.lang.classfile.constantpool.*;
53+
import javax.annotation.processing.AbstractProcessor;
54+
import javax.annotation.processing.RoundEnvironment;
55+
import javax.annotation.processing.SupportedAnnotationTypes;
56+
import javax.lang.model.SourceVersion;
57+
import javax.lang.model.element.TypeElement;
5358
import toolbox.JavacTask;
5459
import toolbox.Task;
5560
import toolbox.Task.Expect;
@@ -242,6 +247,29 @@ public void testIncubating(Path base) throws Exception {
242247
.outdir(testModuleClasses)
243248
.files(findJavaFiles(testModuleSrc))
244249
.run(Expect.SUCCESS);
250+
251+
//test with annotation processing
252+
log = new JavacTask(tb)
253+
.options("--module-path", classes.toString(),
254+
"-XDrawDiagnostics",
255+
"-Werror")
256+
.outdir(testModuleClasses)
257+
.files(findJavaFiles(testModuleSrc))
258+
.processors(new ProcessorImpl())
259+
.run(Expect.FAIL)
260+
.writeAll()
261+
.getOutputLines(Task.OutputKind.DIRECT);
262+
263+
expected = Arrays.asList(
264+
"- compiler.warn.incubating.modules: jdk.i",
265+
"- compiler.err.warnings.and.werror",
266+
"1 error",
267+
"1 warning"
268+
);
269+
270+
if (!expected.equals(log)) {
271+
throw new AssertionError("Unexpected output: " + log);
272+
}
245273
}
246274

247275
private void copyJavaBase(Path targetDir) throws IOException {
@@ -270,4 +298,16 @@ private void addModuleResolutionAttribute(Path classfile, int resolution_flags)
270298
out.write(newBytes);
271299
}
272300
}
301+
302+
@SupportedAnnotationTypes("*")
303+
private static class ProcessorImpl extends AbstractProcessor {
304+
@Override
305+
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
306+
return false;
307+
}
308+
@Override
309+
public SourceVersion getSupportedSourceVersion() {
310+
return SourceVersion.latest();
311+
}
312+
}
273313
}

test/langtools/tools/javac/processing/rounds/OverwriteBetweenCompilations_2.out

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,3 @@ public abstract class GeneratedClass<E extends java.lang.Number> extends java.ut
5353

5454
public void test(long a);
5555
}
56-
- compiler.note.deprecated.filename: OverwriteBetweenCompilationsSource.java
57-
- compiler.note.deprecated.recompile

0 commit comments

Comments
 (0)