find Annotation for Class - Java java.lang.annotation

Java examples for java.lang.annotation:Class Annotation

Description

find Annotation for Class

Demo Code


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

public class Main {
    public static void main(String[] argv) throws Exception {
        Class classy = String.class;
        Class targetAnnotation = String.class;
        System.out.println(findAnnotation(classy, targetAnnotation));
    }//from   ww  w.  jav  a2  s  .c o m

    static Annotation findAnnotation(Class<?> classy,
            Class<? extends Annotation> targetAnnotation) {
        Annotation[] annotations = classy.getAnnotations();
        for (Annotation annotation : annotations) {
            if (targetAnnotation.isInstance(annotation)) {
                return annotation;
            }
        }
        return null;
    }
}

Related Tutorials