Java Reflection Annotation getAnnotationValue(Object aObj, String aValue)

Here you can find the source of getAnnotationValue(Object aObj, String aValue)

Description

Get annotation value of annotation object via reflection

License

Apache License

Declaration

public static Object getAnnotationValue(Object aObj, String aValue) throws ReflectiveOperationException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.lang.reflect.AnnotatedElement;

import java.lang.reflect.Proxy;

public class Main {
    /**//  w  ww.j av  a  2s . c o  m
     * Get annotation value of annotation object via reflection
     */
    public static Object getAnnotationValue(Object aObj, String aValue) throws ReflectiveOperationException {
        return aObj.getClass().getMethod(aValue).invoke(aObj);
    }

    /**
     * Get annotation value of annotation object via reflection
     */
    public static Object getAnnotationValue(AnnotatedElement aobj, Class aClass, String aValue)
            throws ReflectiveOperationException {
        return getAnnotationValue(getAnnotation(aobj, aClass), aValue);
    }

    /**
     * Get annotation of an object via reflection
     */
    public static Object getAnnotation(AnnotatedElement aobj, Class aClass) throws ReflectiveOperationException {
        for (Object a : aobj.getAnnotations()) {
            if (isAnnotationInstance(aClass, a))
                return a;
        }
        return null;
    }

    private static boolean isAnnotationInstance(Class aClass, Object a) {
        if (Proxy.isProxyClass(a.getClass())) {
            for (Class aInterface : a.getClass().getInterfaces()) {
                if (aInterface.isAssignableFrom(aClass)) {
                    return true;
                }
            }
        }
        return aClass.isInstance(a);
    }
}

Related

  1. getAnnotationValue(Annotation annotation, String attributeName)
  2. getAnnotationValue(Annotation annotation, String value, Class expectedType)
  3. getAnnotationValue(Annotation annotation, String valueName)
  4. getAnnotationValue(Class anno, Field field, String paramName)
  5. getAnnotationValue(final A annotation, final Function valueRetriever, final V defaultValue)
  6. getAnnotationValue(Object obj, Class annotation, String field)
  7. getAnnotationWithinHierarchy(Class fsmClass, Class aggregateClass)
  8. getAnnotationWithMetaAnnotation(AnnotatedElement annotatedElement, Class metaAnnotationType)