Returns true if any of the supplied annotations is present on the target class or any of its super classes. - Java java.lang.annotation

Java examples for java.lang.annotation:Class Annotation

Description

Returns true if any of the supplied annotations is present on the target class or any of its super classes.

Demo Code


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

public class Main {
    public static void main(String[] argv) throws Exception {
        Class target = String.class;
        Class annotations = String.class;
        System.out.println(isAnyAnnotationPresent(target, annotations));
    }/*w  w  w. j  av  a  2 s.c  om*/

    /**
     * Returns true if any of the supplied annotations is present on the target class
     * or any of its super classes.
     * 
     * @param annotations Annotations to find.
     * 
     * @return true if any of the supplied annotation is present on the target class
     * or any of its super classes.
     **/
    public static boolean isAnyAnnotationPresent(Class<?> target,
            Class<? extends Annotation>... annotations) {
        for (Class<? extends Annotation> annotationClass : annotations) {
            if (isAnnotationPresent(target, annotationClass)) {
                return true;
            }
        }
        return false;
    }

    /**
     * Returns true if the supplied annotation is present on the target class
     * or any of its super classes.
     * 
     * @param annotation Annotation to find.
     * 
     * @return true if the supplied annotation is present on the target class
     * or any of its super classes.
     **/
    public static boolean isAnnotationPresent(Class<?> target,
            Class<? extends Annotation> annotation) {
        Class<?> clazz = target;
        if (clazz.isAnnotationPresent(annotation)) {
            return true;
        }
        while ((clazz = clazz.getSuperclass()) != null) {
            if (clazz.isAnnotationPresent(annotation)) {
                return true;
            }
        }
        return false;
    }
}

Related Tutorials