Searches an annotation in the complete hierarchy - Java java.lang.annotation

Java examples for java.lang.annotation:Annotation Attribute

Description

Searches an annotation in the complete hierarchy

Demo Code


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

public class Main {

    /**// w  ww .  j  a va  2s. c  o  m
     * Searches an annotation in the complete hierarchy
     * @param startClass
     * @param annotation
     * @return
     */
    public static <T extends Annotation> T findInTree(Class startClass,
            Class<T> annotation) {
        T anno = (T) startClass.getAnnotation(annotation);
        if (anno != null) {
            return anno;
        }
        if (startClass.getSuperclass() != Object.class) {
            return findInTree(startClass.getSuperclass(), annotation);
        }
        return null;
    }
}

Related Tutorials