RocketMQ NameServer模块 源码学习

  1. RocketMQ namesrv 模块中 用到了 MixAll 类, 其中有一个 properties2Object(Properties, Object) 通用方法,把properties 转换成 简单的 POJO object。 你可以进一步扩展:从.properties 文件中读取properties key 和 value, 然后将key 和 value 转换成 object。 这个方法是个很好的使用反射的例子,恰到好处的使用了反射。我对源码稍做了些简化,注意我的注释,如下:

    public class MyMixAll {
    
    @Test
    public void properties2ObjectDemo() {
        Properties p = new Properties();
        p.setProperty("age", "18");
        p.setProperty("name", "yonghao");
        Person person = new Person();
        this.properties2Object(p, person);
        System.out.println(JSON.toJSONString(person));
    }
    
    /**
     * 把properties 转换成 简单的 POJO object。
     * @param p properties 可以是读取文件后获得的properties.
     * @param object properties 对应的 object 类
     */
    private void properties2Object(Properties p, Object object) {
        Method[] methods = object.getClass().getMethods();  //这里就是返回公有方法, setName, getName, setAge...
        for (Method method : methods) {
            String mn = method.getName();
            if (mn.startsWith("set")) { // 找到set 方法
                String key = StringUtils.substring(mn, 3).toLowerCase();  // e.g setName 方法, 得到name
                String value = p.getProperty(key);   // 获取setName 方法的 name property value.
                if (value != null) {
                    Class<?>[] pt = method.getParameterTypes();  // 拿到这个set方法的参数类型
                    if (pt != null && pt.length > 0) {
                        String cn = pt[0].getSimpleName(); //获取setName(String name) 参数类型 String
                        Object arg = null;
                        switch (cn) {
                            case "int":
                            case "Integer":
                                arg = Integer.parseInt(value);
                                break;
                            case "long":
                            case "Long":
                                arg = Long.parseLong(value);
                                break;
                            case "boolean":
                            case "Boolean":
                                arg = Boolean.parseBoolean(value);
                                break;
                            case "float":
                            case "Float":
                                arg = Float.parseFloat(value);
                                break;
                            case "String":
                                arg = value;
                                break;
                            default:
                                continue;
                        }
                        try {
                            method.invoke(object, arg); // 执行object 的 method 方法
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
    
    class Person {
        private String name;
        private int age;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
    }
    }
    

猜你喜欢

转载自blog.csdn.net/ZHANGYONGHAO604/article/details/82463345