Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.lang.reflect.Method;

public class Main {
    public static Object invokeMethod(String className, String methodName, Class[] cls, Object[] args)
            throws Exception {
        Class clazz = Class.forName(className);
        return invokeMethod(clazz, null, methodName, cls, args);
    }

    public static Object invokeMethod(Object object, String methodName, Class[] cls, Object[] args)
            throws Exception {
        Class clazz = object.getClass();
        return invokeMethod(clazz, object, methodName, cls, args);
    }

    public static Object invokeMethod(Object object, String methodName) throws Exception {
        Class clazz = object.getClass();
        return invokeMethod(clazz, object, methodName, null, null);
    }

    public static Object invokeMethod(Class clazz, Object object, String methodName, Class[] cls, Object[] args)
            throws Exception {
        Method method;
        if (null == cls) {
            method = clazz.getDeclaredMethod(methodName);
        } else {
            method = clazz.getDeclaredMethod(methodName, cls);
        }

        method.setAccessible(true);

        if (null == args) {
            return method.invoke(object);
        } else {
            return method.invoke(object, args);
        }
    }
}