安卓AOP三劍客之Android APT技術(shù)淺談
通過(guò)學(xué)習(xí)與使用square公司的開(kāi)源項(xiàng)目javapoet,來(lái)實(shí)現(xiàn)倉(cāng)庫(kù)層動(dòng)態(tài)生成代碼
安卓AOP三劍客: APT, AspectJ, Javassist
Android APT
APT(Annotation Processing Tool 的簡(jiǎn)稱),可以在代碼編譯期解析注解,并且生成新的 Java 文件,減少手動(dòng)的代碼輸入。現(xiàn)在有很多主流庫(kù)都用上了 APT,比如 Dagger2, ButterKnife, EventBus3 等
代表框架:
- DataBinding
- Dagger2
- ButterKnife
- EventBus3
- DBFlow
- AndroidAnnotation
使用姿勢(shì)
1,在android工程中,創(chuàng)建一個(gè)java的Module,寫一個(gè)類繼承AbstractProcessor
- @AutoService(Processor.class) // javax.annotation.processing.IProcessor
- @SupportedSourceVersion(SourceVersion.RELEASE_7) //java
- @SupportedAnnotationTypes({ // 標(biāo)注注解處理器支持的注解類型
- "com.annotation.SingleDelegate",
- "com.annotation.MultiDelegate"
- })
- public class AnnotationProcessor extends AbstractProcessor {
- public static final String PACKAGE = "com.poet.delegate";
- public static final String CLASS_DESC = "From poet compiler";
- public Filer filer; //文件相關(guān)的輔助類
- public Elements elements; //元素相關(guān)的輔助類
- public Messager messager; //日志相關(guān)的輔助類
- public Types types;
- @Override
- public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) {
- filer = processingEnv.getFiler();
- elements = processingEnv.getElementUtils();
- messager = processingEnv.getMessager();
- types = processingEnv.getTypeUtils();
- new SingleDelegateProcessor().process(set, roundEnvironment, this);
- new MultiDelegateProcessor().process(set, roundEnvironment, this);
- return true;
- }
- }
2,在繼承AbstractProcessor類中的process方法,處理我們自定義的注解,生成代碼:
- public class SingleDelegateProcessor implements IProcessor {
- @Override
- public void process(Set<? extends TypeElement> set, RoundEnvironment roundEnv,
- AnnotationProcessor abstractProcessor) {
- // 查詢注解是否存在
- Set<? extends Element> elementSet =
- roundEnv.getElementsAnnotatedWith(SingleDelegate.class);
- Set<TypeElement> typeElementSet = ElementFilter.typesIn(elementSet);
- if (typeElementSet == null || typeElementSet.isEmpty()) {
- return;
- }
- // 循環(huán)處理注解
- for (TypeElement typeElement : typeElementSet) {
- if (!(typeElement.getKind() == ElementKind.INTERFACE)) { // 只處理接口類型
- continue;
- }
- // 處理 SingleDelegate,只處理 annotation.classNameImpl() 不為空的注解
- SingleDelegate annotation = typeElement.getAnnotation(SingleDelegate.class);
- if ("".equals(annotation.classNameImpl())) {
- continue;
- }
- Delegate delegate = annotation.delegate();
- // 添加構(gòu)造器
- MethodSpec.Builder constructorBuilder = MethodSpec.constructorBuilder()
- .addModifiers(Modifier.PUBLIC);
- // 創(chuàng)建類名相關(guān) class builder
- TypeSpec.Builder builder =
- ProcessUtils.createTypeSpecBuilder(typeElement, annotation.classNameImpl());
- // 處理 delegate
- builder = ProcessUtils.processDelegate(typeElement, builder,
- constructorBuilder, delegate);
- // 檢查是否繼承其它接口
- builder = processSuperSingleDelegate(abstractProcessor, builder, constructorBuilder, typeElement);
- // 完成構(gòu)造器
- builder.addMethod(constructorBuilder.build());
- // 創(chuàng)建 JavaFile
- JavaFile javaFile = JavaFile.builder(AnnotationProcessor.PACKAGE, builder.build()).build();
- try {
- javaFile.writeTo(abstractProcessor.filer);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
3,在項(xiàng)目Gradle中添加 annotationProcessor project 引用
- compile project(':apt-delegate-annotation')
- annotationProcessor project(':apt-delegate-compiler')
4,如果有自定義注解的話,創(chuàng)建一個(gè)java的Module,專門放入自定義注解。項(xiàng)目與apt Module都需引用自定義注解Module
4-1,主工程:
- compile project(':apt-delegate-annotation')
- annotationProcessor project(':apt-delegate-compiler')
4-2,apt Module:
- compile project(':apt-delegate-annotation')
- compile 'com.google.auto.service:auto-service:1.0-rc2'
- compile 'com.squareup:javapoet:1.4.0'
5,生成的源代碼在build/generated/source/apt下可以看到
難點(diǎn)
就apt本身來(lái)說(shuō)沒(méi)有任何難點(diǎn)可言,難點(diǎn)一在于設(shè)計(jì)模式和解耦思想的靈活應(yīng)用,二在與代碼生成的繁瑣,你可以手動(dòng)字符串拼接,當(dāng)然有更高級(jí)的玩法用squareup的javapoet,用建造者的模式構(gòu)建出任何你想要的源代碼
優(yōu)點(diǎn)
它的強(qiáng)大之處無(wú)需多言,看代表框架的源碼,你可以學(xué)到很多新姿勢(shì)。總的一句話:它可以做任何你不想做的繁雜的工作,它可以幫你寫任何你不想重復(fù)代碼。懶人福利,老司機(jī)必備神技,可以提高車速,讓你以任何姿勢(shì)漂移。它可以生成任何源代碼供你在任何地方使用,就像劍客的劍,快疾如風(fēng),無(wú)所不及
我想稍微研究一下,APT還可以在哪些地方使用,比如:Repository層?
APT在Repository層的嘗試
了解APT與簡(jiǎn)單學(xué)習(xí)之后,搭建Repository層時(shí),發(fā)現(xiàn)有一些簡(jiǎn)單,重復(fù)模版的代碼
每一次添加新接口都需要簡(jiǎn)單地修改很多地方,能不能把一部分代碼自動(dòng)生成,減少改動(dòng)的次數(shù)呢?
Repository層
IRemoteDataSource, RemoteDataSourceImpl
遠(yuǎn)程數(shù)據(jù)源,屬于網(wǎng)絡(luò)請(qǐng)求相關(guān)
ILocalDataSource, LocalDataSourceImpl
本地?cái)?shù)據(jù)源,屬于本地?cái)?shù)據(jù)持久化相關(guān)
IRepository,RepositoryImpl
倉(cāng)庫(kù)代理類,代理遠(yuǎn)程數(shù)據(jù)源與本地?cái)?shù)據(jù)源
Repository層APT設(shè)計(jì)思路
發(fā)現(xiàn)在具體實(shí)現(xiàn)類中,大多都是以代理類的形式調(diào)用:方法中調(diào)用代理對(duì)象,方法名稱與參數(shù),返回值類型都相同。顯然可以進(jìn)行APT的嘗試
- 簡(jiǎn)單的情況,具體實(shí)現(xiàn)類中只有一個(gè)代理對(duì)象
- 復(fù)雜的情況,有多個(gè)代理對(duì)象,方法內(nèi)并有一些變化
期望結(jié)果:
- 把RemoteDataSourceImpl自動(dòng)化生成
- 把LocalDataSourceImpl自動(dòng)化生成
- 把RepositoryImpl自動(dòng)化生成
自定義注解設(shè)計(jì)
要想具體實(shí)現(xiàn)類自動(dòng)生成,首先要知道需要什么:
- 方便自動(dòng)生成java文件的類庫(kù)
- 自動(dòng)生成類名字是什么
- 需要注入的代理對(duì)象
- 讓代理對(duì)象代理的方法集
自動(dòng)生成java文件的類庫(kù),可以使用 squareup javapoet
自動(dòng)生成類名字,代理對(duì)象,方法集需要通過(guò)自定義注解配置參數(shù)的形成,在AbstractProcessor中獲取
Delegate
- @Retention(RetentionPolicy.SOURCE)
- @Target(ElementType.TYPE)
- public @interface Delegate {
- /**
- * delegate class package
- */
- String delegatePackage();
- /**
- * delegate class name
- */
- String delegateClassName();
- /**
- * delegate simple name
- */
- String delegateSimpleName();
- }
SingleDelegate
- @Retention(RetentionPolicy.SOURCE)
- @Target(ElementType.TYPE)
- public @interface SingleDelegate {
- /**
- * impl class name
- */
- String classNameImpl();
- /**
- * delegate data
- */
- Delegate delegate();
- }
MultiDelegate
- @Retention(RetentionPolicy.SOURCE)
- @Target(ElementType.TYPE)
- public @interface MultiDelegate {
- /**
- * impl class name
- */
- String classNameImpl();
- /**
- * delegate list
- */
- Delegate[] Delegates();
- }
處理自定義的注解、生成代碼
AnnotationProcessor
- @AutoService(Processor.class) // javax.annotation.processing.IProcessor
- @SupportedSourceVersion(SourceVersion.RELEASE_7) //java
- @SupportedAnnotationTypes({ // 標(biāo)注注解處理器支持的注解類型
- "com.annotation.SingleDelegate",
- "com.annotation.MultiDelegate"
- })
- public class AnnotationProcessor extends AbstractProcessor {
- public static final String PACKAGE = "com.poet.delegate";
- public static final String CLASS_DESC = "From poet compiler";
- public Filer filer; //文件相關(guān)的輔助類
- public Elements elements; //元素相關(guān)的輔助類
- public Messager messager; //日志相關(guān)的輔助類
- public Types types;
- @Override
- public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) {
- filer = processingEnv.getFiler();
- elements = processingEnv.getElementUtils();
- messager = processingEnv.getMessager();
- types = processingEnv.getTypeUtils();
- new SingleDelegateProcessor().process(set, roundEnvironment, this);
- new MultiDelegateProcessor().process(set, roundEnvironment, this);
- return true;
- }
- }