8378950: Repeated warnings when annotation processing is happening

Co-authored-by: Archie Cobbs <acobbs@openjdk.org>
Reviewed-by: jlahoda
This commit is contained in:
Dusan Balek 2026-04-02 08:20:47 +00:00 committed by Jan Lahoda
parent c18e3a3377
commit fa5ec62405
4 changed files with 103 additions and 13 deletions

View File

@ -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<JCDiagnostic> ACCEPT_NON_RECOVERABLE_LINTS =
d -> !Optional.of(d)
.map(JCDiagnostic::getLintCategory)
.map(lc -> lc.annotationSuppression ||
lc == Lint.LintCategory.INCUBATING)
.orElse(false);
private final Predicate<JCDiagnostic> ACCEPT_NON_RECOVERABLE =
d -> d.getKind() != JCDiagnostic.Kind.ERROR ||
!d.isFlagSet(DiagnosticFlag.RECOVERABLE) ||

View File

@ -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<String> 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<String> 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<String> 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

View File

@ -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<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
return false;
}
@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latest();
}
}
}

View File

@ -53,5 +53,3 @@ public abstract class GeneratedClass<E extends java.lang.Number> extends java.ut
public void test(long a);
}
- compiler.note.deprecated.filename: OverwriteBetweenCompilationsSource.java
- compiler.note.deprecated.recompile