Mega Code Archive

 
Categories / Java / Reflection
 

Get annotation value

import java.lang.annotation.Annotation; import java.lang.reflect.Method; public class Util{   @SuppressWarnings("unchecked")   public static<T> T getAnnotationValue(Class<?> clazz,Class<? extends Annotation> annotationClass,String element) throws Exception {     Annotation annotation = clazz.getAnnotation(annotationClass);     Method method = annotationClass.getMethod(element,(Class[])null);     if (annotation == null)       return((T)method.getDefaultValue());     return((T)method.invoke(annotation,(Object[])null));   } }