Here you can find the source of getAnnotation(Annotation ann, Class
public static <T extends Annotation> T getAnnotation(Annotation ann, Class<T> annotationType)
//package com.java2s; /*//w ww . j a v a 2 s. c o m * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.lang.annotation.Annotation; import java.lang.reflect.AnnotatedElement; import java.lang.reflect.Method; public class Main { public static <T extends Annotation> T getAnnotation(Annotation ann, Class<T> annotationType) { if (annotationType.isInstance(ann)) { return (T) ann; } return ann.annotationType().getAnnotation(annotationType); } public static <T extends Annotation> T getAnnotation(AnnotatedElement ae, Class<T> annotationType) { T ann = ae.getAnnotation(annotationType); if (ann == null) { for (Annotation metaAnn : ae.getAnnotations()) { ann = metaAnn.annotationType().getAnnotation(annotationType); if (ann != null) { break; } } } return ann; } /** * Get a single {@link Annotation} of {@code annotationType} from the supplied {@link Method}. * <p>Correctly handles bridge {@link Method Methods} generated by the compiler. * @param method the method to look for annotations on * @param annotationType the annotation type to look for * @return the annotations found */ public static <A extends Annotation> A getAnnotation(Method method, Class<A> annotationType) { //Method resolvedMethod = BridgeMethodResolver.findBridgedMethod(method); //return getAnnotation((AnnotatedElement) resolvedMethod, annotationType); return getAnnotation((AnnotatedElement) method, annotationType); } }