Java Reflection Annotation getAnnotation(AnnotatedElement aobj, Class aClass)

Here you can find the source of getAnnotation(AnnotatedElement aobj, Class aClass)

Description

Get annotation of an object via reflection

License

Apache License

Declaration

public static Object getAnnotation(AnnotatedElement aobj, Class aClass) 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 {
    /**/*from   w  w w  .  java2  s  . com*/
     * 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. getAnnotation( final AnnotatedElement anElement, final Class annotationClass)
  2. getAnnotation( final Method method, final Class annotationClass)
  3. getAnnotation(@Nonnull Annotation[] annotations, @Nonnull Class annotation)
  4. getAnnotation(@Nonnull Class cls, @Nonnull Class annotation)
  5. getAnnotation(AnnotatedElement ae, Class annotationType)
  6. getAnnotation(AnnotatedElement element, Class annotation)
  7. getAnnotation(AnnotatedElement element, String annotationTypeName)
  8. getAnnotation(AnnotatedElement target, String annotationType)
  9. getAnnotation(Annotation ann, Class annotationType)