導(dǎo)航
一、代碼的演進(jìn)
二、butterKnife反射調(diào)用
三、javapoet自動(dòng)生成模板代碼
四、apt與注解
五、注解支持多層繼承
六、apt調(diào)試
七、javapoet語法
1、注解支持多層繼承,并且支持field相同去重
繼承是樹形結(jié)構(gòu)

public class BaseActivity extends AppCompatActivity {
@ViewId(R.id.helloTv)
TextView helloTv;
}
public class MainActivity extends BaseActivity {
@ViewId( R.id.register )
Button register;
@ViewId( R.id.login )
Button login;
}
public class TestActivity extends MainActivity {
@ViewId( R.id.guess )
Button guess;
@ViewId( R.id.login )
Button helloTv;
}
上面的代碼是常見情況
1、支持注解是繼承關(guān)系
2、支持子類父類field相同,采用子類注解
public class ElementTree {
public ElementTree parentNode = null;
public List<ElementTree> childNodeList = new ArrayList<>( );
public TypeElement typeElement;
public List<Element> elementList;
public Set<String> fieldNameSets;
public String typeName;
public boolean isSorted;
}
1、思路我們可以獲得Map<TypeElement,List<Element>> 當(dāng)前類,當(dāng)前類元素列表轉(zhuǎn)換成List< ElementTree >
2、創(chuàng)建根節(jié)點(diǎn) root
3、按照中序的方式(中左右)創(chuàng)建樹形關(guān)系
4、遍歷list< ElementTree > 每個(gè)元素,每個(gè)元素判斷是否有父節(jié)點(diǎn),按照子父排序方式獲得所有元素列表List<List<Element>> ,Set<String>
5、用Set<String> 去重
詳見算法
https://github.com/yinlingchaoliu/JavaPoetDemo/blob/master/poet-compiler/src/main/java/com/chaoliu/sort/ElementTree.java
https://github.com/yinlingchaoliu/JavaPoetDemo/blob/master/poet-compiler/src/main/java/com/chaoliu/sort/TreeUtils.java
關(guān)鍵在思路,不多于贅述