Here you can find the source of invokePublicMethod(Object o, String methodName, Class[] classes, Object[] objects)
public static void invokePublicMethod(Object o, String methodName, Class[] classes, Object[] objects)
//package com.java2s; //License from project: Apache License import java.io.PrintStream; import java.lang.reflect.Method; public class Main { public static Object invokePublicMethod(Object o, String methodName) { try {/*from w w w. j av a 2 s .c o m*/ Method m = o.getClass().getMethod(methodName, new Class[0]); m.setAccessible(true); return m.invoke(o, new Object[0]); } catch (Exception e) { e.printStackTrace(); } return null; } public static void invokePublicMethod(Object o, String methodName, Class[] classes, Object[] objects) { try { Method m = o.getClass().getMethod(methodName, classes); m.setAccessible(true); m.invoke(o, objects); } catch (Exception e) { e.printStackTrace(); } } public static Method getMethod(Class<?> c, String methodName, Class<?>... parameterTypes) { try { try { return c.getDeclaredMethod(methodName, parameterTypes); } catch (NoSuchMethodException ex) { Class<?> superclass = c.getSuperclass(); if (superclass != null) { return getMethod(superclass, methodName, parameterTypes); } return null; } } catch (RuntimeException ex) { throw ex; } catch (Exception ex) { // throw newstate ImplementationError(ex); throw new RuntimeException(""); } } public static void printStackTrace(PrintStream out, StackTraceElement[] stackTrace) { for (int i = 2; i < stackTrace.length; i++) { StackTraceElement stackTraceElement = stackTrace[i]; out.println("\tat " + stackTraceElement); } } public static void printStackTrace(StackTraceElement[] stackTrace) { printStackTrace(System.err, stackTrace); } /** * Returns the declared method in the specified class or any of its super * classes. * * @param c * @param methodName * @param types * @return * @throws NoSuchMethodException */ public static Method getDeclaredMethod(Class<?> c, String methodName, Class<?>[] types) throws NoSuchMethodException { Method m = null; while (m == null && c != null) { try { m = c.getDeclaredMethod(methodName, types); } catch (SecurityException e) { throw new RuntimeException(e); } catch (NoSuchMethodException e) { // nop } c = c.getSuperclass(); } if (m == null) { throw new NoSuchMethodException(); } return m; } }