get Annotation from Class - Java java.lang.annotation

Java examples for java.lang.annotation:Class Annotation

Description

get Annotation from Class

Demo Code


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

public class Main {
    public static void main(String[] argv) throws Exception {
        Class clazz = String.class;
        Class annotationClass = String.class;
        System.out.println(getAnnotation(clazz, annotationClass));
    }//from w  w w  .ja  v a2 s  .  co m

    public static <T extends Annotation> T getAnnotation(Class<?> clazz,
            Class<T> annotationClass) {
        T annotation = null;
        if (hasAnnotation(clazz, annotationClass)) {
            annotation = clazz.getAnnotation(annotationClass);
        }
        return annotation;
    }

    public static boolean hasAnnotation(Method method,
            Class<? extends Annotation> annotation) {
        return method != null && method.isAnnotationPresent(annotation);
    }

    public static boolean hasAnnotation(Class<?> clazz,
            Class<? extends Annotation> annotation) {
        return clazz != null && clazz.isAnnotationPresent(annotation);
    }
}

Related Tutorials