1. google提供了一個注冊處理器的庫:用于生產(chǎn)meta-inf信息
compile 'com.google.auto.service:auto-service:1.0-rc2'
一個注解搞定:
AutoService(Processor.class)
public class MyProcessor extends AbstractProcessor {
...
}
2. 項目結(jié)構(gòu):
----bindview-annotation(Java Library)
----bindview-api(Android Library)
----bindview-compiler(Java Library)
----app(Android App)
bindview-annotation 注解聲明
bindview-api 調(diào)用Android SDK API
bindview-compiler 注解處理器相關(guān)
app 測試App
3. Element概念
package com.example; // 包PackageElement
public class MyClass { // 類TypeElement
private int a; // 變量VariableElement
private Foo other; // 變量VariableElement
public Foo () {} // 方法ExecuteableElement
public void setA ( // ExecuteableElement
int newA // TypeElement
) {
}
}
4.getname方法的區(qū)別:
總結(jié):getName()和getCanonicalName()只有數(shù)組和內(nèi)部類的表現(xiàn)不一樣。
詳細參考
以下順序為:
getName()
getCanonicalName()
getSimpleName()
******************************** 普通類 ****************************************
com.ershuai.stu.other.TestClass
com.ershuai.stu.other.TestClass
TestClass
******************************** 普通類 List ****************************************
java.util.ArrayList
java.util.ArrayList
ArrayList
******************************* Integer.class *****************************************
java.lang.Integer
java.lang.Integer
Integer
******************************** int .class ****************************************
int
int
int
******************************** 普通類 array - 有區(qū)別 ****************************************
[Lcom.ershuai.stu.other.TestClass;
com.ershuai.stu.other.TestClass[]
TestClass[]
******************************** 內(nèi)部類 - 有區(qū)別 ****************************************
com.ershuai.stu.other.Test0$NTestClass
com.ershuai.stu.other.Test0.NTestClass
NTestClass
Javapoet生成代碼
占位符的使用
$L 啥也能占
$S for Strings
$T for Types
$N for Names(我們自己生成的方法名或者變量名等等)
這里的$T,在生成的源代碼里面,也會自動導(dǎo)入你的類。
靜態(tài)倒入
JavaFile.builder("com.example.helloworld", hello)
.addStaticImport(hoverboard, "createNimbus")
.addStaticImport(namedBoards, "*")
.addStaticImport(Collections.class, "*")
.build();
那么我們生成的類就會出現(xiàn)這些代碼:
import static com.mattel.Hoverboard.Boards.*;
import static com.mattel.Hoverboard.createNimbus;
import static java.util.Collections.*;