Description

This is proxy ready implementation Class#getAnnotation(Class) .

License

Open Source License

Parameter

Parameter Description
clazz the class to be tested
annotationClass the annotation be be looked up
A the annotation type

Return

true if the clazz or its super-class(s) is annotated by annotationClass

Declaration

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

Method Source Code


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

import java.lang.annotation.Annotation;

public class Main {
    /**//w  w  w.j  a v  a2 s. c om
     * This is proxy ready implementation {@link Class#getAnnotation(Class)}.
     *
     * @param clazz           the class to be tested
     * @param annotationClass the annotation be be looked up
     * @param <A>             the annotation type
     * @return {@code true} if the {@code clazz} or its super-class(s) is annotated by {@code annotationClass}
     */
    public static <A extends Annotation> A getAnnotation(Class<?> clazz, Class<A> annotationClass) {
        final A annotation = clazz.getAnnotation(annotationClass);

        if (annotation == null && clazz.isSynthetic()) { //OK, this is probably proxy
            return getAnnotation(clazz.getSuperclass(), annotationClass);
        } else {
            return annotation;
        }
    }
}

Related

  1. getAnnotation(Class annotationClass, Annotation[] annotations)
  2. getAnnotation(Class base, Class annotationClass)
  3. getAnnotation(Class c, Class annotationClass)
  4. getAnnotation(Class classForAnnotation, Class annotationClass)
  5. getAnnotation(Class clazz, Class annotationClass)
  6. getAnnotation(Class clazz, Class annotClass, boolean forceInherit)
  7. getAnnotation(Class clazz, Class annotationClass)
  8. getAnnotation(Class clazz, Class ann)
  9. getAnnotation(Class clazz, Class annClazz)