Invoke a method on an invoking object with the specified arguments. - Java Reflection

Java examples for Reflection:Invoke

Description

Invoke a method on an invoking object with the specified arguments.

Demo Code


//package com.java2s;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Main {
    public static void main(String[] argv) throws Exception {
        Object invokingObject = "java2s.com";
        String methodNameToInvoke = "java2s.com";
        Object args = "java2s.com";
        System.out.println(invokePrivateMethod(invokingObject,
                methodNameToInvoke, args));
    }/*from  w  ww  .  j  ava  2  s  .c  o m*/

    /**
     * Invoke a method on an invoking object with the specified arguments. I.e.,
     * ((type)invokingObject).methodNameToInvoke (arguments);
     * 
     * @param type
     *            allows method calls based on a supertype of invokingObject
     * @param invokingObject
     *            may be null if calling a static method
     * @param methodNameToInvoke
     * @param args
     * @return the result from invoking the method or null if unsuccessful
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     * @throws NoSuchMethodException
     */
    public static Object invokePrivateMethod(Class<?> type,
            Object invokingObject, String methodNameToInvoke,
            Object... args) throws IllegalArgumentException,
            IllegalAccessException, InvocationTargetException,
            NoSuchMethodException {
        Method[] methods = type.getDeclaredMethods();
        for (Method method : methods) {
            String methodName = method.getName();
            if (methodName.equals(methodNameToInvoke)) {
                Object result = method.invoke(invokingObject, args);
                return (result);
            }
        }
        throw new NoSuchMethodException("Method " + methodNameToInvoke
                + " not found.");
    }

    /**
     * Invoke a method on an invoking object with the specified arguments. I.e.,
     * invokingObject.methodNameToInvoke (arguments);
     * 
     * @param invokingObject
     *            may be null if calling a static method
     * @param methodNameToInvoke
     * @param args
     * @return the result from invoking the method or null if unsuccessful
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     * @throws NoSuchMethodException
     */
    public static Object invokePrivateMethod(Object invokingObject,
            String methodNameToInvoke, Object... args)
            throws IllegalArgumentException, IllegalAccessException,
            InvocationTargetException, NoSuchMethodException {
        Class<?> type = invokingObject.getClass();
        Object result = invokePrivateMethod(type, invokingObject,
                methodNameToInvoke, args);
        return (result);
    }
}

Related Tutorials