Here you can find the source of getAnnotation(final Method method, final Class
public static <T extends Annotation> T getAnnotation(final Method method, final Class<T> annotationClass)
//package com.java2s; /******************************************************************************* * Copyright (c) 2012, 2013 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * //from www. j a v a2 s . c o m * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ import java.lang.annotation.Annotation; import java.lang.reflect.Method; public class Main { public static <T extends Annotation> T getAnnotation(final Method method, final Class<T> annotationClass) { T annotation = method.getAnnotation(annotationClass); if (annotation != null) return annotation; final Class<?> declaringClass = method.getDeclaringClass(); Class<?> superClass = declaringClass.getSuperclass(); while (superClass != null) { try { final Method superClassMethod = superClass.getDeclaredMethod(method.getName(), method.getParameterTypes()); return getAnnotation(superClassMethod, annotationClass); } catch (final Exception exception) { // Ignore and exit here return null; } } return null; } }