Determine whether an annotation for the specified annotation Type is present on the supplied clazz and is java.lang.annotation.Inherited inherited i.e., not declared locally for the class). - Java java.lang.annotation

Java examples for java.lang.annotation:Class Annotation

Description

Determine whether an annotation for the specified annotation Type is present on the supplied clazz and is java.lang.annotation.Inherited inherited i.e., not declared locally for the class).

Demo Code


import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;

public class Main{

    /**/*from   w ww  .j  a v  a2s . c  om*/
     * Determine whether an annotation for the specified <code>annotationType</code> is present
     * on the supplied <code>clazz</code> and is {@link java.lang.annotation.Inherited inherited}
     * i.e., not declared locally for the class).
     * <p>If the supplied <code>clazz</code> is an interface, only the interface itself will be checked.
     * In accordance with standard meta-annotation semantics, the inheritance hierarchy for interfaces
     * will not be traversed. See the {@link java.lang.annotation.Inherited JavaDoc} for the
     * &#064;Inherited meta-annotation for further details regarding annotation inheritance.
     * @param annotationType the Class object corresponding to the annotation type
     * @param clazz the Class object corresponding to the class on which to check for the annotation
     * @return <code>true</code> if an annotation for the specified <code>annotationType</code> is present
     * on the supplied <code>clazz</code> and is {@link java.lang.annotation.Inherited inherited}
     * @see Class#isAnnotationPresent(Class)
     * @see #isAnnotationDeclaredLocally(Class, Class)
     */
    public static boolean isAnnotationInherited(
            Class<? extends Annotation> annotationType, Class<?> clazz) {
        Assert.notNull(annotationType, "Annotation type must not be null");
        Assert.notNull(clazz, "Class must not be null");
        return (clazz.isAnnotationPresent(annotationType) && !isAnnotationDeclaredLocally(
                annotationType, clazz));
    }
    /**
     * Determine whether an annotation for the specified <code>annotationType</code> is
     * declared locally on the supplied <code>clazz</code>. The supplied {@link Class}
     * may represent any type.
     * <p>Note: This method does <strong>not</strong> determine if the annotation is
     * {@link java.lang.annotation.Inherited inherited}. For greater clarity regarding inherited
     * annotations, consider using {@link #isAnnotationInherited(Class, Class)} instead.
     * @param annotationType the Class object corresponding to the annotation type
     * @param clazz the Class object corresponding to the class on which to check for the annotation
     * @return <code>true</code> if an annotation for the specified <code>annotationType</code>
     * is declared locally on the supplied <code>clazz</code>
     * @see Class#getDeclaredAnnotations()
     * @see #isAnnotationInherited(Class, Class)
     */
    public static boolean isAnnotationDeclaredLocally(
            Class<? extends Annotation> annotationType, Class<?> clazz) {
        Assert.notNull(annotationType, "Annotation type must not be null");
        Assert.notNull(clazz, "Class must not be null");
        boolean declaredLocally = false;
        for (Annotation annotation : Arrays.asList(clazz
                .getDeclaredAnnotations())) {
            if (annotation.annotationType().equals(annotationType)) {
                declaredLocally = true;
                break;
            }
        }
        return declaredLocally;
    }
}

Related Tutorials