Here you can find the source of getAnnotation(Class ac, AnnotatedElement m, Annotation... directAnnotations)
public static <A extends Annotation> A getAnnotation(Class<A> ac, AnnotatedElement m, Annotation... directAnnotations)
//package com.java2s; import java.lang.annotation.Annotation; import java.lang.reflect.AnnotatedElement; import java.lang.reflect.Member; public class Main { public static <A extends Annotation> A getAnnotation(Class<A> ac, AnnotatedElement m, Annotation... directAnnotations) { return getAnnotation(ac, false, m, directAnnotations); }// w w w . j ava 2 s . c o m private static <A extends Annotation> A getAnnotation(Class<A> ac, boolean inherit, AnnotatedElement m, Annotation... directAnnotations) { if (directAnnotations != null) { for (Annotation ann : directAnnotations) { if (ac.isInstance(ann)) { return ac.cast(ann); } } } if (m == null) { return null; } A a = m.getAnnotation(ac); if (a != null) { return a; } if (inherit) { if (m instanceof Member) { return getAnnotation(ac, inherit, ((Member) m).getDeclaringClass()); } if (m instanceof Class<?>) { Class<?> c = (Class<?>) m, dc = c.getDeclaringClass(); Class p = c.getSuperclass(); while (p != null) { a = getAnnotation(ac, true, p); if (a != null) { return a; } p = p.getSuperclass(); } if (dc != null) { return getAnnotation(ac, inherit, dc); } } } return null; } }