參考資料
前言
APT(Annotation Processing Tool
的簡稱)捻勉,可以在代碼編譯期解析注解体箕,并且生成新的 Java 文件,減少手動的代碼輸入〔绷ィ現(xiàn)在有很多主流庫都用上了 APT,比如 Dagger2, ButterKnife, EventBus3 等甜奄,我們要緊跟潮流哼丈,與時俱進吶! (? ??_??)?
普及一下ButterKnife支持的注解
1 .使用最多的Bind.class
@Retention(CLASS) @Target(FIELD)
public @interface Bind {
/** View ID to which the field will be bound. */
int[] value();
}
2.BindBool BindColor BindDimen BindDrawable BindInt BindString靶擦,這些使用比較少。
比如:
@Bindbool(R.bool.is_tablet)
boolean isTable;
Element
源代碼中的每一部分都是一個特定的元素類型雇毫,分別代表了包玄捕、類、方法等等棚放,具體看 Demo枚粘。
package com.example;
public class Foo { // TypeElement
private int a; // VariableElement
private Foo other; // VariableElement
public Foo() {} // ExecuteableElement
public void setA( // ExecutableElement
int newA // TypeElement
)
}
這些 Element
元素,相當(dāng)于 XML 中的 DOM 樹飘蚯,可以通過一個元素去訪問它的父元素或者子元素馍迄。
element.getEnclosingElement();// 獲取父元素
element.getEnclosedElements();// 獲取子元素