private static < A extends Annotation > A getAnnotation ( final Method m , final Class < A > annotationClass ) { // if we have invalid data the result is null if ( m == null || annotationClass == null ) { return null ; } if ( m . isAnnotationPresent ( annotationClass ) ) { return m . getAnnotation ( annotationClass ) ; } // if we've already reached the Object class, return null; Class < ? > c = m . getDeclaringClass ( ) ; if ( c . getSuperclass ( ) == null ) { return null ; } // check directly implemented interfaces for the method being checked for ( Class < ? > i : c . getInterfaces ( ) ) { try { Method im = i . getMethod ( m . getName ( ) , m . getParameterTypes ( ) ) ; return getAnnotation ( im , annotationClass ) ; } catch ( final SecurityException ex ) { continue ; } catch ( final NoSuchMethodException ex ) { continue ; } } try { return getAnnotation ( c . getSuperclass ( ) . getMethod ( m . getName ( ) , m . getParameterTypes ( ) ) , annotationClass ) ; } catch ( final SecurityException ex ) { return null ; } catch ( final NoSuchMethodException ex ) { return null ; } }
Searches the class hierarchy to see if the method or it s super implementations and interfaces has the annotation .