Here you can find the source of getAnnotation(Class> clazz, Class
public static <T extends Annotation> T getAnnotation(Class<?> clazz, Class<T> annotationClass)
//package com.java2s; //License from project: Apache License import java.lang.annotation.Annotation; import java.lang.annotation.Inherited; import java.lang.reflect.Method; public class Main { public static <T extends Annotation> T getAnnotation(Method method, Class<T> annotationClass) { T t = getAnnotation(method, annotationClass, annotationClass.isAnnotationPresent(Inherited.class)); return t; }//from w w w. j av a2s . c o m public static <T extends Annotation> T getAnnotation(Class<?> clazz, Class<T> annotationClass) { T t = clazz.getAnnotation(annotationClass); return t; } private static <T extends Annotation> T getAnnotation(Method method, Class<T> annotationClass, boolean seekSuper) { T t = method.getAnnotation(annotationClass); if (t == null && seekSuper) { Class<?> clazz = method.getDeclaringClass().getSuperclass(); if (clazz != null) { try { method = clazz.getMethod(method.getName(), method.getParameterTypes()); t = getAnnotation(method, annotationClass, true); } catch (NoSuchMethodException e) { } } } return t; } private static boolean isAnnotationPresent(boolean isAll, Class<?> clazz, Class<? extends Annotation>... annotationClasses) { for (Class c : annotationClasses) { if (clazz.isAnnotationPresent(c)) { if (!isAll) { return true; } } else if (isAll) { return false; } } return isAll; } private static boolean isAnnotationPresent(boolean isAll, Method method, Class<? extends Annotation>... annotationClasses) { for (Class c : annotationClasses) { if (method.isAnnotationPresent(c)) { if (!isAll) { return true; } } else if (isAll) { return false; } } return isAll; } }