反射

  •  获取Class类
      //获取Class类
     //(方法一)
     Class class1 = UserBean.class;
     //(方法二)
    Class class2 = new UserBean().getClass();
     //(方法三)
     Class class3 = Class.forName("cn.xxx.domain.UserBean");

     

  •  利用反射封装JavaBean数据
    /**
         * 封装JavaBean数据
         *
         * @param t
         * @param m
         * @param <T>
         * @throws IllegalAccessException
         * @throws InstantiationException
         */
        private <T> T getBean(T t, Map<String, String[]> m) throws IllegalAccessException, InstantiationException {
            //记录存储数据的属性名
            ArrayList<String> names = new ArrayList();
            //获取Class类
            Class c = (Class) t;
            //获取Class类的父类
            Class cParent = c.getSuperclass();
            //实例类
            T cParentIn = (T) c.newInstance();
            Set<Map.Entry<String, String[]>> entries = m.entrySet();
            Iterator<Map.Entry<String, String[]>> iterator = entries.iterator();
            //获取全部字段,包括私有字段
            Field[] declaredFields = cParent.getDeclaredFields();
            //
            while (iterator.hasNext()) {
                Map.Entry<String, String[]> next = iterator.next();
                String[] strs = next.getValue();
                for (Field field : declaredFields) {
                    if (next.getKey().equals(field.getName())) {
                        //设置私有对象可以访问
                        field.setAccessible(true);
                        String name = next.getKey();
                        String value = strs[0];
                        try {
                            //获取属性,name为参数名
                            Field declaredField = cParent.getDeclaredField(name);
                            //设置私有对象可以访问
                            declaredField.setAccessible(true);
                            //一定要子类实例,不然没有办法访问父类私有属性,cParentIn是类的实例
                            declaredField.set(cParentIn, value);
                            names.add(name);
                        } catch (NoSuchFieldException e) {
                            e.printStackTrace();
                        }
    
                        break;
                    }
                }
            }
    
            //获取类中参数的值
            //方法一:
            for (Field field : declaredFields) {
                field.setAccessible(true);
                String name = field.getName();
                try {
                    Field declaredField = cParent.getDeclaredField(name);
                    if (declaredField != null) {
                        declaredField.setAccessible(true);
                        //一定要判断,不然当属性值为空就会报错
                        if (declaredField.get(cParentIn) != null) {
                            //获取参数
                            String value = declaredField.get(cParentIn).toString();
                            System.out.println("key=" + name + ",value=" + value);
                        }
                    }
    
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                }
            }
    
            //方法二
            try {
                for (String name : names) {
                    Field declaredField = cParent.getDeclaredField(name);
                    declaredField.setAccessible(true);
                    String value = declaredField.get(cParentIn).toString();
                    System.out.println("key=" + name + ",value=" + value);
                }
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
    
            return cParentIn;
        }

     

posted @ 2018-12-24 13:32  好胖的兔子  阅读(138)  评论(0编辑  收藏  举报