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

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

Description

Invoke a non-public static method by name from a class.

License

Apache License

Parameter

Parameter Description
clz The class.
methodName The static method name to be invoked.
params Optional parameters.

Exception

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

Return

Optional result object, or Void.

Declaration

public static Object invokeMethod(Class<?> clz, String methodName, Object... params)
        throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException,
        InvocationTargetException 

Method Source Code

//package com.java2s;
/*   Copyright (c) 2015 Magnet Systems, Inc.
 *
 *  Licensed 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./*from  www  .  j av a  2s .c  om*/
 */

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

public class Main {
    /**
     * Invoke a non-public method from an object.  To invoke a static non-public
     * method, <code>obj</code> is ignored.  This invocation may fail if 
     * SecurityManager is used to enforce access permission.
     * @param obj The object to be used, or null
     * @param method A Method object with the matching parameter types.
     * @param params Optional parameters.
     * @return Optional result object, or Void.
     * @throws IllegalAccessException
     * @throws IllegalArgumentException
     * @throws InvocationTargetException
     */
    public static Object invokeMethod(Object obj, Method method, Object... params)
            throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        boolean accessible = true;
        try {
            if (!(accessible = method.isAccessible())) {
                method.setAccessible(true);
            }
            return method.invoke(obj, params);
        } finally {
            if ((method != null) && !accessible) {
                method.setAccessible(false);
            }
        }
    }

    /**
     * Invoke a non-public static method by name from a class.  This invocation
     * may faile if SecurityManager is used to enforce access permission.
     * @param clz The class.
     * @param methodName The static method name to be invoked.
     * @param params Optional parameters.
     * @return Optional result object, or Void.
     * @throws NoSuchMethodException
     * @throws SecurityException
     * @throws IllegalAccessException
     * @throws IllegalArgumentException
     * @throws InvocationTargetException
     */
    public static Object invokeMethod(Class<?> clz, String methodName, Object... params)
            throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException,
            InvocationTargetException {
        Method method = getMethod(clz, methodName, params);
        return invokeMethod(null, method, params);
    }

    /**
     * Invoke a non-public method by name from an object.  This invocation may
     * fail if SecurityManager is used to enforce access permission.
     * @param obj The object to be used.
     * @param methodName A method name to be invoked.
     * @param params Optional parameters
     * @return Optional result object, or Void.
     * @throws NoSuchMethodException
     * @throws SecurityException
     * @throws IllegalAccessException
     * @throws IllegalArgumentException
     * @throws InvocationTargetException
     */
    public static Object invokeMethod(Object obj, String methodName, Object... params) throws NoSuchMethodException,
            SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        Method method = getMethod(obj, methodName, params);
        return invokeMethod(obj, method, params);
    }

    /**
     * Get a non-public (i.e. declared) static method from a class.
     * @param clz The class.
     * @param methodName A static method name.
     * @param params Optional parameters.
     * @return A Method object.
     * @throws NoSuchMethodException
     * @throws SecurityException
     */
    public static Method getMethod(Class<?> clz, String methodName, Object... params)
            throws NoSuchMethodException, SecurityException {
        if (params == null) {
            return clz.getMethod(methodName);
        } else {
            int i = 0;
            Class<?>[] paramClasses = new Class<?>[params.length];
            for (Object param : params) {
                paramClasses[i++] = param.getClass();
            }
            return clz.getDeclaredMethod(methodName, paramClasses);
        }
    }

    /**
     * Get a non-public (i.e. declared) method from an object.
     * @param obj The object to be used.
     * @param methodName A method name.
     * @param params Optional parameters.
     * @return A Method object.
     * @throws NoSuchMethodException
     * @throws SecurityException
     */
    public static Method getMethod(Object obj, String methodName, Object... params)
            throws NoSuchMethodException, SecurityException {
        if (params == null) {
            return obj.getClass().getMethod(methodName);
        } else {
            int i = 0;
            Class<?>[] paramClasses = new Class<?>[params.length];
            for (Object param : params) {
                paramClasses[i++] = param.getClass();
            }
            return obj.getClass().getDeclaredMethod(methodName, paramClasses);
        }
    }

    /**
     * Get a non-public (i.e. declared) static method from a class.
     * @param clz The class.
     * @param methodName A method name.
     * @param paramClasses Classes of the optional parameters.
     * @return A Method object.
     * @throws NoSuchMethodException
     * @throws SecurityException
     */
    public static Method getMethod(Class<?> clz, String methodName, Class<?>... paramClasses)
            throws NoSuchMethodException, SecurityException {
        if (paramClasses == null) {
            return clz.getMethod(methodName);
        } else {
            return clz.getDeclaredMethod(methodName, paramClasses);
        }
    }

    /**
     * Get a non-public (i.e. declared) method from an object.
     * @param obj The object to be used.
     * @param methodName A method name.
     * @param paramClasses Classes of the optional parameters.
     * @return A Method object.
     * @throws NoSuchMethodException
     * @throws SecurityException
     */
    public static Method getMethod(Object obj, String methodName, Class<?>... paramClasses)
            throws NoSuchMethodException, SecurityException {
        if (paramClasses == null) {
            return obj.getClass().getMethod(methodName);
        } else {
            return obj.getClass().getDeclaredMethod(methodName, paramClasses);
        }
    }
}

Related

  1. invokeMethod(Class clazz, String methodName, Object[] args)
  2. invokeMethod(Class targetClass, Object obj, String methodName, Object arg)
  3. invokeMethod(Class clazz, E instance, String[] names, Object... args)
  4. invokeMethod(Class clazz, Object object, String methodName, Class[] parameterTypes, Object[] parameters)
  5. invokeMethod(Class clazz, String method, Class[] args, Object object, Object[] objects)
  6. invokeMethod(Class returnClass, String methodName, Object ivokeObject, Object... objects)
  7. invokeMethod(final Method method, final Object instance, final Object... args)
  8. invokeMethod(final Method method, final Object object)
  9. invokeMethod(final Method method, final Object object, final Object[] args)