ARouter采用了编译时注解,在IDE编译阶段,即会生成相关文件,以供后续使用。具体实现在arouter-compiler,由于编译时注解的实现原理都大同小异,在这里就不详细说明了。
@AutoService(Processor.class)
@SupportedOptions(KEY_MODULE_NAME)
@SupportedSourceVersion(SourceVersion.RELEASE_7)
@SupportedAnnotationTypes({ANNOTATION_TYPE_ROUTE, ANNOTATION_TYPE_AUTOWIRED})
public class RouteProcessor extends AbstractProcessor {
@Override
public boolean process(Set extends TypeElement> annotations, RoundEnvironment roundEnv) {
if (CollectionUtils.isNotEmpty(annotations)) {
Set extends Element> routeElements = roundEnv.getElementsAnnotatedWith(Route.class);
//遍历注解,存储至map集合,再写入到文件中
this.parseRoutes(routeElements);
return true;
}
return false;
}
主要来看编译之后,ARouter产生的文件。文件的路径在
app/build/generated/source/apt/debug/com.alibaba.android.arouter/
文件夹下
public class ARouter$$Group$$service implements IRouteGroup {
@Override
public void loadInto(Map atlas) {
atlas.put("/service/hello", RouteMeta.build(RouteType.PROVIDER, HelloServiceImpl.class,
"/service/hello", "service", null, -1, -2147483648));
atlas.put("/service/json", RouteMeta.build(RouteType.PROVIDER, JsonServiceImpl.class,
"/service/json", "service", null, -1, -2147483648));
atlas.put("/service/single", RouteMeta.build(RouteType.PROVIDER, SingleService.class,
"/service/single", "service", null, -1, -2147483648));
}
}
public class ARouter$$Group$$test implements IRouteGroup {
@Override
public void loadInto(Map atlas) {
atlas.put("/test/activity1", RouteMeta.build(RouteType.ACTIVITY, Test1Activity.class,
"/test/activity1", "test", new java.util.HashMap(){{put("pac", 9);
put("url", 8); }}, -1, -2147483648));
atlas.put("/test/activity2", RouteMeta.build(RouteType.ACTIVITY, Test2Activity.class,
"/test/activity2", "test", new java.util.HashMap(){{put("key1", 8); }}
, -1, -2147483648));
atlas.put("/test/fragment", RouteMeta.build(RouteType.FRAGMENT, BlankFragment.class,
"/test/fragment", "test", null, -1, -2147483648));
atlas.put("/test/webview", RouteMeta.build(RouteType.ACTIVITY, TestWebview.class,
"/test/webview", "test", null, -1, -2147483648));
}
}
用于记录组级别的路由清单,所有组级别的路由清单又会在此记录。
public class ARouter$$Root$$app implements IRouteRoot {
@Override
public void loadInto(Map> routes) {
routes.put("service", ARouter$$Group$$service.class);
routes.put("test", ARouter$$Group$$test.class);
}
}
可以发现拦截器的put顺序,已在编译阶段,按照优先级排序好了。
public class ARouter$$Interceptors$$app implements IInterceptorGroup {
@Override
public void loadInto(Map> interceptors) {
interceptors.put(3, Test2Interceptor.class);
interceptors.put(5, Test3Interceptor.class);
interceptors.put(7, Test1Interceptor.class);
}
}
public class ARouter$$Providers$$app implements IProviderGroup {
@Override
public void loadInto(Map providers) {
providers.put("com.alibaba.android.arouter.demo.testservice.HelloService",
RouteMeta.build(RouteType.PROVIDER, HelloServiceImpl.class, "/service/hello",
"service", null, -1, -2147483648));
providers.put("com.alibaba.android.arouter.facade.service.SerializationService",
RouteMeta.build(RouteType.PROVIDER, JsonServiceImpl.class, "/service/json", "service",
null, -1, -2147483648));
providers.put("com.alibaba.android.arouter.demo.testservice.SingleService",
RouteMeta.build(RouteType.PROVIDER, SingleService.class, "/service/single",
"service", null, -1, -2147483648));
}
}
从ARouter编译的产物,我们可以猜测出,ARouter是通过加载这些路由清单,从而进行跳转等操作的。
下篇文章,我们来验证我们的猜想。