static Method getPublicMethod ( Class pClass , Method pMethod ) { // See if this is a public class declaring the method if ( Modifier . isPublic ( pClass . getModifiers ( ) ) ) { try { Method m ; try { m = pClass . getDeclaredMethod ( pMethod . getName ( ) , pMethod . getParameterTypes ( ) ) ; } catch ( java . security . AccessControlException ex ) { // kludge to accommodate J2EE RI's default settings // TODO: see if we can simply replace // getDeclaredMethod() with getMethod() ...? m = pClass . getMethod ( pMethod . getName ( ) , pMethod . getParameterTypes ( ) ) ; } if ( Modifier . isPublic ( m . getModifiers ( ) ) ) { return m ; } } catch ( NoSuchMethodException exc ) { } } // Search the interfaces { Class [ ] interfaces = pClass . getInterfaces ( ) ; if ( interfaces != null ) { for ( int i = 0 ; i < interfaces . length ; i ++ ) { Method m = getPublicMethod ( interfaces [ i ] , pMethod ) ; if ( m != null ) { return m ; } } } } // Search the superclass { Class superclass = pClass . getSuperclass ( ) ; if ( superclass != null ) { Method m = getPublicMethod ( superclass , pMethod ) ; if ( m != null ) { return m ; } } } return null ; }
If the given class is public and has a Method that declares the same name and arguments as the given method then that method is returned . Otherwise the superclass and interfaces are searched recursively .