Search in sources :

Example 1 with InjectExtraInvalid

use of com.andframe.annotation.inject.InjectExtraInvalid in project AndFrameWorks by scwang90.

the class Injecter method injectExtra.

private static void injectExtra(Field field, Object handler, Context context) {
    InjectExtraInvalid invalid = AfReflecter.getAnnotation(handler.getClass(), InjectExtraInvalid.class);
    if (invalid != null && invalid.value().length == 0) {
        return;
    }
    if (field.isAnnotationPresent(InjectExtra.class)) {
        InjectExtra inject = field.getAnnotation(InjectExtra.class);
        if (invalid != null) {
            boolean find = false;
            for (String extra : invalid.value()) {
                if (TextUtils.equals(extra, inject.value())) {
                    find = true;
                    break;
                }
            }
            if (find) {
                return;
            }
        }
        try {
            Extrater intent = new AfIntent();
            if (handler instanceof Activity) {
                intent = new AfIntent(((Activity) handler).getIntent());
            } else if (handler instanceof Fragment) {
                final Fragment fragment = (Fragment) handler;
                Bundle bundle = fragment.getArguments();
                if (bundle != null) {
                    intent = new AfBundle(fragment.getArguments()) {

                        @Override
                        public <T> T get(String _key, Class<T> clazz) {
                            T t = super.get(_key, clazz);
                            if (t == null && fragment.getActivity() != null) {
                                return new AfIntent(fragment.getActivity().getIntent()).get(_key, clazz);
                            }
                            return t;
                        }

                        @Override
                        public <T> List<T> getList(String _key, Class<T> clazz) {
                            List<T> list = super.getList(_key, clazz);
                            if (list == null || list.size() == 0) {
                                return new AfIntent(fragment.getActivity().getIntent()).getList(_key, clazz);
                            }
                            return list;
                        }
                    };
                } else {
                    if (context instanceof Activity && fragment.getActivity() == null) {
                        intent = new AfIntent(((Activity) context).getIntent());
                    } else {
                        intent = new AfIntent(fragment.getActivity().getIntent());
                    }
                }
            }
            Object value;
            Class<?> type = field.getType();
            Type generic = field.getGenericType();
            if (type.isArray()) {
                type = type.getComponentType();
                List<?> list = intent.getList(inject.value(), type);
                value = Array.newInstance(type, list.size());
                value = list.toArray((Object[]) value);
            } else if (List.class.equals(type)) {
                ParameterizedType parameterized = (ParameterizedType) generic;
                type = (Class<?>) parameterized.getActualTypeArguments()[0];
                value = intent.getList(inject.value(), type);
            } else {
                value = intent.get(inject.value(), type);
            }
            if (value != null) {
                field.setAccessible(true);
                field.set(handler, value);
            } else if (inject.necessary()) {
                if (inject.remark().length() > 0) {
                    throw new AfException("缺少必须参数:" + inject.remark());
                } else {
                    throw new AfException("缺少必须参数:" + inject.value());
                }
            }
        } catch (AfException e) {
            throw e;
        } catch (Throwable e) {
            if (inject.necessary()) {
                throw new RuntimeException("缺少必须参数", e);
            }
            AfExceptionHandler.handle(e, TAG(handler, "doInject.InjectExtra.") + field.getName());
        }
    }
}
Also used : Bundle(android.os.Bundle) AfBundle(com.andframe.feature.AfBundle) AfActivity(com.andframe.activity.AfActivity) Activity(android.app.Activity) AfBundle(com.andframe.feature.AfBundle) Fragment(android.support.v4.app.Fragment) InjectExtra(com.andframe.annotation.inject.InjectExtra) ParameterizedType(java.lang.reflect.ParameterizedType) Extrater(com.andframe.api.Extrater) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) InjectExtraInvalid(com.andframe.annotation.inject.InjectExtraInvalid) ColorStateList(android.content.res.ColorStateList) List(java.util.List) AfException(com.andframe.exception.AfException) AfIntent(com.andframe.feature.AfIntent)

Aggregations

Activity (android.app.Activity)1 ColorStateList (android.content.res.ColorStateList)1 Bundle (android.os.Bundle)1 Fragment (android.support.v4.app.Fragment)1 AfActivity (com.andframe.activity.AfActivity)1 InjectExtra (com.andframe.annotation.inject.InjectExtra)1 InjectExtraInvalid (com.andframe.annotation.inject.InjectExtraInvalid)1 Extrater (com.andframe.api.Extrater)1 AfException (com.andframe.exception.AfException)1 AfBundle (com.andframe.feature.AfBundle)1 AfIntent (com.andframe.feature.AfIntent)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 List (java.util.List)1