Java Reflection Method Invoke invokeMethod(Object o, String methodName, Object... args)

Here you can find the source of invokeMethod(Object o, String methodName, Object... args)

Description

Invokes method on object.

License

Open Source License

Parameter

Parameter Description
o The object that contains the field
methodName The name of the field
args The arguments.

Return

Result of method.

Declaration

public static Object invokeMethod(Object o, String methodName, Object... args) throws Throwable 

Method Source Code

//package com.java2s;
/*//from  ww  w  .ja va2 s. co m
 *
 *  Copyright 2016,2017 DTCC, Fujitsu Australia Software Technology, IBM - All Rights Reserved.
 *
 *  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.
 *
 */

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

import java.util.ArrayList;
import java.util.Arrays;

import java.util.Iterator;
import java.util.List;

public class Main {
    /**
     * Invokes method on object.
     * Used to access private methods.
     *
     * @param o          The object that contains the field
     * @param methodName The name of the field
     * @param args       The arguments.
     * @return Result of method.
     */
    public static Object invokeMethod(Object o, String methodName, Object... args) throws Throwable {

        Method[] methods = o.getClass().getDeclaredMethods();
        List<Method> reduce = new ArrayList<>(Arrays.asList(methods));
        for (Iterator<Method> i = reduce.iterator(); i.hasNext();) {
            Method m = i.next();
            if (!methodName.equals(m.getName())) {
                i.remove();
                continue;
            }
            Class<?>[] parameterTypes = m.getParameterTypes();
            if (parameterTypes.length != args.length) {
                i.remove();
                continue;
            }
        }
        if (reduce.isEmpty()) {
            throw new RuntimeException(String.format("TEST ISSUE Could not find method %s on %s with %d arguments.",
                    methodName, o.getClass().getName(), args.length));
        }
        if (reduce.size() > 1) {
            throw new RuntimeException(
                    String.format("TEST ISSUE Could not find unique method %s on %s. Found with %d matches.",
                            methodName, o.getClass().getName(), reduce.size()));
        }

        Method method = reduce.iterator().next();
        method.setAccessible(true);
        try {
            return method.invoke(o, args);
        } catch (IllegalAccessException e) {
            throw e;
        } catch (InvocationTargetException e) {
            throw e.getTargetException();
        }

    }
}

Related

  1. invokeMethod(Object destination, String methodName, Object argument)
  2. invokeMethod(Object handle, String methodName, Class[] parameterClasses, Object... args)
  3. invokeMethod(Object handler, String strMethod, Class[] cls, Object... params)
  4. invokeMethod(Object instance, String methodName, Class expectedReturnType)
  5. invokeMethod(Object o, String fieldName)
  6. invokeMethod(Object o, String methodName, Object[] params)
  7. invokeMethod(Object obj, Class type, String name, Class[] parameterTypes, Object[] parameters)
  8. invokeMethod(Object obj, Method method, Object... args)
  9. invokeMethod(Object obj, String mehtodName, Object... parameter)