Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

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

public class Main {
    /**
     * Call a private method of a given class object - with no parameters
     * 
     * @param objectInstance Class object to invoke method on
     * @param methodName Method name to invoke
     * @param inputVal
     * @return Return value from invoked method
     * 
     * @throws IllegalArgumentException
     * @throws NoSuchMethodException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    public static Object callPrivateMethod(Object objectInstance, String methodName)
            throws IllegalArgumentException, IllegalAccessException, InvocationTargetException,
            NoSuchMethodException // NOSONAR
    {
        Object[] params = null;

        Method method = objectInstance.getClass().getDeclaredMethod(methodName, (Class[]) null);

        method.setAccessible(true);

        return method.invoke(objectInstance, params);
    }

    /**
     * Call a private method of a given class object
     * 
     * @param objectInstance Class object to invoke method on
     * @param methodName Method name to invoke
     * @param int1 int Parameter to the method
     * @return Return value from invoked method
     * 
     * @throws NoSuchMethodException
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    public static Object callPrivateMethod(Object objectInstance, String methodName, int int1)
            throws NoSuchMethodException, IllegalArgumentException, IllegalAccessException,
            InvocationTargetException // NOSONAR
    {

        Method method = objectInstance.getClass().getDeclaredMethod(methodName, Integer.TYPE);

        method.setAccessible(true);

        return method.invoke(objectInstance, int1);
    }
}