Android中对于Java 反射(Reflect)语法的封装类

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/guolipeng_network/article/details/84774648

最近在研究Android插件化,通篇都在谈Hook,当然都是依赖Java的反射技术,然而反射获取构造函数,创建类的实例对象,反射调用方法,反射字段以及对泛型类的反射语法比较繁琐,步骤较多,开发成本较高,于是就自己封装了一个反射相关的工具类,方便在开发过程中对反射的使用。

package com.example.guolipeng.demo.utils;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * 反射语法的封装类
 * 1.反射出一个构造函数
 * 2.调用实例方法(普通方法和静态方法)
 * 3.获取并设置一个字段的值
 * 4.对泛型类的处理
 */
public class RefInvoke {

    public static Object createObject(String className) {
        Class[] paramTypes = new Class[]{};
        Object[] paramValues = new Object[]{};
        return createObject(className, paramTypes, paramValues);
    }

    public static Object createObject(String className, Class paramType, Object paramValue) {
        Class[] paramTypes = new Class[]{paramType};
        Object[] paramValues = new Object[]{paramValue};
        return createObject(className, paramTypes, paramValues);
    }

    /**
     * 反射创建类的实例对象
     *
     * @param className   类名
     * @param paramTypes  构造函数的参数类型数组
     * @param paramValues 构造函数的参数值数组
     * @return 对象实例
     */
    public static Object createObject(String className, Class[] paramTypes, Object[] paramValues) {
        try {
            Class<?> c = Class.forName(className);
            Constructor<?> constructor = c.getDeclaredConstructor(paramTypes);
            constructor.setAccessible(true);
            return constructor.newInstance(paramValues);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Object createObject(Class<?> c) {
        Class[] paramTypes = new Class[]{};
        Object[] paramValues = new Object[]{};
        return createObject(c, paramTypes, paramValues);
    }

    public static Object createObject(Class<?> c, Class paramType, Object paramValue) {
        Class[] paramTypes = new Class[]{paramType};
        Object[] paramValues = new Object[]{paramValue};
        return createObject(c, paramTypes, paramValues);
    }

    public static Object createObject(Class<?> c, Class[] paramTypes, Object[] paramValues) {
        try {
            Constructor<?> constructor = c.getDeclaredConstructor(paramTypes);
            constructor.setAccessible(true);
            return constructor.newInstance(paramValues);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Object invokeInstanceMethod(Object obj, String methodName) {
        Class[] paramTypes = new Class[]{};
        Object[] paramValues = new Object[]{};
        return invokeInstanceMethod(obj, methodName, paramTypes, paramValues);
    }

    public static Object invokeInstanceMethod(Object obj, String methodName, Class paramType, Object paramValue) {
        Class[] paramTypes = new Class[]{paramType};
        Object[] paramValues = new Object[]{paramValue};
        return invokeInstanceMethod(obj, methodName, paramTypes, paramValues);
    }

    /**
     * 调用实例方法
     *
     * @param obj         实例对象
     * @param methodName  方法名称
     * @param paramTypes  方法参数类型数组
     * @param paramValues 方法参数值数组
     * @return 方法返回值
     */
    public static Object invokeInstanceMethod(Object obj, String methodName, Class[] paramTypes, Object[] paramValues) {
        if (obj == null) {
            return null;
        }
        try {
            Method method = obj.getClass().getDeclaredMethod(methodName, paramTypes);
            method.setAccessible(true);
            return method.invoke(obj, paramValues);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Object invokeStaticMethod(String className, String methodName) {
        Class[] paramTypes = new Class[]{};
        Object[] paramValues = new Object[]{};
        return invokeStaticMethod(className, methodName, paramTypes, paramValues);
    }

    public static Object invokeStaticMethod(String className, String methodName, Class paramType, Object paramValue) {
        Class[] paramTypes = new Class[]{paramType};
        Object[] paramValues = new Object[]{paramValue};
        return invokeStaticMethod(className, methodName, paramTypes, paramValues);
    }

    /**
     * 调用静态方法
     *
     * @param className   类名
     * @param methodName  方法名
     * @param paramTypes  方法参数类型数组
     * @param paramValues 方法参数值数组
     * @return 方法返回值
     */
    public static Object invokeStaticMethod(String className, String methodName, Class[] paramTypes, Object paramValues) {
        try {
            Class<?> c = Class.forName(className);
            Method method = c.getDeclaredMethod(methodName, paramTypes);
            method.setAccessible(true);
            return method.invoke(null, paramValues);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Object invokeStaticMethod(Class<?> c, String methodName) {
        Class[] paramTypes = new Class[]{};
        Object[] paramValues = new Object[]{};
        return invokeStaticMethod(c, methodName, paramTypes, paramValues);
    }

    public static Object invokeStaticMethod(Class<?> c, String methodName, Class paramType, Object paramValue) {
        Class[] paramTypes = new Class[]{paramType};
        Object[] paramValues = new Object[]{paramValue};
        return invokeStaticMethod(c, methodName, paramTypes, paramValues);
    }

    public static Object invokeStaticMethod(Class<?> c, String methodName, Class[] paramTypes, Object paramValues) {
        try {
            Method method = c.getDeclaredMethod(methodName, paramTypes);
            method.setAccessible(true);
            return method.invoke(null, paramValues);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取字段的值
     *
     * @param className 类名
     * @param obj       实例对象
     * @param fieldName 字段名称
     * @return 字段值
     */
    public static Object getFieldObject(String className, Object obj, String fieldName) {
        try {
            Class<?> c = Class.forName(className);
            Field field = c.getDeclaredField(fieldName);
            field.setAccessible(true);
            return field.get(obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Object getFieldObject(Class<?> c, Object obj, String fieldName) {
        try {
            Field field = c.getDeclaredField(fieldName);
            field.setAccessible(true);
            return field.get(obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    // 简写
    public static Object getFieldObject(Object obj, String fieldName) {
        return getFieldObject(obj.getClass(), obj, fieldName);
    }

    public static Object getStaticFieldObject(String className, String fieldName) {
        return getFieldObject(className, null, fieldName);
    }

    public static Object getStaticFieldObject(Class<?> c, String fieldName) {
        return getFieldObject(c, null, fieldName);
    }

    /**
     * 设置一个字段的值
     *
     * @param className  类名
     * @param obj        实例对象
     * @param fieldName  字段名称
     * @param fieldValue 字段值
     */
    public static void setFieldObject(String className, Object obj, String fieldName, Object fieldValue) {
        Field field = (Field) getFieldObject(className, obj, fieldName);
        if (field != null) {
            try {
                field.set(obj, fieldValue);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    public static void setFieldObject(Class c, Object obj, String fieldName, Object fieldValue) {
        Field field = (Field) getFieldObject(c, obj, fieldName);
        if (field != null) {
            try {
                field.set(obj, fieldValue);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    // 简写
    public static void setFieldObject(Object obj, String fieldName, Object fieldValue) {
        setFieldObject(obj.getClass(), obj, fieldName, fieldValue);
    }

    public static void setStaticFieldObject(String className, String fieldName, Object fieldValue) {
        setFieldObject(className, null, fieldName, fieldValue);
    }

    public static void setStaticFieldObject(Class c, String filedName, Object filedValue) {
        setFieldObject(c, null, filedName, filedValue);
    }
}

展开阅读全文

没有更多推荐了,返回首页