1. 程式人生 > >RocketMQ NameServer模組 原始碼學習

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;
        }
    }
    }