Skip to content

Commit

Permalink
Fix annotation processing with matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
biboudis committed Aug 9, 2024
1 parent 4780fa5 commit 6ab9242
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ Type signature(MethodSymbol msym,
argbuf.append(l.head.vartype.type);
}

// Enter and bindings.
// Enter and attribute bindings.
ListBuffer<Type> bindingsbuf = null;

if (bindings != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ public void visitMethodDef(JCMethodDecl tree) {
scan(tree.typarams);
scan(tree.recvparam);
scan(tree.params);
scan(tree.bindings);
scan(tree.thrown);
scan(tree.defaultValue);
scan(tree.body);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ public void visitMethodDef(JCMethodDecl tree) {
tree.typarams = translateTypeParams(tree.typarams);
tree.recvparam = translate(tree.recvparam);
tree.params = translateVarDefs(tree.params);
if (tree.bindings != null)
tree.bindings = translateVarDefs(tree.bindings);
tree.thrown = translate(tree.thrown);
tree.body = translate(tree.body);
result = tree;
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,41 @@
* @test
* @summary Verify that annotation processing works with patterns.
* @library /tools/lib
* /tools/javac/lib
* @modules jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.main
* @build toolbox.TestRunner toolbox.ToolBox AnnotationProcessing
* @run main AnnotationProcessing
*/

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.nio.file.Files;
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.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.tools.Diagnostic;

import toolbox.JavacTask;
import toolbox.Task;
import toolbox.TestRunner;
import toolbox.TestRunner.Test;
import toolbox.ToolBox;

import static java.util.stream.Collectors.joining;

public class AnnotationProcessing extends TestRunner {

public static void main(String... args) throws Exception {
Expand Down Expand Up @@ -81,6 +95,64 @@ public pattern T(int i) {
.writeAll();
}

@Test
public void testAnnotationProcessing(Path outerBase) throws Exception {
Path src = outerBase.resolve("src");
tb.writeJavaFiles(src,
"""
import static java.lang.annotation.RetentionPolicy.CLASS;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
public class T {
@Retention(RUNTIME)
@interface RuntimeAnnotation {
int value() default 0;
}
@Retention(CLASS)
@interface ClassAnnotation {
int value() default 0;
}
public static class Person1 {
private final String name;
private final String username;
private boolean capitalize;
public Person1(String name, String username, boolean capitalize) {
this.name = name;
this.username = username;
this.capitalize = capitalize;
}
@AnnotationProcessing.BindingProcessor.Bindings
public pattern Person1(@ClassAnnotation(21) String name, @RuntimeAnnotation(42) String username) {
if (capitalize) {
match Person1(this.name.toUpperCase(), this.username.toUpperCase());
} else {
match Person1(this.name, this.username);
}
}
}
}
""");
Path classes = outerBase.resolve("classes");
Files.createDirectories(classes);
List<String> output = new JavacTask(tb)
.options("-XDrawDiagnostics", "-proc:only", "-processor", "AnnotationProcessing$BindingProcessor",
"-processorpath", System.getProperty("test.classes"), "-parameters",
"--enable-preview", "--source", System.getProperty("java.specification.version"))
.outdir(classes.toString())
.files(tb.findJavaFiles(src))
.run()
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);

if (!output.contains("- compiler.note.proc.messager: T.Person1.Person1(@T.ClassAnnotation(21) name, @T.RuntimeAnnotation(42) username)")) {
throw new AssertionError("Error in annotation processing when processing matchers\n" + output);
}
}

@SupportedAnnotationTypes("*")
public static class P extends AbstractProcessor {
@Override
Expand All @@ -94,4 +166,33 @@ public SourceVersion getSupportedSourceVersion() {
}
}

@SupportedAnnotationTypes("*")
public static class BindingProcessor extends JavacTestingAbstractProcessor {

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@interface Bindings {
String[] value() default {};
}

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
for (Element element : roundEnv.getElementsAnnotatedWith(Bindings.class)) {
if (element instanceof ExecutableElement exec) {
String message = String.format("%s.%s(%s)",
exec.getEnclosingElement(),
exec.getSimpleName(),
exec.getBindings().stream().map(this::printBinding).collect(joining(", ")));
messager.printMessage(Diagnostic.Kind.OTHER, message);
}
}
return false;
}

private String printBinding(VariableElement binding) {
return binding.getAnnotationMirrors().stream().map(String::valueOf).collect(joining(" "))
+ (binding.getAnnotationMirrors().isEmpty() ? "" : " ")
+ binding.getSimpleName();
}
}
}

0 comments on commit 6ab9242

Please sign in to comment.