Java Reflection Method Invoke invokeMethod(Class clazz, Object object, String methodName, Class[] parameterTypes, Object[] parameters)

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

Description

invoke Method

License

Open Source License

Declaration

public static Object invokeMethod(Class<?> clazz, Object object, String methodName, Class<?>[] parameterTypes,
            Object[] parameters) 

Method Source Code

//package com.java2s;
/*/*from  w  w  w.ja  va2 s  .  c om*/
 * Copyright 2011-2016 ZXC.com All right reserved. This software is the confidential and proprietary information of
 * ZXC.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into with ZXC.com.
 */

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

public class Main {
    public static Object invokeMethod(Class<?> clazz, Object object, String methodName, Class<?>[] parameterTypes,
            Object[] parameters) {
        Method method = getDeclaredMethod(clazz, methodName, parameterTypes);
        if (method == null) {
            throw new RuntimeException("Method `" + methodName + "` not found in class `" + clazz.getName() + "`.");
        }
        if (!method.isAccessible()) {
            method.setAccessible(true);
        }
        try {
            return method.invoke(object, parameters);
        } catch (IllegalArgumentException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }

    public static Method getDeclaredMethod(Class<?> clazz, String methodName) {
        try {
            Method foundMethod = null;
            Method[] methods = clazz.getDeclaredMethods();
            for (Method m : methods) {
                if (methodName.equals(m.getName())) {
                    if (foundMethod != null) {
                        throw new RuntimeException("Found two method named: " + methodName + " in class: " + clazz);
                    }
                    foundMethod = m;
                }
            }
            if (foundMethod == null) {
                throw new NoSuchMethodException("Cannot find method named: " + methodName + " in class: " + clazz);
            }
            return foundMethod;
        } catch (SecurityException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            if (clazz == Object.class) {
                return null;
            } else {
                return getDeclaredMethod(clazz.getSuperclass(), methodName);
            }
        }
    }

    public static Method getDeclaredMethod(Class<?> clazz, String methodName, Class<?>[] parameterTypes) {
        try {
            Method method = clazz.getDeclaredMethod(methodName, parameterTypes);
            return method;
        } catch (SecurityException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            if (clazz == Object.class) {
                return null;
            } else {
                return getDeclaredMethod(clazz.getSuperclass(), methodName, parameterTypes);
            }
        }
    }
}

Related

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