Java Reflection Method Invoke invokeMethod(Object obj, String methodName, Object... params)

Here you can find the source of invokeMethod(Object obj, String methodName, Object... params)

Description

Get and invoke the target method from the given object with given parameters

License

Apache License

Parameter

Parameter Description
obj the object to get and invoke method from
methodName the name of the method to invoke
params the parameters for the method to invoke

Return

the return value of the method invocation

Declaration

public static Object invokeMethod(Object obj, String methodName, Object... params) 

Method Source Code

//package com.java2s;
/**/* w w  w .j a  va2s. c o m*/
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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

public class Main {
    /**
     * Get and invoke the target method from the given object with given parameters
     * @param obj the object to get and invoke method from
     * @param methodName the name of the method to invoke
     * @param params the parameters for the method to invoke
     * @return the return value of the method invocation
     */
    public static Object invokeMethod(Object obj, String methodName, Object... params) {
        Method m;
        try {
            m = obj.getClass().getMethod(methodName, getParameterTypes(params));
            m.setAccessible(true);
            return m.invoke(obj, params);
        } catch (NoSuchMethodException e) {
            throw new UnsupportedOperationException("Cannot find specified method " + methodName, e);
        } catch (IllegalAccessException e) {
            throw new UnsupportedOperationException("Unable to access specified method " + methodName, e);
        } catch (IllegalArgumentException e) {
            throw new UnsupportedOperationException("Illegal arguments supplied for method " + methodName, e);
        } catch (InvocationTargetException e) {
            throw new UnsupportedOperationException("Method threw an exception for " + methodName, e);
        }
    }

    private static Class<?>[] getParameterTypes(Object[] params) {
        Class<?>[] parameterTypes = new Class<?>[params.length];
        for (int i = 0; i < params.length; i++) {
            parameterTypes[i] = params[i].getClass();
        }
        return parameterTypes;
    }
}

Related

  1. invokeMethod(Object obj, String method)
  2. invokeMethod(Object obj, String method, Object... args)
  3. invokeMethod(Object obj, String methodName, Object... args)
  4. invokeMethod(Object obj, String methodName, Object... args)
  5. invokeMethod(Object obj, String methodname, Object... args)
  6. invokeMethod(Object obj, String methodName, Object[] params, Class[] paramTypes)
  7. invokeMethod(Object object, Method method, Object... args)
  8. invokeMethod(Object object, String methodName, Class[] parameterTypes, Object[] parameters)
  9. invokeMethod(Object object, String methodName, Class[] signature, Object[] parameters)