ARouter源码解析02-加载路由表单

来源:互联网 发布:卸载office2013软件 编辑:程序博客网 时间:2024/06/05 10:00

上篇文章我我们分析到,ARouter在IDE编译阶段,会自动生成一些文件。在这篇文章,我们将分析加载生成文件中的路由表单的过程。

ARouter.init()

ARouter唯一初始化的过程,在ARouter.init()中。

public static void init(Application application) {    //...    hasInit = _ARouter.init(application);    if (hasInit) {        _ARouter.afterInit();    }    //...}  

通过_ARouter.init(),接着调用了LogisticsCenter.init(),从而预先加载所有的路由表单。

public synchronized static void init(Context context, ThreadPoolExecutor tpe) throws HandlerException {    // 这些类通过 ARouter-compiler 生成,这个会去扫描包名下所有的ClassName    List<String> classFileNames = ClassUtils.getFileNameByPackageName(mContext, ROUTE_ROOT_PAKCAGE);    for (String className : classFileNames) {        if (className.startsWith(ROUTE_ROOT_PAKCAGE + DOT + SDK_NAME + SEPARATOR + SUFFIX_ROOT)) {                // 加载Root Elements 到 Warehouse的groupsIndex 集合中                ((IRouteRoot) (Class.forName(className).getConstructor().newInstance())).loadInto(Warehouse.groupsIndex);            } else if (className.startsWith(ROUTE_ROOT_PAKCAGE + DOT + SDK_NAME + SEPARATOR + SUFFIX_INTERCEPTORS)) {                // 加载Interceptor Elements 到  Warehouse的interceptorsIndex 集合中                ((IInterceptorGroup) (Class.forName(className).getConstructor().newInstance())).loadInto(Warehouse.interceptorsIndex);            } else if (className.startsWith(ROUTE_ROOT_PAKCAGE + DOT + SDK_NAME + SEPARATOR + SUFFIX_PROVIDERS)) {                // 加载Provider Elements 到 Warehouse的providersIndex 集合中                ((IProviderGroup) (Class.forName(className).getConstructor().newInstance())).loadInto(Warehouse.providersIndex);            }    }}  

可以看到,这里会先扫描所有的ROUTE_ROOT_PAKCAGE包下的className,然后遍历这些className,从而将IRouteGroup、IProvider、RouteMeta分别加载到Warehouse的相关集合中存储起来,方便之后的调用。

需要注意的是: 这里并没有把路由表单的目标类预先进行加载,只是将他们的class等数据先缓存起来,这么做的好处显而易见,无须再次通过上面的这些方法来获取路由表单。

再来看一眼Warehouse类

class Warehouse {    // Cache route and metas    static Map<String, Class<? extends IRouteGroup>> groupsIndex = new HashMap<>();    static Map<String, RouteMeta> routes = new HashMap<>();    // Cache provider    static Map<Class, IProvider> providers = new HashMap<>();    static Map<String, RouteMeta> providersIndex = new HashMap<>();    //...}  

OK,现在路由表单已加载到内存中了,那么,怎么通过这些路由表单,进行路由跳转呢 ? 我们下篇文章来进行分析。

原创粉丝点击