Update tests

This commit is contained in:
Liam Miller-Cushon 2025-11-05 08:43:34 +01:00
parent 6decf278f7
commit 895b48fc7f
2 changed files with 91 additions and 6 deletions

View File

@ -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<? extends TypeElement> 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<String> 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<java.lang.String>) 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<java.lang.String>) has annotations []",
"1 error");
// FIXME
expectedOutput = List.of("");
if (!expectedOutput.equals(log)) {
throw new Exception("expected output not found: " + log);
}