Java Reflection Annotation getAnnotationElementValue(AnnotatedElement annotatedElement, String annotationClassName, String annotationElementName, Class annotationElementType)

Here you can find the source of getAnnotationElementValue(AnnotatedElement annotatedElement, String annotationClassName, String annotationElementName, Class annotationElementType)

Description

get Annotation Element Value

License

Open Source License

Declaration

public static <T> T getAnnotationElementValue(AnnotatedElement annotatedElement, String annotationClassName,
            String annotationElementName, Class<T> annotationElementType) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;

public class Main {
    public static <T> T getAnnotationElementValue(AnnotatedElement annotatedElement, String annotationClassName,
            String annotationElementName, Class<T> annotationElementType) {
        final Annotation annotation = getAnnotation(annotatedElement, annotationClassName);
        return getAnnotationElementValue(annotation, annotationElementName, annotationElementType);
    }/* w w  w  . j a v  a  2 s. c om*/

    @SuppressWarnings("unchecked")
    public static <T> T getAnnotationElementValue(Annotation annotation, String annotationElementName,
            Class<T> annotationElementType) {
        try {
            if (annotation != null) {
                for (Method method : annotation.getClass().getMethods()) {
                    if (method.getName().equals(annotationElementName)) {
                        final Object value = method.invoke(annotation);
                        if (annotationElementType.isInstance(value)) {
                            return (T) value;
                        }
                    }
                }
            }
            return null;
        } catch (ReflectiveOperationException e) {
            throw new RuntimeException(e);
        }
    }

    public static Annotation getAnnotation(AnnotatedElement annotatedElement, String annotationClassName) {
        if (annotatedElement != null) {
            for (Annotation annotation : annotatedElement.getAnnotations()) {
                if (annotation.annotationType().getName().equals(annotationClassName)) {
                    return annotation;
                }
            }
        }
        return null;
    }
}

Related

  1. getAnnotationClass(Class entityClass, Class annotationClass)
  2. getAnnotationClass(String name)
  3. getAnnotationDeep(Annotation from, Class toFind)
  4. getAnnotationDefault(Class annotationClass, String element)
  5. getAnnotationDefaultMap(Class annotationClass)
  6. getAnnotationField(Annotation annotation, String field)
  7. getAnnotationFields(Class claz, Class annotationType)
  8. getAnnotationFields(Class clazz, Class annotationClass)
  9. getAnnotationForMethodOrContainingClass(Method m, Class annotationType)