Android?ButterKnife依賴注入框架使用教程
簡介
BuffterKnife 采用 注解+ APT技術(shù)
APT:Annotation Processor tool 注解處理器,是javac的一個工具,每個處理器都是繼承于AbstractProcessor
注解處理器是運行在自己的java虛擬機中
APT如何生成字節(jié)碼文件:
Annotation Processing 不能加入或刪除java方法
APT整個流程
- 聲明的注解等待生命周期為CLASS
- 繼承AbstractProcessor類
- 再調(diào)用AbstractProcessor 的process方法
AbstractProcessor.java
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by FernFlower decompiler) // package javax.annotation.processing; import java.util.Collections; import java.util.HashSet; import java.util.Objects; import java.util.Set; import javax.lang.model.SourceVersion; import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.Element; import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.TypeElement; import javax.tools.Diagnostic.Kind; public abstract class AbstractProcessor implements Processor { protected ProcessingEnvironment processingEnv; private boolean initialized = false; protected AbstractProcessor() { } public Set<String> getSupportedOptions() { SupportedOptions so = (SupportedOptions)this.getClass().getAnnotation(SupportedOptions.class); return so == null ? Collections.emptySet() : arrayToSet(so.value(), false); } public Set<String> getSupportedAnnotationTypes() { // 返回所支持注解的類型 SupportedAnnotationTypes sat = (SupportedAnnotationTypes)this.getClass().getAnnotation(SupportedAnnotationTypes.class); boolean initialized = this.isInitialized(); if (sat == null) { if (initialized) { this.processingEnv.getMessager().printMessage(Kind.WARNING, "No SupportedAnnotationTypes annotation found on " + this.getClass().getName() + ", returning an empty set."); } return Collections.emptySet(); } else { boolean stripModulePrefixes = initialized && this.processingEnv.getSourceVersion().compareTo(SourceVersion.RELEASE_8) <= 0; return arrayToSet(sat.value(), stripModulePrefixes); } } public SourceVersion getSupportedSourceVersion() { //用來指定所使用的java版本 SupportedSourceVersion ssv = (SupportedSourceVersion)this.getClass().getAnnotation(SupportedSourceVersion.class); SourceVersion sv = null; if (ssv == null) { sv = SourceVersion.RELEASE_6; if (this.isInitialized()) { Messager var10000 = this.processingEnv.getMessager(); Kind var10001 = Kind.WARNING; String var10002 = this.getClass().getName(); var10000.printMessage(var10001, "No SupportedSourceVersion annotation found on " + var10002 + ", returning " + sv + "."); } } else { sv = ssv.value(); } return sv; } public synchronized void init(ProcessingEnvironment processingEnv) { // 初始化工作 if (this.initialized) { throw new IllegalStateException("Cannot call init more than once."); } else { Objects.requireNonNull(processingEnv, "Tool provided null ProcessingEnvironment"); this.processingEnv = processingEnv; this.initialized = true; } } public abstract boolean process(Set<? extends TypeElement> var1, RoundEnvironment var2); // process相對于main函數(shù),即方法的入口,在process方法中可以完成掃描、評估、處理注解等等代碼。process方法最后會生成所需要的java代碼 public Iterable<? extends Completion> getCompletions(Element element, AnnotationMirror annotation, ExecutableElement member, String userText) { return Collections.emptyList(); } protected synchronized boolean isInitialized() { return this.initialized; } private static Set<String> arrayToSet(String[] array, boolean stripModulePrefixes) { assert array != null; Set<String> set = new HashSet(array.length); String[] var3 = array; int var4 = array.length; for(int var5 = 0; var5 < var4; ++var5) { String s = var3[var5]; if (stripModulePrefixes) { int index = s.indexOf(47); if (index != -1) { s = s.substring(index + 1); } } set.add(s); } return Collections.unmodifiableSet(set); } }
ProcessingEnvironment.java
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by FernFlower decompiler) // package javax.annotation.processing; import java.util.Locale; import java.util.Map; import javax.lang.model.SourceVersion; import javax.lang.model.util.Elements; import javax.lang.model.util.Types; public interface ProcessingEnvironment { Map<String, String> getOptions(); Messager getMessager(); Filer getFiler(); //用于創(chuàng)建文件 Elements getElementUtils(); //用來處理Element的工具類,Element是指在注解處理過程中掃描的所有java源文件,可以把這個源文件想象成Element的全部,而源代碼中每個獨立的部分就可以認(rèn)作為特定類型的Element Types getTypeUtils(); //TypeElement代表Element的類型, Types是用于獲取源代碼類型的信息 SourceVersion getSourceVersion(); Locale getLocale(); }
ButterKnife的工作原理
- 編譯的時候掃描注解,并做相應(yīng)的處理,生成java代碼,生成Java代碼是調(diào)用 javapoet 庫生成的
- 調(diào)用ButterKnife.bind(this);方法的時候,將ID與對應(yīng)的上下文綁定在一起
到此這篇關(guān)于Android ButterKnife依賴注入框架使用教程的文章就介紹到這了,更多相關(guān)Android ButterKnife依賴注入內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android用注解與反射實現(xiàn)Butterknife功能
- Android中butterknife的使用與自動化查找組件插件詳解
- 詳解Android Studio安裝ButterKnife插件(手動安裝)
- Android Studio使用ButterKnife和Zelezny的方法
- Android Kotlin環(huán)境使用ButterKnife的方法
- Android Studio中ButterKnife插件的安裝與使用詳解
- 解決Android Studio 3.0 butterknife:7.0.1配置的問題
- Android注解使用之ButterKnife 8.0詳解
- Android注解ButterKnife的基本使用
相關(guān)文章
詳解Android系統(tǒng)中跨應(yīng)用數(shù)據(jù)分享功能的實現(xiàn)
這篇文章主要介紹了Android系統(tǒng)中跨應(yīng)用數(shù)據(jù)分享功能的實現(xiàn),文中分為發(fā)送文字、二進(jìn)制內(nèi)容和圖片三種情況來講,需要的朋友可以參考下2016-04-04android ListActivity顯示圖標(biāo)實例
在ListActivity中顯示圖標(biāo),好像并不復(fù)雜,實現(xiàn)起來卻不輕松,我們下面一步步來實現(xiàn)ListActivity中顯示圖標(biāo)2013-11-11Android Studio kotlin生成編輯類注釋代碼
這篇文章主要介紹了Android Studio kotlin生成編輯類注釋代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03Android本地存儲SharedPreferences詳解
這篇文章主要介紹了Android本地存儲SharedPreferences詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05Android Studio如何查看源碼并調(diào)試的方法步驟
這篇文章主要介紹了Android Studio如何查看源碼并調(diào)試的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05Android 破解視頻App去除廣告功能詳解及解決辦法總結(jié)
這篇文章主要介紹了Android 破解視頻App去除廣告功能詳解及解決辦法總結(jié)的相關(guān)資料,這里對視頻播放原理及破解去除廣告幾種方法進(jìn)行了總結(jié),需要的朋友可以參考下2016-12-12