Here you can find the source of getAnnotation(Class> klazz, Class
Parameter | Description |
---|---|
klazz | a parameter |
annotationClass | a parameter |
public static <T extends Annotation> T getAnnotation(Class<?> klazz, Class<T> annotationClass)
//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); } } }