Java Reflection Method Invoke invokeMethod(Class clazz, String methodName, Object[] args)

Here you can find the source of invokeMethod(Class clazz, String methodName, Object[] args)

Description

invoke Method

License

Open Source License

Declaration

public static Object invokeMethod(Class clazz, String methodName,
            Object[] args) throws IllegalArgumentException,
            IllegalAccessException, InvocationTargetException,
            SecurityException, NoSuchMethodException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 IBM Corporation and others.
 * 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:/* w  w  w. j  a va  2  s.  c om*/
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

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

public class Main {
    public static Object invokeMethod(Class clazz, String methodName,
            Object[] args) throws IllegalArgumentException,
            IllegalAccessException, InvocationTargetException,
            SecurityException, NoSuchMethodException {
        return invokeMethod(clazz, null, methodName, args);
    }

    public static Object invokeMethod(Class clazz, Object target,
            String methodName, Object[] args)
            throws IllegalArgumentException, IllegalAccessException,
            InvocationTargetException, SecurityException,
            NoSuchMethodException {
        Class[] signature = new Class[args.length];
        for (int i = 0; i < args.length; i++) {
            Class thisClass = args[i].getClass();
            if (thisClass == Integer.class)
                signature[i] = int.class;
            else if (thisClass == Long.class)
                signature[i] = long.class;
            else if (thisClass == Byte.class)
                signature[i] = byte.class;
            else
                signature[i] = thisClass;
        }
        Method method = clazz.getMethod(methodName, signature);
        return method.invoke(target, args);
    }
}

Related

  1. invoke(MethodHandle methodHandle, Object... params)
  2. invokeJdbcMethod(Method method, Object target)
  3. invokeJdbcMethod(Method method, Object target)
  4. invokeMethod(Class clazz, Object classObject, String methodName, Class[] paramTypes, Object... args)
  5. invokeMethod(Class clazz, Object obj, String methodName, Class[] parametersTypes, Object[] parameters)
  6. invokeMethod(Class targetClass, Object obj, String methodName, Object arg)
  7. invokeMethod(Class clazz, E instance, String[] names, Object... args)
  8. invokeMethod(Class clazz, Object object, String methodName, Class[] parameterTypes, Object[] parameters)
  9. invokeMethod(Class clazz, String method, Class[] args, Object object, Object[] objects)