本篇介绍功能代码复用架构中的细节
…还有一些自己定义的辅助注解。
try {
List methods = mTaskMethods.get(taskId);
if (methods == null) {
List taskMethods = new ArrayList<>();
for (Method method : ActionService.class.getDeclaredMethods()) {
int[] task_ids = method.getAnnotation(TASK_IDS.class).value();
for (int task_id : task_ids) {
if (task_id == taskId) {
taskMethods.add(method);
break;
}
}
}
mTaskMethods.put(taskId, methods = taskMethods);
}
for (Method method : methods) {
method.invoke(H.getInstance().createService(), event);
}
//update lastest oprate timestrap
mUpdateTime = System.currentTimeMillis();
} catch (Exception e) {
e.printStackTrace();
}
ActionAssertEntity actionAssertEntity = mActions.get(method);
if (actionAssertEntity == null) {
int type = -1;
String className = null;
String[] classNames = null;
Annotation[] annotations = method.getAnnotations();
if (annotations != null) {
Class extends Action> run = method.getAnnotation(RUN.class).value();
Annotation[] annotations1 = run.getAnnotations();
if (annotations1 != null) {
for (Annotation annotation : annotations1) {
if (annotation instanceof EVENT_TYPE) {
type = ((EVENT_TYPE) annotation).value();
} else if (annotation instanceof EVENT_CLASS) {
className = ((EVENT_CLASS) annotation).value();
} else if (annotation instanceof EVENT_CLASSES) {
classNames = ((EVENT_CLASSES) annotation).value();
}
}
}
if (type < 0) {
throw new IllegalArgumentException("类 " + run.getName() + "必须指定一个触发事件的类型 EVENT_TYPE 来过滤触发的事件");
}
if (TextUtils.isEmpty(className) && (classNames == null || classNames.length == 0)) {
throw new IllegalArgumentException("类 " + run.getName() + "必须指定一个类名 EVENT_CLASS 来过滤触发的事件");
}
mActions.put(method, actionAssertEntity = new ActionAssertEntity().eventType(type).className(className).classNames(classNames).action(run.newInstance()));
} else {
throw new IllegalArgumentException("you should identify the required annotations!");
}
}
这样就实现了retrofit2式的代理架构。学以致用,嘿嘿。