Java Reflection Method Invoke invokeMethod(Object caller, Object classInstance, String className, String methodName, String parameterClasses[], Object[] parameters)

Here you can find the source of invokeMethod(Object caller, Object classInstance, String className, String methodName, String parameterClasses[], Object[] parameters)

Description

Invokes/call a method.

License

Open Source License

Parameter

Parameter Description
caller a parameter
classInstance a parameter
className a parameter
methodName a parameter
parameterClasses a parameter
parameters a parameter

Return

The result of the called method or null (if the call returns null or if the method could not be called).

Declaration

public static Object invokeMethod(Object caller, Object classInstance, String className, String methodName,
        String parameterClasses[], Object[] parameters) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2008 Olivier Moises//from ww  w  .j a  v a2 s  . com
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Olivier Moises- initial API and implementation
 *******************************************************************************/

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Main {
    /**
     * Invokes/call a method.
     * 
     * @param caller
     * @param classInstance
     * @param className
     * @param methodName
     * @param parameterClasses
     * @param parameters
     * @return The result of the called method or null (if the call returns null
     *         or if the method could not be called).
     * 
     */
    public static Object invokeMethod(Object caller, Object classInstance, String className, String methodName,
            String parameterClasses[], Object[] parameters) {
        Object result = null;
        try {
            Class<?> clazz = caller.getClass().getClassLoader().loadClass(className);
            Class<?> paramClasses[] = new Class<?>[0];
            if (parameterClasses != null && parameterClasses.length > 0) {
                paramClasses = new Class<?>[parameterClasses.length];
                for (int i = 0; i < parameterClasses.length; i++)
                    paramClasses[i] = caller.getClass().getClassLoader().loadClass(parameterClasses[i]);
            }
            if (parameters == null)
                parameters = new Object[0];
            Method method = clazz.getMethod(methodName, paramClasses);
            result = method.invoke(classInstance, parameters);
        } catch (ClassNotFoundException e) {
            // NOTHING TO DO HERE, NO PROBS
        } catch (SecurityException e) {
            // when launched in a non OSGI context, the SecurityException means
            // that the platform is not running
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            // NOTHING TO DO HERE, NO PROBS
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return result;
    }
}

Related

  1. invokeMethod(Method method, Object target, Object[] args)
  2. invokeMethod(Method method, Object theObject, Object[] args)
  3. invokeMethod(Method method, Object... arguments)
  4. invokeMethod(Object bean, Method method, Object... param)
  5. invokeMethod(Object bean, Method method, Object[] args)
  6. invokeMethod(Object caller, String methodName, Object[] params)
  7. invokeMethod(Object classInstance, Method method, Object... args)
  8. invokeMethod(Object cls, String methodName, Class paramClass, String paramValue)
  9. invokeMethod(Object destination, String methodName, Object argument)