參考文章
https://joyrun.github.io/2016/07/18/java-annotation/
https://joyrun.github.io/2016/07/19/AptHelloWorld/
正文
-
注解:
[email protected] : 規定Annotation所修飾的對象範圍
ElementType.TYPE:類、接口
ElementType.CONSTRUCTOR:構造器聲明
ElementType.FIELD:成員變量、對象、屬性(包括enum執行個體)
ElementType.LOCAL_VARIABLE:局部變量聲明
ElementType.METHOD:方法聲明
ElementType.PACKAGE:包聲明
ElementType.PARAMETER:參數聲明
2.Retention:對Annotation的“生命周期”限制
RetentionPolicy.SOURCE:在源檔案中有效
RetentionPolicy.CLASS:在class檔案中有效
RetentionPolicy.RUNTIME:在運作時有效 ,ps:可以通過反射,擷取到該注解的屬性值,進而去做一些運作時的邏輯處理
3.Documented: 一個标記注解,沒有成員
4.Inherited: 闡述了某個被标注的類型是被繼承的
-
demo練習
https://joyrun.github.io/2016/07/19/AptHelloWorld/
涉及到的知識點:
- AutoService:也是一個注解,官方翻譯是:An annotation for service providers as described in {@link java.util.ServiceLoader}。主要的作用是注解 processor 類,并對其生成 META-INF 的配置資訊。
-
JavaPoet:這個比較熟悉了,這個庫的主要作用就是幫助我們通過類調用的形式來生成代碼。https://github.com/square/javapoet
ps:
1.使用javaPeot有個技巧,其實就是把要輸出的代碼,先列出來,在按照javaPoet文法進行代碼編寫即可。javaPoet的鍊式調用非常友善閱讀。
2.按照,引用,類名,參數,方法名。倒着寫,思路更清晰
ok,學着寫,使用javapoet成功編譯出代碼。休息了,改天繼續研究
https://github.com/cxMax/AndroidAPT