How to get the value of an Annotation type Element ?
January 24, 2014 Leave a comment
package com.test; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; public class ValueAnnotation { @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE_USE) protected @interface TypeUse { int value(); } @TypeUse(value = 0) class Test<T>{ } }
Processor
private void processEnclosedElements(List<? extends Element> enclosedElements, RoundEnvironment roundEnv) { for( Element e : enclosedElements ){ if (e.getKind() == ElementKind.CLASS){ System.out.println( "Annotation [" + e.getAnnotationMirrors() + "] [" + e + "]"); processClassTypeParameters(e); } } } private void processClassTypeParameters(Element e) { for( AnnotationMirror am : e.getAnnotationMirrors() ){ for(Map.Entry< ? extends ExecutableElement, ? extends AnnotationValue> m : am.getElementValues().entrySet()){ System.out.println( "[" + m.getKey() +"][" + m.getValue() + "]"); } } }
Annotation [@com.test.ValueAnnotation.TypeUse(0)] [com.test.ValueAnnotation.Test]
[value()][0]