public static Method [ ] getUniqueDeclaredMethods ( Class < ? > leafClass ) throws IllegalArgumentException { final List < Method > methods = new ArrayList < Method > ( 32 ) ; doWithMethods ( leafClass , new MethodCallback ( ) { @ Override public void doWith ( Method method ) { boolean knownSignature = false ; Method methodBeingOverriddenWithCovariantReturnType = null ; for ( Method existingMethod : methods ) { if ( method . getName ( ) . equals ( existingMethod . getName ( ) ) && Arrays . equals ( method . getParameterTypes ( ) , existingMethod . getParameterTypes ( ) ) ) { // Is this a covariant return type situation? if ( existingMethod . getReturnType ( ) != method . getReturnType ( ) && existingMethod . getReturnType ( ) . isAssignableFrom ( method . getReturnType ( ) ) ) { methodBeingOverriddenWithCovariantReturnType = existingMethod ; } else { knownSignature = true ; } break ; } } if ( methodBeingOverriddenWithCovariantReturnType != null ) { methods . remove ( methodBeingOverriddenWithCovariantReturnType ) ; } if ( ! knownSignature ) { methods . add ( method ) ; } } } ) ; return methods . toArray ( new Method [ methods . size ( ) ] ) ; }
Get the unique set of declared methods on the leaf class and all superclasses . Leaf class methods are included first and while traversing the superclass hierarchy any methods found with signatures matching a method already included are filtered out .