AbstractProcessor 注解處理器相關(guān)

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)倒入

Javapoet參考

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.*;
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容