在具体的组件中 , 实现上述 ComponentService 接口 , 并添加 @Route 注解 ;
package kim.hsl.library2; import android.util.Log; import kim.hsl.base.ComponentService; import kim.hsl.router_annotation.Route; @Route(path = "/library2/StringService") public class StringService implements ComponentService { @Override public void doSomething() { Log.i("StringService", "library2 组件中的 StringService 服务 "); } }
该类定义位置 : 在任意模块都可以调用该类 ;
二、注解处理器添加对上述 " 组件间共享的服务 " 的支持
之前在注解处理器中 , 只支持 android.app.Activity 的路由节点添加 ;
// 获取 android.app.Activity 类型的注解节点 TypeElement activityElement = mElementUtils.getTypeElement("android.app.Activity"); // 判断 typeMirror 注解节点是否是 Activity 类型 if (mTypeUtils.isSubtype(element.asType(), activityElement.asType())) { // 该节点是 android.app.Activity 类型的 routeBean = new RouteBean( RouteBean.Type.ACTIVITY, // 路由对象类型 element, // 路由节点 null, // 类对象 route.path(), // 路由地址 route.group()); // 路由组 }
当前注解处理器中 , 支持 kim.hsl.route_core.template.IService 类型的 路由节点 添加 ;
// 获取 android.app.Activity 类型的注解节点 TypeElement activityElement = mElementUtils.getTypeElement("android.app.Activity"); // 获取 组件间共享服务 的接口, 该接口仅用于表示组件类型 TypeElement iServiceElement = mElementUtils.getTypeElement("kim.hsl.route_core.template.IService"); // 判断 typeMirror 注解节点是否是 Activity 类型 if (mTypeUtils.isSubtype(element.asType(), activityElement.asType())) { // 该节点是 android.app.Activity 类型的 routeBean = new RouteBean( RouteBean.Type.ACTIVITY, // 路由对象类型 element, // 路由节点 null, // 类对象 route.path(), // 路由地址 route.group()); // 路由组 }else if (mTypeUtils.isSubtype(element.asType(), iServiceElement.asType())) { // 该节点是 kim.hsl.route_core.template.IService 类型的 routeBean = new RouteBean( RouteBean.Type.ISERVICE, // 路由对象类型 element, // 路由节点 null, // 类对象 route.path(), // 路由地址 route.group()); // 路由组 }else{ // 该节点不是 android.app.Activity 类型的 throw new RuntimeException("@Route 注解节点类型错误"); }
三、注解处理器 生成代码规则
注解处理器的 process 方法调用 , 是按照 Module 模块进行的 ;
如果 Module 模块中有相关注解 , 传入的 Set<? extends TypeElement> set 参数不为空 , 就会进行相关处理 ;
如果 Module 模块中没有相关注解 , 传入的 Set<? extends TypeElement> set 参数为空 , 此时就不进行后续操作 ;
下图红色的 library1 模块中没有注解 ;
蓝色的 library2 模块中添加了 @Route(path = “/library2/StringService”) 注解 ;
绿色的 app 模块中添加了 @Route(path = “/app/MainActivity”) 注解 ;
Module 模块中 , 使用注解生成的源码 , 都在对应模块的 " build\generated\ap_generated_sources\debug\out\ " 目录中 ;

