android 深度链接,Android:从零开始打造自己的深度链接库(二):ARouter源码解析...

前言

这一篇我们来具体看一下ARouter的实现原理,如果你之前没有接触过ARouter,可以先阅读上一篇:

Android:从零开始打造自己的深度链接库(一):ARouter简介

废话不多,我们赶紧分析源码。

正文

首先我们从github下载最新的源码:

56930b18cb53

image

被选中的三个Module是我们今天分析的重点:

arouter-annotation

从名称看我们可以猜到这是自定义注解的库,我们就直接截个图:

56930b18cb53

arouter-annotation

我们看到了之前使用过的@Router,@Autowired,@Interceptor,这个库就直接略过。

arouter-compiler

我们先思考一个问题:

为什么通过注解,我们就可以跳转到指定的页面呢,ARouter是怎么做到的?

如果你之前看过EventBus,或者LitePal的源码,这一篇对于你来说就是小case。因为核心的原理都是一样的。

@AutoService(Processor.class)

@SupportedOptions(KEY_MODULE_NAME)

@SupportedSourceVersion(SourceVersion.RELEASE_7)

@SupportedAnnotationTypes({ANNOTATION_TYPE_AUTOWIRED})

public class AutowiredProcessor extends AbstractProcessor

要想编译时处理注解,需要实现对应的AbstractProcessor,并使用@AutoService(Processor.class)注解,这样在编译时,这个注解解析器就会被执行,我们可以在编译时生成源文件来保存必要的信息,这样做的好处是,把注解解析的成本放在编译期间,从而节省了运行时的时间,对于用户来说,这样的体验是值得推荐的。

我们以AutowiredProcessor为例,分析一下编译期间ARouter到底做了哪些工作:

@Override

public boolean process(Set extends TypeElement> set, RoundEnvironment roundEnvironment) {

if (CollectionUtils.isNotEmpty(set)) {

try {

logger.info(">>> Found autowired field, start... <<

// 取出Autowired.class

categories(roundEnvironment.getElementsAnnotatedWith(Autowired.class));

// 生成帮助文件

generateHelper();

} catch (Exception e) {

logger.error(e);

}

// 注解处理完毕返回true

return true;

}

return false;

}

直接的解析工作需要重写process方法,从所有的注解中取出需要解析的注解集合,先看看这个categories()方法做了哪些处理:

private void categories(Set extends Element> elements) throws IllegalAccessException {

// 判断使用了Autowired注解是否为空

if (CollectionUtils.isNotEmpty(elements)) {

for (Element element : elements) {

// 得到被注解的元素的父级元素

// 例如被注解的是一个属性,那么得到的就是这个类

TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();

// 被注解的元素不可以被private修饰

if (element.getModifiers().contains(Modifier.PRIVATE)) {

throw new IllegalAccessException("The inject fields CAN NOT BE 'private'!!! please check field ["

+ element.getSimpleName() + "] in class [" + enclosingElement.getQualifiedName() + "]");

}

// 因为编译解析器,可能被调用多次,所以这里使用一个键值对组合保存了之前解析过的信息,提高执行效率

// 如果已经解析过了,找到之前的key,直接添加到集合中

if (parentAndChild.containsKey(enclosingElement)) { // Has categries

parentAndChild.get(enclosingElement).add(element);

}

// 没有解析过,保存到集合中

else {

List childs = new ArrayList<>();

childs.add(element);

parentAndChild.put(enclosingElement, childs);

}

}

logger.info("categories finished.");

}

}

上面的代码注释已经写的很详细了,这里我们了解到了新的信息:

@Autowired不能修饰private属性!!!

接下来我们分析一下generateHelper()方法,因为代码比较多,所以我把几个核心的地方单独分析,刚才我们已经把每一个元素和对应的@AutoWired注解都保存了起来,那肯定是要遍历就这个集合了:

for (Map.Entry> entry : parentAndChild.entrySet()) {

// 创建inject方法

MethodSpec.Builder injectMethodBuilder = MethodSpec.methodBuilder(METHOD_INJECT)

.addAnnotation(Override.class)

.addModifiers(PUBLIC)

.addParameter(objectParamSpec);

// 使用了注解的类

TypeElement parent = entry.getKey();

// 被注解修饰的元素

List childs = entry.getValue();

// 类名

String qualifiedName = parent.getQualifiedName().toString();

// 包名

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

// 要生成的源文件的名称

String fileName = parent.getSimpleName() + NAME_OF_AUTOWIRED;

// 对源文件添加public修饰符,添加要实现的接口,和注释

TypeSpec.Builder helper = TypeSpec.classBuilder(fileName)

.addJavadoc(WARNING_TIPS)

.addSuperinterface(ClassName.get(type_ISyringe))

.addModifiers(PUBLIC);

// 添加一个私有的serializationService属性

FieldSpec jsonServiceField = FieldSpec.builder(TypeName.get(type_JsonService.asType()), "serializationService", Modifier.PRIVATE).build();

helper.addField(jsonServiceField);

// 为刚才创建的inject方法添加代码

// serializationService = ARouter.getInstance().navigation(SerializationService.class)

injectMethodBuilder.addStatement("serializationService = $T.getInstance().navigation($T.class)", ARouterClass, ClassName.get(type_JsonService));

// ClassName.get(parent)类名

injectMethodBuilder.addStatement("$T substitute = ($T)target", ClassName.get(parent), ClassName.get(parent));

// 遍历被注解修饰的元素列表

// 此处先省略

for (Element element : childs) {

...

}

// 源文件添加方法

helper.addMethod(injectMethodBuilder.build());

// 生成源文件

JavaFile.builder(packageName, helper.build()).build().writeTo(mFiler);

}

果然这里要开始生成源文件了,首先定义了要生成的文件的名称和位置(包名),然后创建一个public的inject方法,此处添加了一个SerializationService服务,这个服务是序列化的服务,我们可以先忽略。

然后就压开始遍历被@Autowired注解的元素集合了:

Autowired fieldConfig = element.getAnnotation(Autowired.class);

// 被注解的属性的名称

String fieldName = element.getSimpleName().toString();

// 如果这是一个IProvide的子类

if (types.isSubtype(element.asType(), iProvider)) {

// 如果在注解中特别指定了名称

if ("".equals(fieldConfig.name())) { // User has not set service path, then use byType.

// 在inject方法中使用属性名,获取属性对应的实例

injectMethodBuilder.addStatement(

"substitute." + fieldName + " = $T.getInstance().navigation($T.class)",

ARouterClass,

ClassName.get(element.asType())

);

} else {

// 使用注解中的名称找到对应的实例

injectMethodBuilder.addStatement(

"substitute." + fieldName + " = ($T)$T.getInstance().build($S).navigation()",

ClassName.get(element.asType()),

ARouterClass,

fieldConfig.name()

);

}

// 判断该属性是否是必须,如果没有找到可以实例化的对象,注解抛出错误

if (fieldConfig.required()) {

injectMethodBuilder.beginControlFlow("if (substitute." + fieldName + " == null)");

injectMethodBuilder.addStatement(

"throw new RuntimeException(\"The field '" + fieldName + "' is null, in class '\" + $T.class.getName() + \"!\")", ClassName.get(parent));

injectMethodBuilder.endControlFlow();

}

}

else{

...

}

首先判断被@Autowired注解的属性是否是一个IProvider接口,IProvider接口表示一个对外开放的服务,不同Module可以通过IProvider得到对应的服务,这样不同Module之间就可以进行通信。

String originalValue = "substitute." + fieldName;

String statement = "substitute." + fieldName + " = " + buildCastCode(element) + "substitute.";

boolean isActivity = false;

// 是否是Activity的子类

if (types.isSubtype(parent.asType(), activityTm)) { // Activity, then use getIntent()

isActivity = true;

// 可以看Activity要通过intent进行属性的初始化

statement += "getIntent().";

}

// 是否是Frament的子类

else if (types.isSubtype(parent.asType(), fragmentTm) || types.isSubtype(parent.asType(), fragmentTmV4)) { // Fragment, then use getArguments()

// 可以看Activity要通过getArguments进行属性的初始化

statement += "getArguments().";

}

// 从此判断可以看出,@Autowired注解的类型只有IProvder,Activity和Fragment

else {

throw new IllegalAccessException("The field [" + fieldName + "] need autowired from intent, its parent must be activity or fragment!");

}

// 根据对应的类型,找到需要的类型

// 例如getInt(),getBoolean()

statement = buildStatement(originalValue, statement, typeUtils.typeExchange(element), isActivity);

// 如果是一个对象,我们要对应serializationService来实现初始化

if (statement.startsWith("serializationService.")) { // Not mortals

injectMethodBuilder.beginControlFlow("if (null != serializationService)");

injectMethodBuilder.addStatement(

"substitute." + fieldName + " = " + statement,

(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 {

njectMethodBuilder.addStatement(statement, StringUtils.isEmpty(fieldConfig.name()) ? fieldName : fieldConfig.name());

}

// 非空检查

if (fieldConfig.required() && !element.asType().getKind().isPrimitive()) { // Primitive wont be check.

injectMethodBuilder.beginControlFlow("if (null == substitute." + fieldName + ")");

injectMethodBuilder.addStatement(

"$T.e(\"" + Consts.TAG + "\", \"The field '" + fieldName + "' is null, in class '\" + $T.class.getName() + \"!\")", AndroidLog, ClassName.get(parent));

injectMethodBuilder.endControlFlow();

}

上面的代码解析是IProvider以外的情况,通过分析我们又学到了新的知识:

@Autowired只能直接IProvider对象,或者是Activity和Fragment中使用!!!

并且知道Activity和Fragment的中的属性是通过intent和arguments来初始化的,我们已经完整的分析了整个@Autowired直接的解析过程,但是仍然有些懵逼,因为是源文件是通过代码写入的,所以最直观的方法就是看看源文件到底生成了什么。

首先看IProvider:

// 测试服务,其中HelloService实现了IProvider接口

public class TestService {

@Autowired

public HelloService helloService;

public TestService(){

ARouter.getInstance().inject(this);

}

}

// 以下是生成的源文件

/**

* DO NOT EDIT THIS FILE!!! IT WAS GENERATED BY AROUTER. */

public class TestService$$ARouter$$Autowired implements ISyringe {

private SerializationService serializationService;

@Override

public void inject(Object target) {

serializationService = ARouter.getInstance().navigation(SerializationService.class);

TestService substitute = (TestService)target;

substitute.helloService = ARouter.getInstance().navigation(HelloService.class);

}

}

Activity:

@Route(path = "/main/TestActivity", extras = 1)

public class TestActivity extends AppCompatActivity {

@Autowired

String name;

@Autowired

int age;

@Autowired

TestBean testBean;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_test);

ARouter.getInstance().inject(this);

}

}

// 生成文件代码

public class TestActivity$$ARouter$$Autowired implements ISyringe {

private SerializationService serializationService;

@Override

public void inject(Object target) {

serializationService = ARouter.getInstance().navigation(SerializationService.class);

TestActivity substitute = (TestActivity)target;

substitute.name = substitute.getIntent().getStringExtra("name");

substitute.age = substitute.getIntent().getIntExtra("age", substitute.age);

substitute.testBean = (com.lzp.arouter.activity.bean.TestBean) substitute.getIntent().getSerializableExtra("testBean");

}

}

@Autowired注解已经分析完毕,通过同样的道理,我们还可以自行查看RouteProcessor,InterceptorProcessor的内容,最终都是生成了各种源文件保存注解中的信息,这里就不具体分析了。

arouter-api

这个就是ARouter的核心库了,通过刚才分析编译库的内容,我们可以很轻松的知道@Autowired的执行过程,例如:

@Route(path = "/main/TestActivity", extras = 1)

public class TestActivity extends AppCompatActivity {

@Autowired

String name;

@Autowired

int age;

@Autowired

TestBean testBean;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_test);

// 必须调用该方法,才有有效

ARouter.getInstance().inject(this);

}

}

我们找到inject的最终调用层级在:

static void inject(Object thiz) {

AutowiredService autowiredService = ((AutowiredService) ARouter.getInstance().build("/arouter/service/autowired").navigation());

if (null != autowiredService) {

autowiredService.autowire(thiz);

}

}

根据路由地址“/arouter/service/autowired”,我们在库中找到了AutowiredServiceImpl:

@Route(path = "/arouter/service/autowired")

public class AutowiredServiceImpl implements AutowiredService {

...

@Override

public void autowire(Object instance) {

String className = instance.getClass().getName();

try {

if (!blackList.contains(className)) {

ISyringe autowiredHelper = classCache.get(className);

if (null == autowiredHelper) { // No cache.

// 通过源文件名称的生成规则,找到生成的源文件

autowiredHelper = (ISyringe) Class.forName(instance.getClass().getName() + SUFFIX_AUTOWIRED).getConstructor().newInstance();

}

// 调用源文件的inject方法,完成自动装配的工作

autowiredHelper.inject(instance);

classCache.put(className, autowiredHelper);

}

} catch (Exception ex) {

blackList.add(className); // This instance need not autowired.

}

}

}

@Autowired的运行流程就是这样,其中最重要的就是

ARouter.getInstance().build("/arouter/service/autowired").navigation());

我们发现无论是Activity,Fragment还是Service,都是通过这个方法进行匹配的,搞定了他,ARouter的api库就算是大功告成了。

ARouter类的功能都是委托_ARouter类实现的,navigation方法被重载了很多种,但是核心的步骤只有三个:

1、找到路由地址,组装成Postcard对象

2、调用LogisticsCenter.completion(postcard),完善Postcard的信息

3、返回指定的对象或跳转。

1、找到路由地址,组装成Postcard对象

如果我们直接指定了路由地址,例如:

ARouter.getInstance().build("/arouter/service/autowired").navigation());

// 创建对应的Postcard

protected Postcard build(String path, String group) {

if (TextUtils.isEmpty(path) || TextUtils.isEmpty(group)) {

throw new HandlerException(Consts.TAG + "Parameter is invalid!");

} else {

// 找到是否有自定义PathReplaceService

PathReplaceService pService = ARouter.getInstance().navigation(PathReplaceService.class);

if (null != pService) {

path = pService.forString(path);

}

// 默认是没有的,所以会进入到这里

// 如果没有指定group,默认的分组是第一个斜线的单词

return new Postcard(path, group);

}

}

如果我们没有指定url地址,而是使用Class:

protected T navigation(Class extends T> service) {

try {

// 通过编译生成的文件得到Postcard

Postcard postcard = LogisticsCenter.buildProvider(service.getName());

// 注释已经写了,是为了适配1.0.5的版本

// Compatible 1.0.5 compiler sdk.

// Earlier versions did not use the fully qualified name to get the service

if (null == postcard) {

// No service, or this service in old version.

postcard = LogisticsCenter.buildProvider(service.getSimpleName());

}

if (null == postcard) {

return null;

}

// 这是第二步了

LogisticsCenter.completion(postcard);

return (T) postcard.getProvider();

} catch (NoRouteFoundException ex) {

logger.warning(Consts.TAG, ex.getMessage());

return null;

}

}

我们编译出的文件已经记录了Class和路由的对应信息,直接就可以使用。

调用LogisticsCenter.completion(postcard)

这个方法会通过Postcard中的路径信息,完成页面的查找操作:

public synchronized static void completion(Postcard postcard) {

if (null == postcard) {

throw new NoRouteFoundException(TAG + "No postcard!");

}

// 得到RouteMeta信息,这个在编译器中已经生成了

RouteMeta routeMeta = Warehouse.routes.get(postcard.getPath());

// 一个空的检查,这里就先忽略了

if (null == routeMeta) { // Maybe its does't exist, or didn't load.

...

} else {

// 通过RouteMeta,完善Postcard的信息

// 要找到的类名

postcard.setDestination(routeMeta.getDestination());

// 路由的类型

postcard.setType(routeMeta.getType());

// 优先级

postcard.setPriority(routeMeta.getPriority());

// extra,注解中的extra

postcard.setExtra(routeMeta.getExtra());

// 通过uri,解析路由的参数

Uri rawUri = postcard.getUri();

if (null != rawUri) { // Try to set params into bundle.

Map resultMap = TextUtils.splitQueryParameters(rawUri);

Map paramsType = routeMeta.getParamsType();

if (MapUtils.isNotEmpty(paramsType)) {

// 添加参数

for (Map.Entry params : paramsType.entrySet()) {

setValue(postcard,

params.getValue(),

params.getKey(),

resultMap.get(params.getKey()));

}

postcard.getExtras().putStringArray(ARouter.AUTO_INJECT, paramsType.keySet().toArray(new String[]{}));

}

postcard.withString(ARouter.RAW_URI, rawUri.toString());

}

// 根据不同的类型,做一些补充操作

switch (routeMeta.getType()) {

// 如果是IProvider,这里已经直接通过反射创建对象了

case PROVIDER: // if the route is provider, should find its instance

// Its provider, so it must implement IProvider

Class extends IProvider> providerMeta = (Class extends IProvider>) routeMeta.getDestination();

IProvider instance = Warehouse.providers.get(providerMeta);

if (null == instance) { // There's no instance of this provider

IProvider provider;

try {

provider = providerMeta.getConstructor().newInstance();

provider.init(mContext);

Warehouse.providers.put(providerMeta, provider);

instance = provider;

} catch (Exception e) {

throw new HandlerException("Init provider failed! " + e.getMessage());

}

}

// 设置属性,之后通过这个属性,得到创建的对象

postcard.setProvider(instance);

// greenChannel之前已经说过了,表示跳过interceptor

postcard.greenChannel(); // Provider should skip all of interceptors

break;

case FRAGMENT:

// fragment也是跳过interceptor的

postcard.greenChannel(); // Fragment needn't interceptors

default:

break;

}

}

}

返回指定的对象或跳转

在第二步我们已经完善了Postcard的信息,接下来是第三步,这里有两种情况,如果我们直接使用的是:

protected T navigation(Class extends T> service) {

try {

// 通过编译生成的文件得到Postcard

Postcard postcard = LogisticsCenter.buildProvider(service.getName());

// 注释已经写了,是为了适配1.0.5的版本

// Compatible 1.0.5 compiler sdk.

// Earlier versions did not use the fully qualified name to get the service

if (null == postcard) {

// No service, or this service in old version.

postcard = LogisticsCenter.buildProvider(service.getSimpleName());

}

if (null == postcard) {

return null;

}

// 完善Postcard信息

LogisticsCenter.completion(postcard);

// 返回指定对象

return (T) postcard.getProvider();

} catch (NoRouteFoundException ex) {

logger.warning(Consts.TAG, ex.getMessage());

return null;

}

}

通过刚才第二步的分析,我们可以推断出: navigation(Class extends T> service)仅仅适用于IProvider。

其他情况会调用:

protected Object navigation(final Context context, final Postcard postcard, final int requestCode, final NavigationCallback callback) {

try {

// 第二步,完善Postcard信息

LogisticsCenter.completion(postcard);

} catch (NoRouteFoundException ex) {

// 此处省略,会打印信息,回调callback的onlost方法

...

return null;

}

// 回调onFound

if (null != callback) {

callback.onFound(postcard);

}

// 是否要被拦截

if (!postcard.isGreenChannel()) {

interceptorService.doInterceptions(postcard, new InterceptorCallback() {

@Override

public void onContinue(Postcard postcard) {

// 继续

_navigation(context, postcard, requestCode, callback);

}

@Override

public void onInterrupt(Throwable exception) {

if (null != callback) {

callback.onInterrupt(postcard);

}

}

});

} else {

// 继续

return _navigation(context, postcard, requestCode, callback);

}

return null;

}

如果跳过或通过了拦截器就会调用_navigation(context, postcard, requestCode, callback)方法,这也是最后一个分析的方法了:

private Object _navigation(final Context context, final Postcard postcard, final int requestCode, final NavigationCallback callback) {

final Context currentContext = null == context ? mContext : context;

switch (postcard.getType()) {

// 找到Activity,intent添加参数,startActivity

case ACTIVITY:

// Build intent

final Intent intent = new Intent(currentContext, postcard.getDestination());

intent.putExtras(postcard.getExtras());

// Set flags.

int flags = postcard.getFlags();

if (-1 != flags) {

intent.setFlags(flags);

} else if (!(currentContext instanceof Activity)) { // Non activity, need less one flag.

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

}

// Set Actions

String action = postcard.getAction();

if (!TextUtils.isEmpty(action)) {

intent.setAction(action);

}

// 切换到主线程

runInMainThread(new Runnable() {

@Override

public void run() {

startActivity(requestCode, currentContext, intent, postcard, callback);

}

});

break;

// 在第二步已经创建IProvider了,直接返回就可以了

case PROVIDER:

return postcard.getProvider();

// 广播,Provider,Fragment

case BOARDCAST:

case CONTENT_PROVIDER:

case FRAGMENT:

Class fragmentMeta = postcard.getDestination();

try {

// 通过返回创建指定对象,请注意这是无参构造方法

Object instance = fragmentMeta.getConstructor().newInstance();

// Fragment要添加参数

if (instance instanceof Fragment) {

((Fragment) instance).setArguments(postcard.getExtras());

} else if (instance instanceof android.support.v4.app.Fragment) {

((android.support.v4.app.Fragment) instance).setArguments(postcard.getExtras());

}

return instance;

} catch (Exception ex) {

logger.error(Consts.TAG, "Fetch fragment instance error, " + TextUtils.formatStackTrace(ex.getStackTrace()));

}

case METHOD:

case SERVICE:

default:

return null;

}

return null;

}

到这里ARouter的源码分析就到此结束了。

总结

经过漫长的分析,我们了解了ARouter的实现原理,还学到了文档可能不会提及的用法,最后我们做一个总结:

@Autowired只能注解非private属性

@Autowired只能注解IProvider,或者在Activity和Fragment中使用。

@Autowired注解IProvider会反射创建对象,Activity和Fragment中则通过intent和argument中的携带参数进行填充。

navigation(Class clazz)方法仅适用于IProvider

IProvider和Fragment默认跳过拦截器

如果interceptor要进行耗时操作,请开启异步线程,在主线程中可能会引起ANR

下一篇:Android:从零开始打造自己的深度链接库(三):自定义XML协议

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值