從無到有手寫B(tài)utterKnife框架

導(dǎo)航

一、代碼的演進(jìn)
二、butterKnife反射調(diào)用
三、javapoet自動(dòng)生成模板代碼
四、apt與注解
五、注解支持多層繼承
六、apt調(diào)試
七、javapoet語法

1、注解支持多層繼承,并且支持field相同去重

繼承是樹形結(jié)構(gòu)

例如javapoetdemo中的示例
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)鍵在思路,不多于贅述

從無到有手寫butterKnife框架

https://github.com/yinlingchaoliu/JavaPoetDemo

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

相關(guān)閱讀更多精彩內(nèi)容

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