Java Reflection Annotation getAnnotation(Class klazz, Class annotationClass)

Here you can find the source of getAnnotation(Class klazz, Class annotationClass)

Description

get Annotation

License

Open Source License

Parameter

Parameter Description
klazz a parameter
annotationClass a parameter

Return

the annotation of the given class from the given class

Declaration

public static <T extends Annotation> T getAnnotation(Class<?> klazz, Class<T> annotationClass) 

Method Source Code


//package com.java2s;
/*/*from   w  w  w . ja va2s.  c  o  m*/
 * JBoss, Home of Professional Open Source.
*
* See the LEGAL.txt file distributed with this work for information regarding copyright ownership and licensing.
*
* See the AUTHORS.txt file distributed with this work for a full listing of individual contributors.
*/

import java.lang.annotation.Annotation;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;

public class Main {
    /**
     * @param klazz
     * @param annotationClass
     *
     * @return the annotation of the given class from the given class
     */
    public static <T extends Annotation> T getAnnotation(Class<?> klazz, Class<T> annotationClass) {
        return klazz.getAnnotation(annotationClass);
    }

    /**
     * @param accessibleObject
     * @param annotationClass
     *
     * @return the annotation of the given class from the given accessibleObject
     */
    public static <T extends Annotation> T getAnnotation(AccessibleObject accessibleObject,
            Class<T> annotationClass) {
        return accessibleObject.getAnnotation(annotationClass);
    }

    /**
     * @param enumValue
     * @param annotationClass
     *
     * @return the annotation of the given class from the given enumValue
     */
    public static <T extends Annotation> T getAnnotation(Enum<?> enumValue, Class<T> annotationClass) {
        try {
            Field enumField = enumValue.getClass().getField(enumValue.name());
            return getAnnotation(enumField, annotationClass);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }
}

Related

  1. getAnnotation(Class clazz, String name, Class[] types, Class annotationType)
  2. getAnnotation(Class cls, Class annotationClass)
  3. getAnnotation(Class cls, Class annotation)
  4. getAnnotation(Class componentClass, Class annotationClass)
  5. getAnnotation(Class configInterface, Method method, Class annotationType, boolean searchMethodType)
  6. getAnnotation(Class objectClass, Class annotationClass)
  7. getAnnotation(Class onClass, Class desiredAnnotationClass)
  8. getAnnotation(Class target, Class annoCls)
  9. getAnnotation(Class target, Class annotationClass)