java反射机制调用方法及简易封装工具类


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

public class Test {

    public static void main(String[] args) {
        try {
            Class clazz = Class.forName("com.lgz.Test");
            Method method = clazz.getMethod("run");
            method.invoke(clazz.newInstance());

            Method methodPrivate = clazz.getDeclaredMethod("eat");
            methodPrivate.setAccessible(true);//调private方法
            methodPrivate.invoke(clazz.newInstance());

            Method methodStatic = clazz.getMethod("work");
            methodStatic.invoke(null);

            Method method1 = clazz.getMethod("speak", int.class, String.class);
            method1.invoke(clazz.newInstance(), 22, "小明");
        } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
            e.printStackTrace();
        }

    }

    public static void work() {
        System.out.println("work() 方法被调用...");
    }

    private void eat() {
        System.out.println("eat() 方法被调用...");
    }

    public void run() {
        System.out.println("run() 方法被调用...");
    }

    public void speak(int age, String name) {
        System.out.println("speak() 方法被调用.... age = " + age + " name= " + name);
    }
}


工具类:

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


public class RefUtil {


    public static  T execute(Class clazz, Object obj, String methodName) {
        try {
            Method method = clazz.getMethod(methodName);
            return method == null ? null : (T) method.invoke(obj);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static  T executePrivate(Class clazz, Object obj, String methodName) {
        try {
            Method methodPrivate = clazz.getDeclaredMethod(methodName);
            methodPrivate.isAccessible();
            methodPrivate.setAccessible(true);//调private方法
            return (T) methodPrivate.invoke(obj);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
            return null;
        }

    }


    public static  T execute(Class objclass, Object obj, String methodName, T value) {
        try {
            if (value == null) return null;
            Method func = objclass.getMethod(methodName, value.getClass());
            if (func != null) {
                return (T) func.invoke(obj, value);
            } else {
                return null;
            }
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static  T executeInt(Class objclass, Object obj, String methodName, int value) {
        try {
            Method func = objclass.getMethod(methodName, int.class);
            Test.p(func.getClass());
            return (T) func.invoke(obj, value);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static  T executeDouble(Class objclass, Object obj, String methodName, double value) {
        try {
            Method func = objclass.getMethod(methodName, double.class);
            return (T) func.invoke(obj, value);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static  T executeFloat(Class objclass, Object obj, String methodName, float value) {
        try {
            Method func = objclass.getMethod(methodName, float.class);
            return (T) func.invoke(obj, value);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
            return null;
        }
    }


    public static  T executeLong(Class objclass, Object obj, String methodName, long value) {
        try {
            Method func = objclass.getMethod(methodName, long.class);
            return (T) func.invoke(obj, value);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
            return null;
        }
    }


}

你可能感兴趣的:(java)