find Annotations In Type - Java java.lang.annotation

Java examples for java.lang.annotation:Annotation Type

Description

find Annotations In Type

Demo Code


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

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] argv) throws Exception {
        Class clazz = String.class;
        Class annotationClass = String.class;
        System.out.println(findAnnotationsInType(clazz, annotationClass));
    }//from  www  . j a  v a  2  s.  co  m

    public static <T extends Annotation> List<T> findAnnotationsInType(
            Class<?> clazz, Class<T> annotationClass) {
        List<T> answer = new ArrayList<T>();
        T anno = clazz.getAnnotation(annotationClass);

        if (anno != null) {
            answer.add(anno);
        }

        if (clazz.getSuperclass() != null) {
            answer.addAll(findAnnotationsInType(clazz.getSuperclass(),
                    annotationClass));
        }

        return answer;
    }
}

Related Tutorials