Java Reflection Method Invoke invokeMethod(String className, String method, Class[] paramTypes, Object obj, Object[] args)

Here you can find the source of invokeMethod(String className, String method, Class[] paramTypes, Object obj, Object[] args)

Description

invoke Method

License

Open Source License

Declaration

public static Object invokeMethod(String className, String method, Class[] paramTypes, Object obj,
            Object[] args) throws Exception 

Method Source Code

//package com.java2s;
/*/*  w  w w.  j av a 2  s  . c  om*/
 * Ulm University DUCKS project
 * 
 * Author:      Elmar Schoch <elmar.schoch@uni-ulm.de>
 * 
 * (C) Copyright 2006, Ulm University, all rights reserved.
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program 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 General Public License for more details.
 * 
 */

import java.lang.reflect.Method;

public class Main {
    public static Object invokeMethod(String className, String method, Class[] paramTypes, Object obj,
            Object[] args) throws Exception {

        Object result = null;
        try {
            Class cls = Class.forName(className);
            Method m = cls.getMethod(method, paramTypes);
            result = m.invoke(obj, args);
        } catch (Exception e) {
            throw new Exception("Method invoke failed: Class: " + className + " Method: " + method);
        }
        return result;
    }
}

Related

  1. invokeMethod(Object target, String name, Class... parameterTypes)
  2. invokeMethod(Object target, String name, Object[] args, Class[] argTypes)
  3. invokeMethod(Object target, String signature, Object... args)
  4. invokeMethod(Object target, String thisMethod, Object value)
  5. invokeMethod(Object theObject, String methodName, Object... parametersObject)
  6. invokeMethod(String methodName, Object gameCommand)
  7. invokeMethod(String name, Object target)
  8. invokeMethod2(Class cls, Object obj, String methodName, Object[] args)
  9. invokeMethodAndGet(Object object, String methodName, Object... args)