日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Android?ButterKnife依賴注入框架使用教程_Android

作者:xuyin1204 ? 更新時間: 2023-06-04 編程語言

簡介

BuffterKnife 采用 注解+ APT技術

APT:Annotation Processor tool 注解處理器,是javac的一個工具,每個處理器都是繼承于AbstractProcessor

注解處理器是運行在自己的java虛擬機中

APT如何生成字節碼文件:

Annotation Processing 不能加入或刪除java方法

APT整個流程

  • 聲明的注解等待生命周期為CLASS
  • 繼承AbstractProcessor類
  • 再調用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函數,即方法的入口,在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();  //用于創建文件
    Elements getElementUtils();  //用來處理Element的工具類,Element是指在注解處理過程中掃描的所有java源文件,可以把這個源文件想象成Element的全部,而源代碼中每個獨立的部分就可以認作為特定類型的Element
    Types getTypeUtils();  //TypeElement代表Element的類型, Types是用于獲取源代碼類型的信息
    SourceVersion getSourceVersion();
    Locale getLocale();
}

ButterKnife的工作原理

  • 編譯的時候掃描注解,并做相應的處理,生成java代碼,生成Java代碼是調用 javapoet 庫生成的
  • 調用ButterKnife.bind(this);方法的時候,將ID與對應的上下文綁定在一起

原文鏈接:https://blog.csdn.net/xuyin1204/article/details/127963265

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新