compiler:编译
RouteProcessor
AbstractProcessor
RouteMeta RouteType Activity
JavaFile.build
PROVIDER Service Fragment RouteMeta
init
processingEnv.getOptions //获取用户配置的【moduleName】
annotationProcessorOptions
arguments = AROUTER_MODULE_NAME
public static final String KEY_MODULE_NAME = "AROUTER_MODULE_NAME";
process
NAME_OF_ROOT NAME_OF_PROVIDER NAME_OF_AUTOWIRED NAME_OF_INTERCEPTOR
NAME_OF_GROUP
Activity Fragment Fragment SERVICE PARCEABLE
获取所有添加Route注解的元素
TypeMirror
// Inline:
// @Column(name = "updated_at", nullable = false)
//
// Not inline:
// @Column(
// name = "updated_at",
// nullable = false
// )
public final class AnnotationSpec {
RouteMeta
RouteMeta封装了路由相关的信息
```Map<String, RouteMeta>```
*/
ParameterizedTypeName inputMapTypeOfGroup = ParameterizedTypeName.get(
ClassName.get(Map.class),
ClassName.get(String.class),
ClassName.get(RouteMeta.class)
);
ClassName ParameterizedTypeName ParameterSpec MethodSpec
// 创建出来的函数如下
/**
* @Override
* public void loadInto(Map<String, Class<? extends IRouteGroup>> routes) { }
*/
IRouteGroup loadInto String RouteMeta atlas
public class ARouter$$Group$$main implements IRouteGroup {
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,
routeMeta.getPath().toLowerCase(),
routeMeta.getGroup().toLowerCase());
@Override
public void loadInto(Map<String, RouteMeta> atlas) {
atlas.put("/main/Main", RouteMeta.build(RouteType.ACTIVITY, MainActivity.class, "/main/main", "main", null, -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,
routeMeta.getPath().toLowerCase(),
routeMeta.getGroup().toLowerCase());
IRouteRoot loadInto Map Class IROUTE_GROUP
public class ARouter$$Root$$moduleuser implements IRouteRoot {
@Override
public void loadInto(Map<String, Class<? extends IRouteGroup>> routes) {
routes.put("service", ARouter$$Group$$service.class);
routes.put("user", ARouter$$Group$$user.class);
}
}
ARouter$$Root$$moduleuser
public class ARouter$$Providers$$modulemain implements IProviderGroup {
@Override
public void loadInto(Map<String, RouteMeta> providers) {
}
}
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);
JavaFile build 之后
/**
* DO NOT EDIT THIS FILE!!! IT WAS GENERATED BY AROUTER. */
public class ARouter$$Root$$moduleuser implements IRouteRoot {
@Override
public void loadInto(Map<String, Class<? extends IRouteGroup>> routes) {
routes.put("service", ARouter$$Group$$service.class);
routes.put("user", ARouter$$Group$$user.class);
}
}
InterceptorProcessor IInterceptorGroup loadInto Class
? extends IInterceptor
AutowireProcessor
ParameterSpec
MethodSpec TypeSpec FieldSpec
MethodSpec:
public static Builder methodBuilder(String name) {
return new Builder(name);
}
Builder:
public MethodSpec build() {
return new MethodSpec(this);
}
MeasureSpec:
private MethodSpec(Builder builder) {