Here you can find the source of invokeMethod(Object object, String methodName, Object[] params, Object[] result)
public static boolean invokeMethod(Object object, String methodName, Object[] params, Object[] result)
//package com.java2s; /*/*from w w w .ja v a2s. c om*/ Copyright 2009 Semantic Discovery, Inc. (www.semanticdiscovery.com) This file is part of the Semantic Discovery Toolkit. The Semantic Discovery Toolkit is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. The Semantic Discovery Toolkit is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with The Semantic Discovery Toolkit. If not, see <http://www.gnu.org/licenses/>. */ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class Main { /** * Find and invoke the method on the object if it exists. * <p> * If result is an array of size 1, then return the return value from * invoking the method in result[0]. Note that if the method returns * "void", the result object (result[0]) will be null. * * @return true if method was found and invoked; otherwise, false. */ public static boolean invokeMethod(Object object, String methodName, Object[] params, Object[] result) { if (object == null) return false; boolean retval = false; Method method = null; Class<?>[] paramTypes = params == null ? null : new Class<?>[params.length]; if (params != null) { for (int i = 0; i < params.length; ++i) { paramTypes[i] = params[i] == null ? null : params[i].getClass(); } } final Class<?> theClass = object.getClass(); try { method = theClass.getMethod(methodName, paramTypes); } catch (NoSuchMethodException nsme) { // just return false } catch (SecurityException se) { // rethrow throw new IllegalArgumentException(theClass.getName() + "." + methodName, se); } // invoke if (method != null) { try { final Object invocationResult = method.invoke(object, params); if (result != null && result.length > 0) result[0] = invocationResult; retval = true; } catch (IllegalAccessException iae) { throw new IllegalArgumentException(theClass.getName() + "." + methodName, iae); } catch (InvocationTargetException ite) { throw new IllegalArgumentException(theClass.getName() + "." + methodName, ite); } catch (ExceptionInInitializerError eiie) { throw new IllegalArgumentException(theClass.getName() + "." + methodName, eiie); } } return retval; } /** * Get the class with the given classname; */ public static Class getClass(String classname) { Class result = null; try { result = Class.forName(classname); } catch (ClassNotFoundException e) { throw new IllegalArgumentException(e); } return result; } }