public static Object callMethod ( Object o , String method , Object [ ] params ) { // Get the objects class. Class cls = o . getClass ( ) ; // Get the classes of the parameters. Class [ ] paramClasses = new Class [ params . length ] ; for ( int i = 0 ; i < params . length ; i ++ ) { paramClasses [ i ] = params [ i ] . getClass ( ) ; } try { // Try to find the matching method on the class. Method m = cls . getMethod ( method , paramClasses ) ; // Invoke it with the parameters. return m . invoke ( o , params ) ; } catch ( NoSuchMethodException e ) { throw new IllegalStateException ( e ) ; } catch ( IllegalAccessException e ) { throw new IllegalStateException ( e ) ; } catch ( InvocationTargetException e ) { throw new IllegalStateException ( e ) ; } }
Calls a named method on an object with a specified set of parameters .