I'm trying to instrument some anonymous classes. For that I created an annotation:
@Target(ElementType.TYPE_USE)
@Retention(RetentionPolicy.SOURCE)
public @interface Ann {
}
An annotation processor:
@SupportedAnnotationTypes("Ann")
@SupportedSourceVersion(SourceVersion.RELEASE_11)
public class AP extends AbstractProcessor {
@Override
public boolean process(Set<? extends TypeElement> anns, RoundEnvironment env) {
System.out.println("INVOKED");
return true;
}
}
I compiled these classes and created a test class:
public class T {
interface I {
void f();
}
public static void main(String[] args) {
var o = new @Ann I() {
@Override
public void f() {
System.out.println("f()");
}
};
}
}
Then I tried compiling this class with the annotation processor:
javac src/test/java/T.java -cp build/classes/java/main/ -processor AP -d out
But it doesn't print anything. However, if I move the annotation @Ann from the anonymous class to the T class (@Ann public class T{), it works as expected.
How do I instrument anonymous classes using annotation processing?