private static int getAnnotationDepth ( final Method m , final Class < ? extends Annotation > annotationClass ) { // if we have invalid data the result is -1 if ( m == null || annotationClass == null ) { return - 1 ; } if ( m . isAnnotationPresent ( annotationClass ) ) { return 1 ; } // if we've already reached the Object class, return -1; Class < ? > c = m . getDeclaringClass ( ) ; if ( c . getSuperclass ( ) == null ) { return - 1 ; } // check directly implemented interfaces for the method being checked for ( Class < ? > i : c . getInterfaces ( ) ) { try { Method im = i . getMethod ( m . getName ( ) , m . getParameterTypes ( ) ) ; int d = getAnnotationDepth ( im , annotationClass ) ; if ( d > 0 ) { // since the annotation was on the interface, add 1 return d + 1 ; } } catch ( final SecurityException ex ) { continue ; } catch ( final NoSuchMethodException ex ) { continue ; } } try { int d = getAnnotationDepth ( c . getSuperclass ( ) . getMethod ( m . getName ( ) , m . getParameterTypes ( ) ) , annotationClass ) ; if ( d > 0 ) { // since the annotation was on the superclass, add 1 return d + 1 ; } return - 1 ; } catch ( final SecurityException ex ) { return - 1 ; } catch ( final NoSuchMethodException ex ) { return - 1 ; } }
Searches the class hierarchy to see if the method or it s super implementations and interfaces has the annotation . Returns the depth of the annotation in the hierarchy .