diff --git a/test/langtools/tools/javac/annotations/typeAnnotations/.CompletionErrorOnEnclosingType.java.swp b/test/langtools/tools/javac/annotations/typeAnnotations/.CompletionErrorOnEnclosingType.java.swp new file mode 100644 index 00000000000..8020d3bd75d Binary files /dev/null and b/test/langtools/tools/javac/annotations/typeAnnotations/.CompletionErrorOnEnclosingType.java.swp differ diff --git a/test/langtools/tools/javac/annotations/typeAnnotations/CompletionErrorOnEnclosingType.java b/test/langtools/tools/javac/annotations/typeAnnotations/CompletionErrorOnEnclosingType.java index cc6e48bb85d..f797787495a 100644 --- a/test/langtools/tools/javac/annotations/typeAnnotations/CompletionErrorOnEnclosingType.java +++ b/test/langtools/tools/javac/annotations/typeAnnotations/CompletionErrorOnEnclosingType.java @@ -36,6 +36,17 @@ import toolbox.Task.*; import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; +import java.util.Set; + +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.Element; +import javax.lang.model.element.TypeElement; +import javax.lang.model.type.TypeMirror; +import javax.lang.model.util.ElementFilter; +import javax.tools.Diagnostic; public class CompletionErrorOnEnclosingType { ToolBox tb = new ToolBox(); @@ -43,9 +54,13 @@ public class CompletionErrorOnEnclosingType { public static void main(String... args) throws Exception { CompletionErrorOnEnclosingType t = new CompletionErrorOnEnclosingType(); t.testMissingEnclosingType(); + t.testAnnotationProcessing(); } - void testMissingEnclosingType() throws Exception { + Path src; + Path out; + + void setup() throws Exception { String annoSrc = """ import static java.lang.annotation.ElementType.TYPE_USE; @@ -67,12 +82,16 @@ public class CompletionErrorOnEnclosingType { """; Path base = Paths.get("."); - Path src = base.resolve("src"); + src = base.resolve("src"); tb.createDirectories(src); tb.writeJavaFiles(src, annoSrc, cSrc); - Path out = base.resolve("out"); + out = base.resolve("out"); tb.createDirectories(out); new JavacTask(tb).outdir(out).files(tb.findJavaFiles(src)).run(); + } + + void testMissingEnclosingType() throws Exception { + setup(); // now if we remove A.class there will be an error but javac should not crash tb.deleteFiles(out.resolve("A.class")); @@ -85,14 +104,80 @@ public class CompletionErrorOnEnclosingType { .run(Expect.SUCCESS) .writeAll() .getOutputLines(Task.OutputKind.DIRECT); + } + + @SupportedAnnotationTypes("*") + public static class Processor extends AbstractProcessor { + @Override + public SourceVersion getSupportedSourceVersion() { + return SourceVersion.latestSupported(); + } + + boolean first = true; + + @Override + public boolean process(Set annotations, RoundEnvironment roundEnv) { + if (!first) { + return false; + } + Element te = processingEnv.getElementUtils().getTypeElement("B"); + for (var f : ElementFilter.fieldsIn(te.getEnclosedElements())) { + TypeMirror t = f.asType(); + processingEnv + .getMessager() + .printMessage( + Diagnostic.Kind.NOTE, + "%s (%s) has annotations [%s]" + .formatted(f, t, t.getAnnotationMirrors())); + } + first = false; + return false; + } + } + + void testAnnotationProcessing() throws Exception { + setup(); + + List log = + new JavacTask(tb) + .outdir(out) + .classpath(out) + .options("-XDrawDiagnostics") + .files(src.resolve("C.java")) + .processors(new Processor()) + .run(Expect.SUCCESS) + .writeAll() + .getOutputLines(Task.OutputKind.DIRECT); var expectedOutput = + List.of( + "- compiler.note.proc.messager: a (@Anno A) has" + + " annotations [@Anno]"); + if (!expectedOutput.equals(log)) { + throw new Exception("expected output not found: " + log); + } + + // now if we remove A.class there will be an error and the annotations won't be available + // but javac should not crash + tb.deleteFiles(out.resolve("A.class")); + + log = + new JavacTask(tb) + .outdir(out) + .classpath(out) + .options("-XDrawDiagnostics") + .files(src.resolve("C.java")) + .processors(new Processor()) + .run(Expect.FAIL) + .writeAll() + .getOutputLines(Task.OutputKind.DIRECT); + + expectedOutput = List.of( "B.class:-:-: compiler.err.cant.attach.type.annotations: @Anno, B, a," - + " (compiler.misc.class.file.not.found: A)", + + " (compiler.misc.class.file.not.found: A)", + "- compiler.note.proc.messager: a (A) has annotations []", "1 error"); - // FIXME - expectedOutput = List.of(""); if (!expectedOutput.equals(log)) { throw new Exception("expected output not found: " + log); }