Java Reflection Annotation getAnnotation(Object obj, Class acl)

Here you can find the source of getAnnotation(Object obj, Class acl)

Description

get Annotation

License

Open Source License

Declaration

public static <A extends Annotation> A getAnnotation(Object obj, Class<A> acl) 

Method Source Code


//package com.java2s;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

public class Main {

    public static <A extends Annotation> A getAnnotation(Object obj, Class<A> acl) {
        return getAnnotation(obj.getClass(), acl);
    }//  w w w. j a va 2  s. c o  m

    @SuppressWarnings("unchecked")
    public static <A extends Annotation> A getAnnotation(Class cl, Class<A> acl) {
        A ret = (A) cl.getAnnotation(acl);
        while (ret == null && cl.getSuperclass() != null) {
            cl = cl.getSuperclass();
            ret = (A) cl.getAnnotation(acl);
        }
        return ret;
    }

    @SuppressWarnings("unchecked")
    public static <A extends Annotation> A getAnnotation(Class cl, Method m, Class<A> acl) {
        A ret = (A) m.getAnnotation(acl);
        while (ret == null && cl.getSuperclass() != null) {
            cl = cl.getSuperclass();
            try {
                m = cl.getMethod(m.getName(), m.getParameterTypes());
                ret = m.getAnnotation(acl);
            } catch (NoSuchMethodException e) {
                break;
            }
        }
        return ret;
    }
}

Related

  1. getAnnotation(Method method, Class type)
  2. getAnnotation(Object bean, final Class annotation)
  3. getAnnotation(Object context, Class annotation)
  4. getAnnotation(Object instance, Class type)
  5. getAnnotation(Object obj, Class annClass)
  6. getAnnotation(Object obj, Class annotation)
  7. getAnnotation(ProceedingJoinPoint pjp, Class annotationClass)
  8. getAnnotationAttribute(Annotation a, String attrName)
  9. getAnnotationAttribute(final Annotation anno, final String attrName, final Class attrType)