您好,登錄后才能下訂單哦!
這篇文章主要介紹“Android ButterKnife依賴注入框架如何使用”,在日常操作中,相信很多人在Android ButterKnife依賴注入框架如何使用問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Android ButterKnife依賴注入框架如何使用”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
BuffterKnife 采用 注解+ APT技術(shù)
APT:Annotation Processor tool 注解處理器,是javac的一個(gè)工具,每個(gè)處理器都是繼承于AbstractProcessor
注解處理器是運(yùn)行在自己的java虛擬機(jī)中
APT如何生成字節(jié)碼文件:
Annotation Processing 不能加入或刪除java方法
APT整個(gè)流程
聲明的注解等待生命周期為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相對(duì)于main函數(shù),即方法的入口,在process方法中可以完成掃描、評(píng)估、處理注解等等代碼。process方法最后會(huì)生成所需要的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源文件,可以把這個(gè)源文件想象成Element的全部,而源代碼中每個(gè)獨(dú)立的部分就可以認(rèn)作為特定類型的Element Types getTypeUtils(); //TypeElement代表Element的類型, Types是用于獲取源代碼類型的信息 SourceVersion getSourceVersion(); Locale getLocale(); }
編譯的時(shí)候掃描注解,并做相應(yīng)的處理,生成java代碼,生成Java代碼是調(diào)用 javapoet 庫生成的
調(diào)用ButterKnife.bind(this);方法的時(shí)候,將ID與對(duì)應(yīng)的上下文綁定在一起
到此,關(guān)于“Android ButterKnife依賴注入框架如何使用”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。