Returns the annotation on the given class or the package of the class. - Java java.lang.annotation

Java examples for java.lang.annotation:Class Annotation

Description

Returns the annotation on the given class or the package of the class.

Demo Code


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

public class Main {
    public static void main(String[] argv) throws Exception {
        Class clazz = String.class;
        Class annotationClass = String.class;
        System.out.println(findAnnotation(clazz, annotationClass));
    }//from   w ww .j a  v a2s.c o  m

    /**
     * Returns the annotation on the given class or the package of the class. This searchs up the
     * class hierarchy and the package hierarchy for the closest match.
     * 
     * @param clazz The class to search for the annotation
     * @param annotationClass The class of the annotation 
     * @return   The annotation or null
     */
    public static <T extends Annotation> T findAnnotation(Class<?> clazz,
            Class<T> annotationClass) {
        T ann = null;
        while (ann == null && clazz != null) {
            ann = clazz.getAnnotation(annotationClass);
            if (ann == null) {
                ann = clazz.getPackage().getAnnotation(annotationClass);
            }
            if (ann == null) {
                clazz = clazz.getSuperclass();
            }
        }

        return ann;
    }
}

Related Tutorials