use of com.andframe.exception.AfException 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());
}
}
}
Aggregations