For the following custom Java annotation
@CustomAnnotation(clazz=SomeClass.class) public class MyApplicationCode { ... }
Basically, I want to be able to capture both the class object for the MyApplicationCode parameter and the clazz parameter at compile time to confirm the consistency of coding conventions (another story). Basically, I want to have access to the MyApplicationCode.class and Someclass.class file in the annotation handler. I'm almost there, but I'm missing something. I have
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.SOURCE) public @interface CustomAnnotation { public Class clazz(); }
Then I have a processor:
public class CustomAnnotationProcessor extends AbstractProcessor { private ProcessingEnvironment processingEnvironment; @Override public synchronized void init(ProcessingEnvironment processingEnvironment) { this.processingEnvironment = processingEnvironment; } @Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment environment) { Set<? extends Element> elements = environment.getElementsAnnotatedWith(ActionCommand.class); for(Element e : elements) { Annotation annotation = e.getAnnotation(CustomAnnotation.class); Class clazz = ((CustomAnnotation)annotation).clazz();
What I'm trying to do in the process method is to grab SomeClass.class and MyApplication.class from the source code below to do some custom checking at compile time. I cannot seem that life determines me how to get these two values ββ...
@CustomAnnotation(clazz=SomeClass.class) public class MyApplicationCode
Update: The next post has a lot more detail, and it is much closer. But the problem is that you still have a TypeMirror object from which a class object is pulled that it does not explain: http://blog.retep.org/2009/02/13/getting-class-values-from-annotations -in-an-annotationprocessor /
Update2: You can get MyApplication.class by doing
String classname = ((TypeElement)e).getQualifiedName().toString();
source share