Get annotation value : Annotation « Reflection « Java






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));
  }
}

   
    
    
    
  








Related examples in the same category

1.Uses reflection to display the annotation associated with a method.
2.Get annotation by annotation class
3.Show all annotations for a class and a method.
4.default values in an annotation.
5.Does a method have an annotation
6.Get Annotation Parameter
7.A better concise toString method for annotation types
8.Find Annotated Method
9.Find Annotated Fields
10.Get default annotation value
11.Is Field Annotation Present
12.Get Annotated Declared Fields