I wrote a simple annotation and an AnnotationProcessor to process the annotation.
The annotation has a single value: it should be the name of an existing interface (with package).
The annotation processor should retrieve the value of the annotation, retrieve the Class object of the interface and finally should print all method declared in the interface.
Example: this is my annotation
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.CLASS)
public @interface MyAnnotation{
public String interfaceName();
}
and this is the annotated class:
@MyAnnotation(interfaceName = "java.lang.CharSequence")
public class Example{}
my processor looks like
[...]
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment env) {
for (TypeElement te : annotations) {
for(Element e : env.getElementsAnnotatedWith(te)) {
MyAnnotation myAnnotation = e.getAnnotation(MyAnnotation.class);
String iName = myAnnotation.interfaceName();
Class<?> clazz = Class.forName(iName);
// use reflection to cycle through methods and prints them
[...]
}
}
now this works fine if I configure an interface like java.lang.CharSequence as the interfaceName of MyAnnotation;
however, if I configure as interfaceName an interface located in a .jar file (added in the buildpath of the project), I obtain a ClassNotFoundException when I try to do the Class.forName(...) statement.
Any thoughts?
Thank you, cheers
Elements#getTypeElement(CharSequence)return a non-nullTypeElement?TypeElement).ExecutableElement. Check out the methods of theElementFilterclass. Note, however, that I'm not saying you have to use the annotation processor API in this case.