mFiler = processingEnv.getFiler(); // Generate class. types = processingEnv.getTypeUtils(); // Get type utils. elements = processingEnv.getElementUtils(); // Get class meta.
// Attempt to get user configuration [moduleName] Map<String, String> options = processingEnv.getOptions(); if (MapUtils.isNotEmpty(options)) { moduleName = options.get(KEY_MODULE_NAME); generateDoc = VALUE_ENABLE.equals(options.get(KEY_GENERATE_DOC_NAME)); }
if (StringUtils.isNotEmpty(moduleName)) { moduleName = moduleName.replaceAll("[^0-9a-zA-Z_]+", "");
logger.info("The user has configuration the module name, it was [" + moduleName + "]"); } else { logger.error(NO_MODULE_NAME_TIPS); thrownewRuntimeException("ARouter::Compiler >>> No module name, for more information, look at gradle log."); }
// Follow a sequence, find out metas of group first, generate java file, then statistics them as root. for (Element element : routeElements) { TypeMirrortm= element.asType(); Routeroute= element.getAnnotation(Route.class); RouteMeta routeMeta;
// 如果 element 修饰的类是 Activity 类型的 if (types.isSubtype(tm, type_Activity)) { // Activity logger.info(">>> Found activity route: " + tm.toString() + " <<<");
// 获取 Activity 中 @Autowired 注解的属性,IProvider 类型的除外 Map<String, Integer> paramsType = newHashMap<>(); Map<String, Autowired> injectConfig = newHashMap<>(); for (Element field : element.getEnclosedElements()) { if (field.getKind().isField() && field.getAnnotation(Autowired.class) != null && !types.isSubtype(field.asType(), iProvider)) { // It must be field, then it has annotation, but it not be provider. AutowiredparamConfig= field.getAnnotation(Autowired.class); StringinjectName= StringUtils.isEmpty(paramConfig.name()) ? field.getSimpleName().toString() : paramConfig.name(); paramsType.put(injectName, typeUtils.typeExchange(field)); injectConfig.put(injectName, paramConfig); } } // 构造 activity 类型的路由数据 routeMeta = newRouteMeta(route, element, RouteType.ACTIVITY, paramsType); routeMeta.setInjectConfig(injectConfig); } elseif (types.isSubtype(tm, iProvider)) { // IProvider 类型 logger.info(">>> Found provider route: " + tm.toString() + " <<<"); routeMeta = newRouteMeta(route, element, RouteType.PROVIDER, null); } elseif (types.isSubtype(tm, type_Service)) { // Service 类型 logger.info(">>> Found service route: " + tm.toString() + " <<<"); routeMeta = newRouteMeta(route, element, RouteType.parse(SERVICE), null); } elseif (types.isSubtype(tm, fragmentTm) || types.isSubtype(tm, fragmentTmV4)) { // fragment 类型 logger.info(">>> Found fragment route: " + tm.toString() + " <<<"); routeMeta = newRouteMeta(route, element, RouteType.parse(FRAGMENT), null); } else { thrownewRuntimeException("ARouter::Compiler >>> Found unsupported class type, type = [" + types.toString() + "]."); } // 将生成好的 routeMeta 按组存放进入 groupMap 中 categories(routeMeta); }
// Start generate java source, structure is divided into upper and lower levels, used for demand initialization. for (Map.Entry<String, Set<RouteMeta>> entry : groupMap.entrySet()) { // 每组的组名 StringgroupName= entry.getKey();
// Build group method body Set<RouteMeta> groupData = entry.getValue(); for (RouteMeta routeMeta : groupData) { RouteDocrouteDoc= extractDocInfo(routeMeta); // 类名。比如 com.alibaba.android.arouter.demo.testservice.HelloService ClassNameclassName= ClassName.get((TypeElement) routeMeta.getRawType());
switch (routeMeta.getType()) { case PROVIDER: // Need cache provider's super class // 获取该节点下的接口 List<? extendsTypeMirror> interfaces = ((TypeElement) routeMeta.getRawType()).getInterfaces(); // 遍历接口 for (TypeMirror tm : interfaces) { routeDoc.addPrototype(tm.toString()); // 如果接口是 iProvider 类型 if (types.isSameType(tm, iProvider)) { // Its implements iProvider interface himself. // This interface extend the IProvider, so it can be used for mark provider 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, routeMeta.getPath(), routeMeta.getGroup()); } elseif (types.isSubtype(tm, iProvider)) { // 如果是 iProvider 的子接口 // This interface extend the IProvider, so it can be used for mark provider loadIntoMethodOfProviderBuilder.addStatement( "providers.put($S, $T.build($T." + routeMeta.getType() + ", $T.class, $S, $S, null, " + routeMeta.getPriority() + ", " + routeMeta.getExtra() + "))", tm.toString(), // So stupid, will duplicate only save class name. routeMetaCn, routeTypeCn, className, routeMeta.getPath(), routeMeta.getGroup()); } } break; default: break; }
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. // 生成 ARouter$$Root$$app 的 loadInto 方法体 for (Map.Entry<String, String> entry : rootMap.entrySet()) { loadIntoMethodOfRootBuilder.addStatement("routes.put($S, $T.class)", entry.getKey(), ClassName.get(PACKAGE_OF_GENERATE_FILE, entry.getValue())); } }