Java Reflection Method Invoke invokeMethod(Object caller, String methodName, Object[] params)

Here you can find the source of invokeMethod(Object caller, String methodName, Object[] params)

Description

invoke method and returns it result(s)

License

Open Source License

Exception

Parameter Description
NoSuchMethodException an exception
SecurityException an exception
InvocationTargetException an exception
IllegalAccessException an exception
IllegalArgumentException an exception

Declaration

public static final Object invokeMethod(Object caller, String methodName, Object[] params)
        throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException,
        NoSuchMethodException 

Method Source Code

//package com.java2s;
/**//from  w w  w  . j  av a  2  s.c o  m
 * File Name   : BeanUtil.java
 * Author      : Administrator:Adelwin
 * Create Date   : Mar 13, 2006:1:22:45 AM
 *
 * Copyright (c) 2006 Adelwin. All Rights Reserved. <BR>
 * <BR>
 * This software contains confidential and proprietary information of
 * Adelwin. ("Confidential Information").<BR>
 * <BR>
 * Such Confidential Information shall not be disclosed and it shall
 * only be used in accordance with the terms of the license agreement
 * entered into with Solveware Independent; other than in accordance with the written
 * permission of Solveware Independent. <BR>
 *
 *
 */

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

public class Main {
    /**
     * invoke method and returns it result(s)
     * @throws NoSuchMethodException
     * @throws SecurityException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     * @throws IllegalArgumentException
     */
    public static final Object invokeMethod(Object caller, String methodName, Object[] params)
            throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException,
            NoSuchMethodException {
        //      Class clazz = caller.getClass();
        Method method = null;
        Class<?>[] paramTypes = (params == null ? null : getObjectTypes(params));
        method = findMethod(caller.getClass(), methodName, paramTypes);
        return (method == null ? null : method.invoke(caller, params));
    }

    public static Class<?>[] getObjectTypes(Object[] objs) {
        Class<?>[] objTypes = new Class[objs.length];
        for (int i = 0; i < objs.length; i++) {
            objTypes[i] = objs[i].getClass();
        }
        return objTypes;
    }

    public static final Method findMethod(Class<?> clazz, String methodName, Class<?>[] paramTypes) {
        Method method = null;
        try {
            method = clazz.getDeclaredMethod(methodName, paramTypes);
            return method;
        } catch (NoSuchMethodException e) {
            return findMethod(clazz.getSuperclass(), methodName, paramTypes);
        } catch (NullPointerException e) {
            return null;
        }
    }
}

Related

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