編譯注解----實(shí)例

這個(gè) demo 是這篇博客里的一個(gè)例子,光看沒看懂,又照著寫了一遍也還是很懵,為了更好的理解,幾乎把每一句核心代碼都打印,都注釋了,這樣才略懂了一些在編譯時(shí)生成代碼這個(gè)流程。

下面這個(gè)方法是最懵的,所以注釋寫了很多,里面的各種元素都比較抽象,看的時(shí)候,甚至是第一次寫的時(shí)候,都不知道在寫什么,通過打印把抽象的東西,還原成我們熟悉的東西就好理解多了。

/**
 *
 * @param set  包含的是所有使用的[注解的信息],例如BindView,ContentView
 * @param roundEnvironment 返回的是所有被注解的[元素],例如類,屬性等
 * @return
 */
 @Override
 public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) {
//        mMessager.printMessage(Diagnostic.Kind.NOTE,"process...");
?
 // 防止處理多次,要清空
 mProxyMap.clear();
?
 System.out.println("mProxyMap"+mProxyMap.toString());
 // 獲取全部的帶有 bindview 注解的 element
 Set<? extends Element> elseWithBind = roundEnvironment.getElementsAnnotatedWith(BindView.class);
 System.out.println("elseWithBind: " + elseWithBind.toString());
 //elseWithBind: [mTextView, mImageView]
?
 // 對(duì)bindview 進(jìn)行循環(huán),構(gòu)建 proxyInfo 信息
 for (Element element : elseWithBind) {
 // 檢查 element 的合法性
 checkSAnnotationValid(element,BindView.class);
?
 // 強(qiáng)轉(zhuǎn)成屬性元素
 VariableElement variableElement = (VariableElement) element;
 System.out.println("variableElement: "+variableElement);
//            variableElement: mTextView
?
 System.out.println("-----"+ element.getEnclosingElement());
?
 // 要獲取類元素的類名,直接用 element 也可以,強(qiáng)轉(zhuǎn)不是必須的。
 // 屬性元素的外層一定是類元素
 TypeElement typeElement = (TypeElement) variableElement.getEnclosingElement();
 System.out.println("typeElement: "+typeElement);
 // typeElement: com.limiao.annotationdemo.MainActivity
?
 // 獲取類元素的類名(全路徑名)
 String fqClassName = typeElement.getQualifiedName().toString();
 System.out.println("fqClassName: "+fqClassName);
//            fqClassName: com.limiao.annotationdemo.MainActivity
?
 System.out.println("mProxyMap: "+mProxyMap);
?
 ProxyInfo proxyInfo = mProxyMap.get(fqClassName);
 if (proxyInfo == null){
 proxyInfo = new ProxyInfo(mElementUtils,typeElement);
 // 以 class 名稱為 key,保存到 mProxy 中
 mProxyMap.put(fqClassName,proxyInfo);
 }
?
 System.out.println("proxyInfo:"+proxyInfo);
?
 // 獲取 bindview 注解,把信息放入 proxyInfo 中
 BindView bindAnnotation = element.getAnnotation(BindView.class);
 int id = bindAnnotation.value();
 mMessager.printMessage(Diagnostic.Kind.NOTE,"proxyInfo:" + proxyInfo.toString());
 mMessager.printMessage(Diagnostic.Kind.NOTE,"variableElement:" + variableElement);
 mMessager.printMessage(Diagnostic.Kind.NOTE,"id:" + id);
 // 上面的強(qiáng)轉(zhuǎn)要用到這里,作為參數(shù)
 proxyInfo.injectVarialbles.put(id,variableElement);
 }
?
 // 獲取所有的 ContentView 注解,操作原理和上面的 bindview 一樣
 Set<? extends Element> contentAnnotations = roundEnvironment.getElementsAnnotatedWith(ContentView.class);
 for (Element element : contentAnnotations) {
 TypeElement typeElement = (TypeElement) element;
 String fqClassName  = typeElement.getQualifiedName().toString();
 ProxyInfo proxyInfo = mProxyMap.get(fqClassName);
 if (proxyInfo == null) {
 proxyInfo = new ProxyInfo(mElementUtils,typeElement);
 mProxyMap.put(fqClassName,proxyInfo);
 }
 ContentView contentViewAnnotation = element.getAnnotation(ContentView.class);
 proxyInfo.contentViewId = contentViewAnnotation.value();
 }
?
 // 循環(huán)生成源文件
 for (String key : mProxyMap.keySet()) {
 ProxyInfo proxyInfo = mProxyMap.get(key);
 try {
 System.out.println("ProxyClassFullName: "+proxyInfo.getProxyClassFullName());
 System.out.println("TypeElement: "+proxyInfo.getTypeElement());
 // 創(chuàng)建一個(gè) javaFile 文件
 // 第一個(gè)參數(shù):創(chuàng)建的文件名,包含全路徑
 // 第二個(gè)參數(shù):與此文件的創(chuàng)建有因果關(guān)聯(lián)的類型或包或模塊元素,可以為null(文檔的說明)
//                JavaFileObject jfo = processingEnv.getFiler().createSourceFile(proxyInfo.getProxyClassFullName(),proxyInfo.getTypeElement());
//                試了一下,如果第二個(gè)參數(shù)為 null ,也沒有關(guān)系,還能正常編譯運(yùn)行
 JavaFileObject jfo = processingEnv.getFiler().createSourceFile(proxyInfo.getProxyClassFullName(),null);
 Writer writer = jfo.openWriter();
 writer.write(proxyInfo.generateJavaCode());
 writer.flush();
 writer.close();
 } catch (IOException e) {
?
 e.printStackTrace();
 error(proxyInfo.getTypeElement(),"unable to write injector for type %s: %s",proxyInfo.getTypeElement(),e.getMessage());
 }
 }
?
 return true;
?
 }

參考的這篇博客的作者關(guān)于編譯注解寫了一個(gè)系列的文章,都很好,這里整理一下貼出來,方便查閱。

深入理解編譯注解(一)從實(shí)戰(zhàn)理解什么是編譯注解

深入理解編譯注解(二)annotationProcessor與android-apt

深入理解編譯注解(三)依賴關(guān)系 apt/annotationProcessor與Provided的區(qū)別

深入理解編譯注解(四)常用接口介紹

深入理解編譯注解(五)RetentionPolicy.SOURCE 和 RetentionPolicy.CLASS區(qū)別討論

深入理解編譯注解(六)Butterknife的實(shí)現(xiàn)原理

demo github 地址

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

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

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