自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

安卓AOP三劍客之Android APT技術(shù)淺談

移動(dòng)開(kāi)發(fā) Android
APT(Annotation Processing Tool 的簡(jiǎn)稱),可以在代碼編譯期解析注解,并且生成新的 Java 文件,減少手動(dòng)的代碼輸入?,F(xiàn)在有很多主流庫(kù)都用上了 APT,比如 Dagger2, ButterKnife, EventBus3 等。

通過(guò)學(xué)習(xí)與使用square公司的開(kāi)源項(xiàng)目javapoet,來(lái)實(shí)現(xiàn)倉(cāng)庫(kù)層動(dòng)態(tài)生成代碼

Android APT技術(shù)淺談

安卓AOP三劍客: APT, AspectJ, Javassist

Android APT技術(shù)淺談

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

 

  1. @AutoService(Processor.class) // javax.annotation.processing.IProcessor 
  2. @SupportedSourceVersion(SourceVersion.RELEASE_7) //java 
  3. @SupportedAnnotationTypes({ // 標(biāo)注注解處理器支持的注解類型 
  4.     "com.annotation.SingleDelegate"
  5.     "com.annotation.MultiDelegate" 
  6. }) 
  7. public class AnnotationProcessor extends AbstractProcessor { 
  8.  
  9. public static final String PACKAGE = "com.poet.delegate"
  10. public static final String CLASS_DESC = "From poet compiler"
  11.  
  12. public Filer filer; //文件相關(guān)的輔助類 
  13. public Elements elements; //元素相關(guān)的輔助類 
  14. public Messager messager; //日志相關(guān)的輔助類 
  15. public Types types; 
  16.  
  17. @Override 
  18. public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) { 
  19.     filer = processingEnv.getFiler(); 
  20.     elements = processingEnv.getElementUtils(); 
  21.     messager = processingEnv.getMessager(); 
  22.     types = processingEnv.getTypeUtils(); 
  23.  
  24.     new SingleDelegateProcessor().process(set, roundEnvironment, this); 
  25.     new MultiDelegateProcessor().process(set, roundEnvironment, this); 
  26.  
  27.     return true

2,在繼承AbstractProcessor類中的process方法,處理我們自定義的注解,生成代碼:

 

  1. public class SingleDelegateProcessor implements IProcessor {  
  2. @Override 
  3. public void process(Set<? extends TypeElement> set, RoundEnvironment roundEnv, 
  4.                 AnnotationProcessor abstractProcessor) { 
  5. // 查詢注解是否存在 
  6. Set<? extends Element> elementSet = 
  7.         roundEnv.getElementsAnnotatedWith(SingleDelegate.class); 
  8. Set<TypeElement> typeElementSet = ElementFilter.typesIn(elementSet); 
  9. if (typeElementSet == null || typeElementSet.isEmpty()) { 
  10.     return
  11.  
  12. // 循環(huán)處理注解 
  13. for (TypeElement typeElement : typeElementSet) { 
  14.     if (!(typeElement.getKind() == ElementKind.INTERFACE)) { // 只處理接口類型 
  15.         continue
  16.     } 
  17.  
  18.     // 處理 SingleDelegate,只處理 annotation.classNameImpl() 不為空的注解 
  19.     SingleDelegate annotation = typeElement.getAnnotation(SingleDelegate.class); 
  20.     if ("".equals(annotation.classNameImpl())) { 
  21.         continue
  22.     } 
  23.     Delegate delegate = annotation.delegate(); 
  24.  
  25.     // 添加構(gòu)造器 
  26.     MethodSpec.Builder constructorBuilder = MethodSpec.constructorBuilder() 
  27.             .addModifiers(Modifier.PUBLIC); 
  28.  
  29.     // 創(chuàng)建類名相關(guān) class builder 
  30.     TypeSpec.Builder builder = 
  31.             ProcessUtils.createTypeSpecBuilder(typeElement, annotation.classNameImpl()); 
  32.  
  33.     // 處理 delegate 
  34.     builder = ProcessUtils.processDelegate(typeElement, builder, 
  35.             constructorBuilder, delegate); 
  36.  
  37.     // 檢查是否繼承其它接口 
  38.     builder = processSuperSingleDelegate(abstractProcessor, builder, constructorBuilder, typeElement); 
  39.  
  40.     // 完成構(gòu)造器 
  41.     builder.addMethod(constructorBuilder.build()); 
  42.  
  43.     // 創(chuàng)建 JavaFile 
  44.     JavaFile javaFile = JavaFile.builder(AnnotationProcessor.PACKAGE, builder.build()).build(); 
  45.     try { 
  46.         javaFile.writeTo(abstractProcessor.filer); 
  47.     } catch (IOException e) { 
  48.         e.printStackTrace(); 
  49.     } 

3,在項(xiàng)目Gradle中添加 annotationProcessor project 引用

 

  1. compile project(':apt-delegate-annotation' 
  2. annotationProcessor project(':apt-delegate-compiler'

4,如果有自定義注解的話,創(chuàng)建一個(gè)java的Module,專門放入自定義注解。項(xiàng)目與apt Module都需引用自定義注解Module

4-1,主工程:

 

  1. compile project(':apt-delegate-annotation' 
  2. annotationProcessor project(':apt-delegate-compiler'

4-2,apt Module:

 

  1. compile project(':apt-delegate-annotation' 
  2. compile 'com.google.auto.service:auto-service:1.0-rc2' 
  3. compile 'com.squareup:javapoet:1.4.0' 

5,生成的源代碼在build/generated/source/apt下可以看到

Android APT技術(shù)淺談

難點(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層

Android APT技術(shù)淺談

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

 

  1. @Retention(RetentionPolicy.SOURCE) 
  2. @Target(ElementType.TYPE) 
  3. public @interface Delegate { 
  4.  
  5. /** 
  6.  * delegate class package 
  7.  */ 
  8. String delegatePackage(); 
  9.  
  10. /** 
  11.  * delegate class name 
  12.  */ 
  13. String delegateClassName(); 
  14.  
  15. /** 
  16.  * delegate simple name 
  17.  */ 
  18. String delegateSimpleName(); 

SingleDelegate

 

  1. @Retention(RetentionPolicy.SOURCE) 
  2. @Target(ElementType.TYPE) 
  3. public @interface SingleDelegate { 
  4.  
  5. /** 
  6.  * impl class name 
  7.  */ 
  8. String classNameImpl(); 
  9.  
  10. /** 
  11.  * delegate data 
  12.  */ 
  13. Delegate delegate(); 

MultiDelegate

 

  1. @Retention(RetentionPolicy.SOURCE) 
  2. @Target(ElementType.TYPE) 
  3. public @interface MultiDelegate {  
  4. /** 
  5.  * impl class name 
  6.  */ 
  7. String classNameImpl();  
  8. /** 
  9.  * delegate list 
  10.  */ 
  11. Delegate[] Delegates(); 

處理自定義的注解、生成代碼

AnnotationProcessor

 

  1. @AutoService(Processor.class) // javax.annotation.processing.IProcessor 
  2. @SupportedSourceVersion(SourceVersion.RELEASE_7) //java 
  3. @SupportedAnnotationTypes({ // 標(biāo)注注解處理器支持的注解類型 
  4.     "com.annotation.SingleDelegate"
  5.     "com.annotation.MultiDelegate" 
  6. }) 
  7. public class AnnotationProcessor extends AbstractProcessor {  
  8. public static final String PACKAGE = "com.poet.delegate"
  9. public static final String CLASS_DESC = "From poet compiler" 
  10. public Filer filer; //文件相關(guān)的輔助類 
  11. public Elements elements; //元素相關(guān)的輔助類 
  12. public Messager messager; //日志相關(guān)的輔助類 
  13. public Types types;  
  14. @Override 
  15. public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) { 
  16.     filer = processingEnv.getFiler(); 
  17.     elements = processingEnv.getElementUtils(); 
  18.     messager = processingEnv.getMessager(); 
  19.     types = processingEnv.getTypeUtils(); 
  20.  
  21.     new SingleDelegateProcessor().process(set, roundEnvironment, this); 
  22.     new MultiDelegateProcessor().process(set, roundEnvironment, this); 
  23.  
  24.     return true
責(zé)任編輯:未麗燕 來(lái)源: 喜歡自己才會(huì)擁抱生活
相關(guān)推薦

2019-06-27 10:06:54

Linux 性能工具

2011-03-28 16:04:44

nagios

2021-05-13 10:25:29

Linuxgrep命令

2017-07-25 08:53:14

CorrectLinkCCA-SD算法

2023-10-04 00:20:31

grepLinux

2018-05-04 15:18:01

DockerDocker Comp容器

2021-03-15 07:39:48

LinuxAwk 語(yǔ)言

2011-04-11 11:01:03

AndroidHTC蘋果

2011-07-04 09:07:54

2010-02-04 16:22:21

2019-08-20 14:29:45

grepsedawk

2024-06-04 00:20:00

Python函數(shù)

2009-02-26 18:22:49

桌面虛擬化Linux

2009-03-19 20:52:58

LinuxPHPCMS

2011-08-06 23:58:34

愛(ài)普生投影機(jī)

2023-11-25 17:08:47

ChatbotLLAMALangChain

2025-04-22 09:39:46

Python爬蟲(chóng)網(wǎng)頁(yè)數(shù)據(jù)抓取

2014-11-26 10:18:32

Cloud Setupwindows在線打包工具

2022-03-03 15:15:27

大數(shù)據(jù)數(shù)據(jù)湖技術(shù)

2009-02-12 09:12:27

JPAEJBJSF
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)