Returns information whether the given annotation type annotation is annotated by given annotation type annotated By. - Java java.lang.annotation

Java examples for java.lang.annotation:Annotation Type

Description

Returns information whether the given annotation type annotation is annotated by given annotation type annotated By.

Demo Code


import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Main{
    /**/*from   www.j  a va 2s. c om*/
     * Returns information whether the given annotation type <code>annotation</code> is annotated by
     * given annotation type <code>annotatedBy</code>.
     *
     * @param annotation the annotation type to check.
     * @param annotatedBy the annotation type which may annotates the given annotation.
     * @return <code>true</code> if given annotation type <code>annotatedBy</code> annotates given
     *         annotation type <code>annotation</code>; <code>false</code> otherwise.
     */
    public static boolean isAnnotatedAnnotation(Annotation annotation,
            Class<? extends Annotation> annotatedBy) {
        return isAnnotatedAnnotation(
                annotation == null ? null : annotation.annotationType(),
                annotatedBy);
    }
    /**
     * Returns information whether the given annotation <code>annotation</code> is annotated by given
     * annotation type <code>annotatedBy</code>.
     *
     * @param annotation the annotation to check.
     * @param annotatedBy the annotation type which may annotates the given annotation.
     * @return <code>true</code> if given annotation type <code>annotatedBy</code> annotates given
     *         annotation <code>annotation</code>; <code>false</code> otherwise.
     */
    public static boolean isAnnotatedAnnotation(
            Class<? extends Annotation> annotation,
            Class<? extends Annotation> annotatedBy) {
        Preconditions.checkArgument(annotation != null
                && annotatedBy != null, "Annotation cannot be null");

        Annotation[] annotations = annotation.getAnnotations();
        for (Annotation a : annotations) {
            Class<? extends Annotation> aType = a.annotationType();
            if (annotatedBy.equals(aType)) {
                return true;
            }
        }

        return false;
    }
}

Related Tutorials