Android項目之註解

使用註解能夠增加開發效率,但是可讀性差,其他的註解框架有butterknife。

    /**
     * 注入View
     *
     */
    public static void injectView(Object target, ViewFinder viewFinder) {
        if (target == null || viewFinder == null) {
            throw new NullPointerException();
        }
        Class<?> cls = target.getClass();
        while (cls != null && cls != Object.class && cls != Activity.class && cls != View.class) {
            Field[] declaredFields = cls.getDeclaredFields();
            for (Field field : declaredFields) {
                if (!field.isAccessible())
                    field.setAccessible(true);
                FindViewById findViewById = field.getAnnotation(FindViewById.class);
                if (findViewById == null) {
                    continue;
                } else {
                    int id = findViewById.value();
                    View view = viewFinder.findViewById(id);
                    if (view == null) {
                        // 根據註解的ViewID,無法查找到View, 檢查控件在對應的XML中的ID是否存在
                        String msg = field.getName() + " 根據ID不能查找到對應的View,請檢查XML資源文件中的id屬性是否等於註解的ID";
                        throw new ViewInjectException(msg);
                    } else if (!field.getType().isInstance(view)) {
                        // 控件類型不匹配, 請檢查XML中的控件類型和java代碼類型是否匹配
                        String msg = field.getName() + " 類型匹配錯誤,java類型:" + field.getType().getSimpleName()
                                + ", View在XML中申明的類型:" + view.getClass().getSimpleName() + ",請檢查XML中的控件類型和java代碼類型是否匹配";
                        throw new ViewInjectException(msg);
                    } else {
                        try {
                            field.set(target, view);
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
            cls = cls.getSuperclass();
        }
    }

    /**
     * 注入單擊事件
     *
     */
    public static void injectOnClick(Object target, ViewFinder viewFinder) {
        if (target == null || viewFinder == null) {
            throw new NullPointerException();
        }
        Class<?> cls = target.getClass();

        while (cls != null && cls != Object.class && cls != Activity.class && cls != View.class) {
            Method[] methods = cls.getDeclaredMethods();
            for (Method method : methods) {
                if (!method.isAccessible()) {
                    method.setAccessible(true);
                }

                OnClick onClick = method.getAnnotation(OnClick.class);
                if (onClick != null) {
                    Class<?>[] parameterTypes = method.getParameterTypes();
                    Class<?> viewClass = null;
                    if (parameterTypes.length != 1) {
                        String msg = method.getName() + " OnClick註解方法有且只能有一個參數!";
                        throw new ViewInjectException(msg);
                    } else {
                        viewClass = parameterTypes[0];
                    }

                    int[] ids = onClick.value();
                    for (int id : ids) {
                        View targetView = viewFinder.findViewById(id);
                        if (targetView == null) {
                            // 根據註解的ViewID,無法查找到對應的View, 檢查控件在對應的XML中的ID是否存在
                            String msg = method.getName() + " 根據ID不能查找到對應的View,請檢查XML資源文件中的id屬性是否等於註解的ID";
                            throw new ViewInjectException(msg);
                        } else if (!viewClass.isAssignableFrom(targetView.getClass())) {
                            // 控件類型和方法參數不匹配, 請檢查方法參數的類型
                            String msg = method.getName() + " 方法參數類型:" + viewClass.getName() + ", View在XML中申明的類型:"
                                    + targetView.getClass().getName();
                            throw new ViewInjectException(msg);
                        } else {
                            targetView.setOnClickListener(new InjectOnClickListener(method, target));
                        }
                    }
                }
            }
            cls = cls.getSuperclass();
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章