• 学习反射 并尝试写一个反射的工具类


     萌新一枚 , 好久之前就听说过反射这种东西 ,但是真正也就像, 熟练的掌握Windows,Linux开关机一样, 也只是听过而已,

    所以呢, 翻翻一些资料 ,查看了一下反射的一些常用方法:

    反射在线文档

    不过呢,如果只是看这个还是知之甚少 ,所以呢, 干脆静下心来 ,去写一个工具类, 以总结自己所学到的反射的一些技巧:

    个人感觉:反射目前对于我来说的最主要的几个功能就是 ,

    1.获取私有构造器创建的对象.

    2.获取私有的方法并使用.

    3.获取私有的属性值.

    4.获取私有的常量值.

    所以下面写的一个工具类也基本上就是围绕上面的来的,至于其它一些姑且就是锦上添花当一个点缀罢了.

    /**
     * 
     */
    package JavaReflex;
    
    import java.lang.reflect.Constructor;
    import java.lang.reflect.Field;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * 制作一个反射的工具类 , (无论是private还是public)
     * 可以在输入方法名或者变量名的时候
     * 调用方法, 或者吧变量的值都输出来
     * 
     * bug,如果方法不存在,或者属性不存在会直接抛异常
     * @file ReflexUtils.java
     * @author tangweiming
     * @since 2018年7月18日
     */
    public class ReflexUtils {
        
        /**
         * 构造方法Util
         * @param clazz
         * @param constructorType
         * @param constructorValue
         * @param returnClass
         * @return
         * @author tangweiming
         * @since 2018年7月18日
         */
        public static <T> T getInstace(Class clazz, Class[] constructorType,
                Object[] constructorValue, Class<T> returnClass) {
            T constructor = null;
            try {
                Constructor con = clazz.getDeclaredConstructor(constructorType);
                con.setAccessible(true);
                constructor = (T) con.newInstance(constructorValue);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            } catch (SecurityException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            } catch (InstantiationException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
            return constructor;
        }
    
        /**
         * 方法Util
         * 类名 方法名 参数类型 参数 
         * return 返回值
         */
    
        public static Object useMethod(Class clazz, String methodName) {
            return useMethod(clazz, methodName, null, null, null, null);
        }
    
        public static Object useMethod(Class clazz, String methodName,
                Class[] constructorType, Object[] constructorValue) {
            return useMethod(clazz, methodName, constructorType, constructorValue,
                    null, null);
        }
    
        /**
         * 下面还可以优化一下, 反射有队方法返回的数据类型进行判断 , 这边可以修改为泛型返回所需要的类型
         * @param clazz
         * @param methodName
         * @param constructorType
         * @param constructorValue
         * @param methodType
         * @param methodValue
         * @return
         * @author tangweiming
         * @since 2018年7月18日
         */
        public static Object useMethod(Class clazz, String methodName,
                Class[] constructorType, Object[] constructorValue,
                Class[] methodType,
                Object methodValue[]) {
            Object result = null;
            try {
                Constructor con = clazz.getDeclaredConstructor(constructorType);//这个修饰可以访问私有的构造器,并且使用
                con.setAccessible(true);
                Object obj = con.newInstance(constructorValue);
                Method method = clazz.getDeclaredMethod(methodName, methodType);
                method.setAccessible(true);
                result = method.invoke(obj, methodValue);
            } catch (InstantiationException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            } catch (SecurityException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
            return result;
        }
    
        /**
         * 变量Util
         * 类名 参数名 类型 参数值
         */
    
        public static Object useFieldValue(Class clazz, String fieldName) {
            return useFieldValue(clazz, fieldName, null, null);
        }
    
        public static Object useFieldValue(Class clazz, String fieldName,
                Class[] constructorType, Object[] constructorValue) {
            Object value = null;
            try {
                Constructor con = clazz.getDeclaredConstructor(constructorType);//这个修饰可以访问私有的构造器,并且使用
                con.setAccessible(true);
                Object obj = con.newInstance(constructorValue);
                Field field = clazz.getDeclaredField(fieldName);
                field.setAccessible(true);
                value = field.get(obj);
            } catch (InstantiationException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            } catch (SecurityException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
            return value;
        }
        /**
         * 常量Util
         * 类名 参数名 类型 参数值
         */
        public static Object getConstantValue(Class clazz, String constantName) {
            Object result = null;
            try {
                Constructor con = clazz.getDeclaredConstructor();//这个修饰可以访问私有的构造器,并且使用
                con.setAccessible(true);
                Object obj = con.newInstance();
                Field field = clazz.getDeclaredField(constantName);
                field.setAccessible(true);
                result = field.get(obj);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            } catch (SecurityException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            } catch (InstantiationException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
            return result;
        }
    
        /**
         * 返回所有方法名
         */
        public static List<String> getAllMethodName(Class clazz) {
            List<String> methodNames = new ArrayList<String>();
            Method[] methods = clazz.getMethods();
            for (Method method : methods) {
                methodNames.add(method.getName());
            }
            return methodNames;
        }
    
        /**
         * 返回所有属性名
         */
        public static List<String> getAllFieldName(Class clazz) {
            List<String> fieldNames = new ArrayList<String>();
            Field[] fields = clazz.getFields();
            for (Field field : fields) {
                fieldNames.add(field.getName());
            }
            return fieldNames;
        }
    
        /**
         * 返回包名
         */
        public static String getPackageName(Class clazz) {
            String packageName = null;
            Package package1 = clazz.getPackage();
            packageName = package1.getName();
            return packageName;
        }
    
        /**
         * 获取父类名称
         */
        public static String getSuperClassName(Class clazz) {
            String superClassName = null;
            Class superclass = clazz.getSuperclass();
            superClassName = superclass.getName();
            return superClassName;
        }
    
        /**
         * 未完待续
         */
    
    }
    
  • 相关阅读:
    倒计时
    用css 添加手状样式,鼠标移上去变小手
    二维数组去重方法
    权限管理
    文件操作
    【十一章】:RabbitMQ队列
    【十一章】:Memcache、Redis
    【第十章】:I/O多路复用、异步I/O(综合篇)
    【模块】:paramiko
    【第九章】:线程、进程和协程
  • 原文地址:https://www.cnblogs.com/shenyanrushang/p/10852504.html
Copyright © 2020-2023  润新知