Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.lang.reflect.Method;

public class Main {
    public static Object invokeMethod(Object object, String methodName) {
        if (null != object) {
            try {
                Class clz = (object instanceof Class ? (Class) object : object.getClass());
                Method method = getMethod(clz, methodName);
                return method.invoke(object);
            } catch (Exception e) {
            }
        }
        return null;
    }

    public static Object invokeMethod(Object object, String methodName, Class param0, Object arg0) {
        if (null != object) {
            try {
                Class clz = (object instanceof Class ? (Class) object : object.getClass());
                Method method = getMethod(clz, methodName, param0);
                return method.invoke(object, arg0);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static Object invokeMethod(Object object, String methodName, Class param0, Class param1, Object arg0,
            Object arg1) throws Exception {
        if (null != object) {
            Class clz = (object instanceof Class ? (Class) object : object.getClass());
            Method method = getMethod(clz, methodName, param0, param1);
            return method.invoke(object, arg0, arg1);
        }
        return null;
    }

    public static Object invokeMethod(Object object, String methodName, Class param0, Class param1, Class param2,
            Object arg0, Object arg1, Object arg2) {
        if (null != object) {
            try {
                Class clz = (object instanceof Class ? (Class) object : object.getClass());
                Method method = getMethod(clz, methodName, param0, param1, param2);
                return method.invoke(object, arg0, arg1, arg2);
            } catch (Exception e) {
            }
        }
        return null;
    }

    public static Object invokeMethod(Object object, String methodName, Class param0, Class param1, Class param2,
            Class param3, Object arg0, Object arg1, Object arg2, Object arg3) {
        if (null != object) {
            try {
                Class clz = (object instanceof Class ? (Class) object : object.getClass());
                Method method = getMethod(clz, methodName, param0, param1, param2, param3);
                return method.invoke(object, arg0, arg1, arg2, arg3);
            } catch (Exception e) {
            }
        }
        return null;
    }

    public static Object invokeMethod(Object object, String methodName, Class param0, Class param1, Class param2,
            Class param3, Class param4, Object arg0, Object arg1, Object arg2, Object arg3, Object arg4) {
        if (null != object) {
            try {
                Class clz = (object instanceof Class ? (Class) object : object.getClass());
                Method method = getMethod(clz, methodName, param0, param1, param2, param3, param4);
                return method.invoke(object, arg0, arg1, arg2, arg3, arg4);
            } catch (Exception e) {
            }
        }
        return null;
    }

    /**
     * @param clz class name
     * @param methodName method name
     * @return true if the method is in the class, else false
     */
    public static Method getMethod(Class clz, String methodName, Class... parameterTypes) {
        if (null != clz) {
            try {
                return clz.getMethod(methodName, parameterTypes);
            } catch (Exception e) {
            }
            return (clz == Object.class ? null : getMethod(clz.getSuperclass(), methodName, parameterTypes));
        }
        return null;
    }
}