Osheep

时光不回头,当下最重要。

Android之旅 -- ARouter 源码分析(二)

《Android之旅 -- ARouter 源码分析(二)》

美景总是好的

Android之旅 — ARouter 源码分析(一) 主要介绍了 ARouter 启动 activity 的基本原理.
这篇我们来看下在编译期 arouter-compiler 都做了哪些事情

介绍

大概梳理一下 arouter-compiler, 先查看 build.gradle

// build.gradle

// 这里要注意, 不能用 com.android.library, 因为对 annotation 的处理需要用的javax 包下的类, android sdk是没有的.
apply plugin: 'java'

...

dependencies {
    // 这里依赖的不是源码的module
    compile 'com.alibaba:arouter-annotation:1.0.3'
    
    // 这个是自动生成 META-INF 配置 Processor 内容的
    compile 'com.google.auto.service:auto-service:1.0-rc2'

    // 用来生成代码的, 比拼字符串生成源码要高大上一些
    compile 'com.squareup:javapoet:1.7.0'

...

可以看到 arouter-compiler 通过 自动配置的 Processor, 查找声明了 arouter-annotation 里定义的 annotation, 然后根据 annotation 的配置使用 javapoet 生成辅助注入的代码, 达到在开发中可以不用实际依赖一些类, 达到模块或者组件之间的解耦.


arouter-compiler 源码分析

arouter-compiler 有三个 Processor
AutowiredProcessor — 处理 @Autowired
InterceptorProcessor — 处理 @Interceptor
RouteProcessor — 处理 @Autowired, Route

  1. 先分析 RouteProcessor 的工作原理, 先看看 init 方法里初始化做了什么
// RouteProcessor.java
    public synchronized void init(ProcessingEnvironment processingEnv) {
        super.init(processingEnv);

        // 用来生成代码的 Filer 对象
        mFiler = processingEnv.getFiler(); 
        
        // 数据类型和类的描述
        types = processingEnv.getTypeUtils();            // Get type utils.
        elements = processingEnv.getElementUtils();      // Get class meta.
        typeUtils = new TypeUtils(types, elements);

        ...
        // module name, 使用 arouter-compiler 的module 都要配置在build.gradle, 否则抛出 exception
        moduleName = options.get(KEY_MODULE_NAME);

        // 获取 com.alibaba.android.arouter.facade.template.IProvider 的 TypeMirror 对象
        iProvider = elements.getTypeElement(Consts.IPROVIDER).asType();
    }

接下来我们进入核心的 process 方法, init, process 这两个方法都是 Processor 自动调用的

// RouteProcessor.java
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
        ...
        Set<? extends Element> routeElements = roundEnv.getElementsAnnotatedWith(Route.class);
        this.parseRoutes(routeElements);
        ...
    }

主要逻辑都在 parseRoutes 这个方法中, 继续查看下去, 这个方法写的有点长…

    private void parseRoutes(Set<? extends Element> routeElements) throws IOException {
            // 创建参数类型 Map<String, Class<? extends IRouteGroup>>
            ParameterizedTypeName inputMapTypeOfRoot = ParameterizedTypeName.get(
                    ClassName.get(Map.class),
                    ClassName.get(String.class),
                    ParameterizedTypeName.get(
                            ClassName.get(Class.class),
                            WildcardTypeName.subtypeOf(ClassName.get(type_IRouteGroup))
                    )
            );

            // 创建参数类型 Map<String, RouteMeta>
            ParameterizedTypeName inputMapTypeOfGroup = ParameterizedTypeName.get(
                    ClassName.get(Map.class),
                    ClassName.get(String.class),
                    ClassName.get(RouteMeta.class)
            );

            // 创建方法参数 routes, atlas, providers
            ParameterSpec rootParamSpec = ParameterSpec.builder(inputMapTypeOfRoot, "routes").build();
            ParameterSpec groupParamSpec = ParameterSpec.builder(inputMapTypeOfGroup, "atlas").build();
            ParameterSpec providerParamSpec = ParameterSpec.builder(inputMapTypeOfGroup, "providers").build();  // Ps. its param type same as groupParamSpec!

            // 声明 public void loadInto(Map<String, Class<? extends IRouteGroup>> routes)
            MethodSpec.Builder loadIntoMethodOfRootBuilder = MethodSpec.methodBuilder(METHOD_LOAD_INTO)
                    .addAnnotation(Override.class)
                    .addModifiers(PUBLIC)
                    .addParameter(rootParamSpec);

            // 遍历所有申明的 @Route 
            for (Element element : routeElements) {
                // 取得 class 的类型描述
                TypeMirror tm = element.asType();
                
                // 取得@Route注解的实例
                Route route = element.getAnnotation(Route.class);
                RouteMeta routeMete = null;

                // 判断class 是否继承自 Activity
                if (types.isSubtype(tm, type_Activity)) {
                    Map<String, Integer> paramsType = new HashMap<>();
                    
                    // 遍历所有声明 @Autowired 的属性
                    for (Element field : element.getEnclosedElements()) {
                        // 满足条件是属性, 声明了 @Autowired, 并且没有实现 iProvider
                        if (field.getKind().isField() && field.getAnnotation(Autowired.class) != null && !types.isSubtype(field.asType(), iProvider)) {

                            Autowired paramConfig = field.getAnnotation(Autowired.class);

                            // 根据 @Autowired 声明的名字做key, 缓存数据类型的 index 值
                            paramsType.put(StringUtils.isEmpty(paramConfig.name()) ? field.getSimpleName().toString() : paramConfig.name(), typeUtils.typeExchange(field));
                        }
                    }

                    // 创建 RouteMeta 对象
                    routeMete = new RouteMeta(route, element, RouteType.ACTIVITY, paramsType);
                } else if (types.isSubtype(tm, iProvider)) {         // IProvider
                    routeMete = new RouteMeta(route, element, RouteType.PROVIDER, null);
                } else if (types.isSubtype(tm, type_Service)) {           // Service
                    routeMete = new RouteMeta(route, element, RouteType.parse(SERVICE), null);
                } else if (types.isSubtype(tm, fragmentTm) || types.isSubtype(tm, fragmentTmV4)) {
                    routeMete = new RouteMeta(route, element, RouteType.parse(FRAGMENT), null);
                }
              
                // 这个方法就是把 route 按照 group 分组保存到 groupMap( Map<String, Set<RouteMeta>> )
                categories(routeMete);
            }
            

            // 声明这个方法 public void loadInto(Map<String, RouteMeta> providers)
            MethodSpec.Builder loadIntoMethodOfProviderBuilder = MethodSpec.methodBuilder(METHOD_LOAD_INTO)
                    .addAnnotation(Override.class)
                    .addModifiers(PUBLIC)
                    .addParameter(providerParamSpec);

            // 开始生成代码, 遍历刚才生成的 groupMap
            for (Map.Entry<String, Set<RouteMeta>> entry : groupMap.entrySet()) {
                String groupName = entry.getKey();
              
                // 声明方法 public void loadInto(Map<String, RouteMeta> atlas)
                MethodSpec.Builder loadIntoMethodOfGroupBuilder = MethodSpec.methodBuilder(METHOD_LOAD_INTO)
                        .addAnnotation(Override.class)
                        .addModifiers(PUBLIC)
                        .addParameter(groupParamSpec);

                // 遍历 group 内保存的 routes
                Set<RouteMeta> groupData = entry.getValue();
                for (RouteMeta routeMeta : groupData) {
                    switch (routeMeta.getType()) {
                        case PROVIDER: 
                            List<? extends TypeMirror> interfaces = ((TypeElement) routeMeta.getRawType()).getInterfaces();

                            // 生成方法内代码, 例如下面
                            // providers.put("com.alibaba.android.arouter.demo.testservice.HelloService", RouteMeta.build(RouteType.PROVIDER, HelloServiceImpl.class, "/service/hello", "service", null, -1, -2147483648));
                            for (TypeMirror tm : interfaces) {
                                if (types.isSameType(tm, iProvider)) {
                                    loadIntoMethodOfProviderBuilder.addStatement(
                                            "providers.put($S, $T.build($T." + routeMeta.getType() + ", $T.class, $S, $S, null, " + routeMeta.getPriority() + ", " + routeMeta.getExtra() + "))",
                                            (routeMeta.getRawType()).toString(),
                                            routeMetaCn,
                                            routeTypeCn,
                                            ClassName.get((TypeElement) routeMeta.getRawType()),
                                            routeMeta.getPath(),
                                            routeMeta.getGroup());
                                } else if (types.isSubtype(tm, iProvider)) {
                                    loadIntoMethodOfProviderBuilder.addStatement(
                                            "providers.put($S, $T.build($T." + routeMeta.getType() + ", $T.class, $S, $S, null, " + routeMeta.getPriority() + ", " + routeMeta.getExtra() + "))",
                                            tm.toString(), 
                                            routeMetaCn,
                                            routeTypeCn,
                                            ClassName.get((TypeElement) routeMeta.getRawType()),
                                            routeMeta.getPath(),
                                            routeMeta.getGroup());
                                }
                            }
                            break;
                        default:
                            break;
                    }

                    // 生成配置的参数类型 map
                    // 例如 : put("pac", 9); put("obj", 10); put("name", 8); put("boy", 0);
                    StringBuilder mapBodyBuilder = new StringBuilder();
                    Map<String, Integer> paramsType = routeMeta.getParamsType();
                    if (MapUtils.isNotEmpty(paramsType)) {
                        for (Map.Entry<String, Integer> types : paramsType.entrySet()) {
                            mapBodyBuilder.append("put(\"").append(types.getKey()).append("\", ").append(types.getValue()).append("); ");
                        }
                    }
                    String mapBody = mapBodyBuilder.toString();

                    // 生成方法内代码
                    // atlas.put("/test/activity2", RouteMeta.build(RouteType.ACTIVITY, Test2Activity.class, "/test/activity2", "test", new java.util.HashMap<String, Integer>(){{put("key1", 8); }}, -1, -2147483648));
                    loadIntoMethodOfGroupBuilder.addStatement(
                            "atlas.put($S, $T.build($T." + routeMeta.getType() + ", $T.class, $S, $S, " + (StringUtils.isEmpty(mapBody) ? null : ("new java.util.HashMap<String, Integer>(){{" + mapBodyBuilder.toString() + "}}")) + ", " + routeMeta.getPriority() + ", " + routeMeta.getExtra() + "))",
                            routeMeta.getPath(),
                            routeMetaCn,
                            routeTypeCn,
                            ClassName.get((TypeElement) routeMeta.getRawType()),
                            routeMeta.getPath().toLowerCase(),
                            routeMeta.getGroup().toLowerCase());
                }

                // 生成 java 文件, 例如 ARouter$$Group$$test.java, ARouter$$Group$$service.java
                String groupFileName = NAME_OF_GROUP + groupName;
                JavaFile.builder(PACKAGE_OF_GENERATE_FILE,
                        TypeSpec.classBuilder(groupFileName)
                                .addJavadoc(WARNING_TIPS)
                                .addSuperinterface(ClassName.get(type_IRouteGroup))
                                .addModifiers(PUBLIC)
                                .addMethod(loadIntoMethodOfGroupBuilder.build())
                                .build()
                ).build().writeTo(mFiler);

                // 缓存文件名
                rootMap.put(groupName, groupFileName);
            }

            // 生成 ARouter$$Root$$app.java 里的代码
            if (MapUtils.isNotEmpty(rootMap)) {
                // Generate root meta by group name, it must be generated before root, then I can find out the class of group.
                for (Map.Entry<String, String> entry : rootMap.entrySet()) {

                    // 生成代码 : routes.put("service", ARouter$$Group$$service.class);
                    loadIntoMethodOfRootBuilder.addStatement("routes.put($S, $T.class)", entry.getKey(), ClassName.get(PACKAGE_OF_GENERATE_FILE, entry.getValue()));
                }
            }

            // 生成 providers java 文件. 例如 ARouter$$Providers$$app.java
            String providerMapFileName = NAME_OF_PROVIDER + SEPARATOR + moduleName;
            JavaFile.builder(PACKAGE_OF_GENERATE_FILE,
                    TypeSpec.classBuilder(providerMapFileName)
                            .addJavadoc(WARNING_TIPS)
                            .addSuperinterface(ClassName.get(type_IProviderGroup))
                            .addModifiers(PUBLIC)
                            .addMethod(loadIntoMethodOfProviderBuilder.build())
                            .build()
            ).build().writeTo(mFiler);

            // 生成 root java 文件. 例如 ARouter$$Root$$app.java
            String rootFileName = NAME_OF_ROOT + SEPARATOR + moduleName;
            JavaFile.builder(PACKAGE_OF_GENERATE_FILE,
                    TypeSpec.classBuilder(rootFileName)
                            .addJavadoc(WARNING_TIPS)
                            .addSuperinterface(ClassName.get(elements.getTypeElement(ITROUTE_ROOT)))
                            .addModifiers(PUBLIC)
                            .addMethod(loadIntoMethodOfRootBuilder.build())
                            .build()
            ).build().writeTo(mFiler);
    }

总结一下RouteProcessor 做了什么事情

  • 根据 @Route 配置 path, group 生成 ARouter$$Root$$”moduleName”
  • ARouter$$Root$$xxxx.java 以 group 为 key, 注入了生成的 ARouter$$Group$$xxxx 类
  • 根据 @Route 配置 生成 ARouter$$Providers$$”groupName” 和 ARouter$$Group$$”groupName”
  • ARouter$$Providers$$xxxx.java 以类为 key, 注入了 IProvider 接口的实现类
  • ARouter$$Group$$xxxx.java 以 path 为 key, 注入 RouteMate实例, 描述实现类和 @Autowired 参数描述

RouteProcessor 完成 ARouter 大部分工作, 除了拦截器和自动注入

  1. InterceptorProcessor init 方法和 RouteProcessor 类似, 直接跳过, 直接看核心方法
// InterceptorProcessor.java
    private void parseInterceptors(Set<? extends Element> elements) throws IOException {
            for (Element element : elements) {
                // 验证必需实现了 IInterceptor 接口
                if (verify(element)) {
                    Interceptor interceptor = element.getAnnotation(Interceptor.class);
                    
                    // 根据配置 @Interceptor 的 priority, 返回是否有重复配置的拦截器, 如果有抛出错误
                    Element lastInterceptor = interceptors.get(interceptor.priority());
                    if (null != lastInterceptor) {
                        throw new IllegalArgumentException...
                    }

                    interceptors.put(interceptor.priority(), element);
                }
            }

            // 声明类型 Map<Integer, Class<? extends ITollgate>>
            ParameterizedTypeName inputMapTypeOfTollgate = ParameterizedTypeName.get(
                    ClassName.get(Map.class),
                    ClassName.get(Integer.class),
                    ParameterizedTypeName.get(
                            ClassName.get(Class.class),
                            WildcardTypeName.subtypeOf(ClassName.get(type_ITollgate))
                    )
            );

            // 方法参数 interceptors
            ParameterSpec tollgateParamSpec = ParameterSpec.builder(inputMapTypeOfTollgate, "interceptors").build();

            // 声明方法 loadInto
            MethodSpec.Builder loadIntoMethodOfTollgateBuilder = MethodSpec.methodBuilder(METHOD_LOAD_INTO)
                    .addAnnotation(Override.class)
                    .addModifiers(PUBLIC)
                    .addParameter(tollgateParamSpec);

                // 生成方法内代码, 例如 : interceptors.put(7, Test1Interceptor.class);
                for (Map.Entry<Integer, Element> entry : interceptors.entrySet()) {
                    loadIntoMethodOfTollgateBuilder.addStatement("interceptors.put(" + entry.getKey() + ", $T.class)", ClassName.get((TypeElement) entry.getValue()));
                }

            // // 生成 Interceptor 的注入代码, 继承了 IInterceptorGroup. 例如 ARouter$$Interceptors$$app.java
            JavaFile.builder(PACKAGE_OF_GENERATE_FILE,
                    TypeSpec.classBuilder(NAME_OF_INTERCEPTOR + SEPARATOR + moduleName)
                            .addModifiers(PUBLIC)
                            .addJavadoc(WARNING_TIPS)
                            .addMethod(loadIntoMethodOfTollgateBuilder.build())
                            .addSuperinterface(ClassName.get(type_ITollgateGroup))
                            .build()
            ).build().writeTo(mFiler);
    }

InterceptorProcessor 就是生成注入拦截器的代码, 拦截器有优先级, 并且一个优先级只能一个拦截器

  1. AutowiredProcessor 负责生成自动注入的代码, 这点 butterknife 原理差不多
    public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) {
        categories(roundEnvironment.getElementsAnnotatedWith(Autowired.class));
        generateHelper();
    }
    private void categories(Set<? extends Element> elements) throws IllegalAccessException {
            for (Element element : elements) {
                TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();
                
                // @Autowired不能修饰 private
                if (element.getModifiers().contains(Modifier.PRIVATE)) {
                    throw new IllegalAccessException("The autowired fields CAN NOT BE 'private'!!! please check field ["
                            + element.getSimpleName() + "] in class [" + enclosingElement.getQualifiedName() + "]");
                }

                // 根据声明的类分组缓存
                if (parentAndChild.containsKey(enclosingElement)) {
                    parentAndChild.get(enclosingElement).add(element);
                } else {
                    List<Element> childs = new ArrayList<>();
                    childs.add(element);
                    parentAndChild.put(enclosingElement, childs);
                }
            }
    }
    private void generateHelper() throws IOException, IllegalAccessException {
        // 声明方法参数 Object target
        ParameterSpec objectParamSpec = ParameterSpec.builder(TypeName.OBJECT, "target").build();

            // 遍历前面分析后的 parentAndChild
            for (Map.Entry<TypeElement, List<Element>> entry : parentAndChild.entrySet()) {

                // 声明方法 public void inject(Object target)
                MethodSpec.Builder injectMethodBuilder = MethodSpec.methodBuilder(METHOD_INJECT)
                        .addAnnotation(Override.class)
                        .addModifiers(PUBLIC)
                        .addParameter(objectParamSpec);

                TypeElement parent = entry.getKey();
                List<Element> childs = entry.getValue();

                String qualifiedName = parent.getQualifiedName().toString();
                String packageName = qualifiedName.substring(0, qualifiedName.lastIndexOf("."));

                // 类名 : com.alibaba.android.arouter.demo.testactivity.Test1Activity
                // 生成 : com.alibaba.android.arouter.demo.testactivity.Test1Activity$$ARouter$$Autowired
                String fileName = parent.getSimpleName() + NAME_OF_AUTOWIRED;

                // 声明 public class Test1Activity$$ARouter$$Autowired implements ISyringe
                TypeSpec.Builder helper = TypeSpec.classBuilder(fileName)
                        .addJavadoc(WARNING_TIPS)
                        .addSuperinterface(ClassName.get(type_ISyringe))
                        .addModifiers(PUBLIC);

                // 声明变量 private SerializationService serializationService;
                FieldSpec jsonServiceField = FieldSpec.builder(TypeName.get(type_JsonService.asType()), "serializationService", Modifier.PRIVATE).build();
                helper.addField(jsonServiceField);
  
                // 生成代码 : serializationService = (SerializationService)ARouter.getInstance().navigation(SerializationService.class);
                injectMethodBuilder.addStatement("serializationService = $T.getInstance().navigation($T.class);", ARouterClass, ClassName.get(type_JsonService));

                // 生成代码 : Test1Activity substitute = (Test1Activity)target;
                injectMethodBuilder.addStatement("$T substitute = ($T)target", ClassName.get(parent), ClassName.get(parent));

                // 生成自动注入的代码
                for (Element element : childs) {
                    Autowired fieldConfig = element.getAnnotation(Autowired.class);
                    String fieldName = element.getSimpleName().toString();
                    if (types.isSubtype(element.asType(), iProvider)) {  // It's provider
                        if ("".equals(fieldConfig.name())) {    // User has not set service path, then use byType.

                            // 生成代码 : substitute.helloService = (HelloService)ARouter.getInstance().navigation(HelloService.class);
                            injectMethodBuilder.addStatement(
                                    "substitute." + fieldName + " = $T.getInstance().navigation($T.class)",
                                    ARouterClass,
                                    ClassName.get(element.asType())
                            );
                        } else {
                            // 生成代码 : substitute.helloService = (HelloService)ARouter.getInstance().build("/test/hello").navigation();
                            injectMethodBuilder.addStatement(
                                    "substitute." + fieldName + " = ($T)$T.getInstance().build($S).navigation();",
                                    ClassName.get(element.asType()),
                                    ARouterClass,
                                    fieldConfig.name()
                            );
                        }

                    } else {  
                        String statment = "substitute." + fieldName + " = substitute.";
                        boolean isActivity = false;
                        if (types.isSubtype(parent.asType(), activityTm)) {
                            isActivity = true;
                            // activity  直接用getIntent()
                            statment += "getIntent().";
                        } else if (types.isSubtype(parent.asType(), fragmentTm) || types.isSubtype(parent.asType(), fragmentTmV4)) {
                            // fragment  直接用getArguments()
                            statment += "getArguments().";
                        } else {
                            throw new IllegalAccessException("The field [" + fieldName + "] need autowired from intent, its parent must be activity or fragment!");
                        }
        
                        // 生成注入代码
                        statment = buildStatement(statment, typeUtils.typeExchange(element), isActivity);

                        // 判断serializationService注入
                        if (statment.startsWith("serializationService.")) {   // Not mortals
                            injectMethodBuilder.beginControlFlow("if (null != serializationService)");
                            injectMethodBuilder.addStatement(
                                    "substitute." + fieldName + " = " + statment,
                                    (StringUtils.isEmpty(fieldConfig.name()) ? fieldName : fieldConfig.name()),
                                    ClassName.get(element.asType())
                            );
                            injectMethodBuilder.nextControlFlow("else");
                            injectMethodBuilder.addStatement(
                                    "$T.e(\"" + Consts.TAG +  "\", \"You want automatic inject the field '" + fieldName + "' in class '$T' , then you should implement 'SerializationService' to support object auto inject!\")", AndroidLog, ClassName.get(parent));
                            injectMethodBuilder.endControlFlow();
                        } else {
                            injectMethodBuilder.addStatement(statment, StringUtils.isEmpty(fieldConfig.name()) ? fieldName : fieldConfig.name());
                        }
                    }
                }


                helper.addMethod(injectMethodBuilder.build());

                // 生成 Autowired 的注入代码, 继承了 ISyringe. 例如 Test1Activity$$ARouter$$Autowired.java
                JavaFile.builder(packageName, helper.build()).build().writeTo(mFiler);
            }
    }
点赞