From fa5ec62405af5ca104ca269d3470e07de690386f Mon Sep 17 00:00:00 2001 From: Dusan Balek Date: Thu, 2 Apr 2026 08:20:47 +0000 Subject: [PATCH] 8378950: Repeated warnings when annotation processing is happening Co-authored-by: Archie Cobbs Reviewed-by: jlahoda --- .../JavacProcessingEnvironment.java | 15 ++++-- .../APImplicitClassesWarnings.java | 53 +++++++++++++++++-- .../tools/javac/modules/IncubatingTest.java | 46 ++++++++++++++-- .../rounds/OverwriteBetweenCompilations_2.out | 2 - 4 files changed, 103 insertions(+), 13 deletions(-) diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java index 74d082d4b64..809c19d5012 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -1104,7 +1104,7 @@ public class JavacProcessingEnvironment implements ProcessingEnvironment, Closea return true; return deferredDiagnosticHandler.getDiagnostics().stream() - .anyMatch(d -> (d.getKind() == Diagnostic.Kind.WARNING && werror) || + .anyMatch(d -> (d.getKind() == Diagnostic.Kind.WARNING && werror && ACCEPT_NON_RECOVERABLE_LINTS.test(d)) || (d.getKind() == Diagnostic.Kind.ERROR && (fatalErrors || !d.isFlagSet(RECOVERABLE)))); } @@ -1195,12 +1195,19 @@ public class JavacProcessingEnvironment implements ProcessingEnvironment, Closea } void showDiagnostics(boolean showAll) { - deferredDiagnosticHandler.reportDeferredDiagnostics(showAll ? ACCEPT_ALL - : ACCEPT_NON_RECOVERABLE); + deferredDiagnosticHandler.reportDeferredDiagnostics( + ACCEPT_NON_RECOVERABLE_LINTS.and(showAll ? ACCEPT_ALL + : ACCEPT_NON_RECOVERABLE)); log.popDiagnosticHandler(deferredDiagnosticHandler); compiler.setDeferredDiagnosticHandler(null); } //where: + private final Predicate ACCEPT_NON_RECOVERABLE_LINTS = + d -> !Optional.of(d) + .map(JCDiagnostic::getLintCategory) + .map(lc -> lc.annotationSuppression || + lc == Lint.LintCategory.INCUBATING) + .orElse(false); private final Predicate ACCEPT_NON_RECOVERABLE = d -> d.getKind() != JCDiagnostic.Kind.ERROR || !d.isFlagSet(DiagnosticFlag.RECOVERABLE) || diff --git a/test/langtools/tools/javac/implicitCompile/APImplicitClassesWarnings.java b/test/langtools/tools/javac/implicitCompile/APImplicitClassesWarnings.java index dc60c6cc78a..8c1e356217c 100644 --- a/test/langtools/tools/javac/implicitCompile/APImplicitClassesWarnings.java +++ b/test/langtools/tools/javac/implicitCompile/APImplicitClassesWarnings.java @@ -23,7 +23,7 @@ /* * @test - * @bug 8378740 + * @bug 8378740 8378950 * @summary Verify warnings are properly suppressed for the combination of * annotation processing and implicit compilation * @library /tools/lib @@ -96,9 +96,7 @@ public class APImplicitClassesWarnings { List expected = List.of( "Use.java:4:5: compiler.warn.has.been.deprecated.for.removal: test.Depr, test", - "Use.java:4:5: compiler.warn.has.been.deprecated.for.removal: test.Depr, test", - "Use.java:4:5: compiler.warn.has.been.deprecated.for.removal: test.Depr, test", - "3 warnings" + "1 warning" ); tb.checkEqual(expected, log); @@ -143,6 +141,53 @@ public class APImplicitClassesWarnings { .writeAll(); } + @Test + public void testCorrectImport() throws Exception { + Path src = base.resolve("src"); + Path classes = base.resolve("classes"); + tb.writeJavaFiles(src, + """ + package test; + + @Deprecated(forRemoval=true) + public class Depr { + public static class Nested {} + } + """, + """ + package test; + import test.Depr.Nested; + public class Use { + Implicit implicit; + Nested nest; + } + """, + """ + package test; + public interface Implicit {} + """); + Files.createDirectories(classes); + + List log = new JavacTask(tb) + .options("-d", classes.toString(), + "-XDrawDiagnostics", + "-implicit:class", + "-sourcepath", src.toString()) + .files(src.resolve("test").resolve("Depr.java"), + src.resolve("test").resolve("Use.java")) + .processors(new ProcessorImpl()) + .run() + .writeAll() + .getOutputLines(Task.OutputKind.DIRECT); + + List expected = List.of( + "Use.java:2:12: compiler.warn.has.been.deprecated.for.removal: test.Depr, test", + "1 warning" + ); + + tb.checkEqual(expected, log); + } + @SupportedAnnotationTypes("*") private static class ProcessorImpl extends AbstractProcessor { @Override diff --git a/test/langtools/tools/javac/modules/IncubatingTest.java b/test/langtools/tools/javac/modules/IncubatingTest.java index 68f615abc04..6ff1a0a29c8 100644 --- a/test/langtools/tools/javac/modules/IncubatingTest.java +++ b/test/langtools/tools/javac/modules/IncubatingTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -23,7 +23,7 @@ /* * @test - * @bug 8171177 8187591 + * @bug 8171177 8187591 8378950 * @summary Verify that ModuleResolution attribute flags are honored. * @library /tools/lib * @modules jdk.compiler/com.sun.tools.javac.api @@ -46,10 +46,15 @@ import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; - +import java.util.Set; import java.lang.classfile.*; import java.lang.classfile.attribute.ModuleResolutionAttribute; import java.lang.classfile.constantpool.*; +import javax.annotation.processing.AbstractProcessor; +import javax.annotation.processing.RoundEnvironment; +import javax.annotation.processing.SupportedAnnotationTypes; +import javax.lang.model.SourceVersion; +import javax.lang.model.element.TypeElement; import toolbox.JavacTask; import toolbox.Task; import toolbox.Task.Expect; @@ -242,6 +247,29 @@ public class IncubatingTest extends ModuleTestBase { .outdir(testModuleClasses) .files(findJavaFiles(testModuleSrc)) .run(Expect.SUCCESS); + + //test with annotation processing + log = new JavacTask(tb) + .options("--module-path", classes.toString(), + "-XDrawDiagnostics", + "-Werror") + .outdir(testModuleClasses) + .files(findJavaFiles(testModuleSrc)) + .processors(new ProcessorImpl()) + .run(Expect.FAIL) + .writeAll() + .getOutputLines(Task.OutputKind.DIRECT); + + expected = Arrays.asList( + "- compiler.warn.incubating.modules: jdk.i", + "- compiler.err.warnings.and.werror", + "1 error", + "1 warning" + ); + + if (!expected.equals(log)) { + throw new AssertionError("Unexpected output: " + log); + } } private void copyJavaBase(Path targetDir) throws IOException { @@ -270,4 +298,16 @@ public class IncubatingTest extends ModuleTestBase { out.write(newBytes); } } + + @SupportedAnnotationTypes("*") + private static class ProcessorImpl extends AbstractProcessor { + @Override + public boolean process(Set annotations, RoundEnvironment roundEnv) { + return false; + } + @Override + public SourceVersion getSupportedSourceVersion() { + return SourceVersion.latest(); + } + } } diff --git a/test/langtools/tools/javac/processing/rounds/OverwriteBetweenCompilations_2.out b/test/langtools/tools/javac/processing/rounds/OverwriteBetweenCompilations_2.out index 826e2b4bcb0..431fd3d9079 100644 --- a/test/langtools/tools/javac/processing/rounds/OverwriteBetweenCompilations_2.out +++ b/test/langtools/tools/javac/processing/rounds/OverwriteBetweenCompilations_2.out @@ -53,5 +53,3 @@ public abstract class GeneratedClass extends java.ut public void test(long a); } -- compiler.note.deprecated.filename: OverwriteBetweenCompilationsSource.java -- compiler.note.deprecated.recompile