Java Reflection Annotation getAnnotation(Class clazz, Class annotationClass)

Here you can find the source of getAnnotation(Class clazz, Class annotationClass)

Description

get Annotation

License

Apache License

Declaration

public static <T extends Annotation> T getAnnotation(Class<?> clazz,
            Class<T> annotationClass) 

Method Source Code

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

import java.lang.annotation.Annotation;
import java.lang.annotation.Inherited;

import java.lang.reflect.Method;

public class Main {

    public static <T extends Annotation> T getAnnotation(Method method,
            Class<T> annotationClass) {
        T t = getAnnotation(method, annotationClass,
                annotationClass.isAnnotationPresent(Inherited.class));
        return t;
    }//from w w w.  j  av a2s . c  o  m

    public static <T extends Annotation> T getAnnotation(Class<?> clazz,
            Class<T> annotationClass) {
        T t = clazz.getAnnotation(annotationClass);
        return t;
    }

    private static <T extends Annotation> T getAnnotation(Method method,
            Class<T> annotationClass, boolean seekSuper) {
        T t = method.getAnnotation(annotationClass);
        if (t == null && seekSuper) {
            Class<?> clazz = method.getDeclaringClass().getSuperclass();
            if (clazz != null) {
                try {
                    method = clazz.getMethod(method.getName(),
                            method.getParameterTypes());
                    t = getAnnotation(method, annotationClass, true);
                } catch (NoSuchMethodException e) {

                }
            }
        }
        return t;
    }

    private static boolean isAnnotationPresent(boolean isAll,
            Class<?> clazz,
            Class<? extends Annotation>... annotationClasses) {
        for (Class c : annotationClasses) {
            if (clazz.isAnnotationPresent(c)) {
                if (!isAll) {
                    return true;
                }
            } else if (isAll) {
                return false;
            }
        }
        return isAll;
    }

    private static boolean isAnnotationPresent(boolean isAll,
            Method method, Class<? extends Annotation>... annotationClasses) {
        for (Class c : annotationClasses) {
            if (method.isAnnotationPresent(c)) {
                if (!isAll) {
                    return true;
                }
            } else if (isAll) {
                return false;
            }
        }
        return isAll;
    }
}

Related

  1. getAnnotation(Class clazz, Class annClazz)
  2. getAnnotation(Class clazz, Class annotation)
  3. getAnnotation(Class clazz, Class annotation)
  4. getAnnotation(Class clazz, Class annotationClass)
  5. getAnnotation(Class clazz, Class annotationClass)
  6. getAnnotation(Class clazz, Class annotationType)
  7. getAnnotation(Class clazz, Class annotationType)
  8. getAnnotation(Class clazz, Class annotationType)
  9. getAnnotation(Class clazz, String fieldName, Class annotationClass)