ButterKnife依赖注入框架源码解析
创始人
2024-02-06 13:01:47
0

BuffterKnife 采用 注解+ APT技术
APT:Annotation Processor tool 注解处理器,是javac的一个工具,每个处理器都是继承于AbstractProcessor

注解处理器是运行在自己的java虚拟机中

APT如何生成字节码文件:
在这里插入图片描述

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

APT整个流程

    1. 声明的注解等待生命周期为CLASS
    1. 继承AbstractProcessor类
    1. 再调用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 getSupportedOptions() {SupportedOptions so = (SupportedOptions)this.getClass().getAnnotation(SupportedOptions.class);return so == null ? Collections.emptySet() : arrayToSet(so.value(), false);}public Set 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 var1, RoundEnvironment var2);  // process相对于main函数,即方法的入口,在process方法中可以完成扫描、评估、处理注解等等代码。process方法最后会生成所需要的java代码public Iterable getCompletions(Element element, AnnotationMirror annotation, ExecutableElement member, String userText) {return Collections.emptyList();}protected synchronized boolean isInitialized() {return this.initialized;}private static Set arrayToSet(String[] array, boolean stripModulePrefixes) {assert array != null;Set 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 getOptions();Messager getMessager();Filer getFiler();  //用于创建文件Elements getElementUtils();  //用来处理Element的工具类,Element是指在注解处理过程中扫描的所有java源文件,可以把这个源文件想象成Element的全部,而源代码中每个独立的部分就可以认作为特定类型的ElementTypes getTypeUtils();  //TypeElement代表Element的类型, Types是用于获取源代码类型的信息SourceVersion getSourceVersion();Locale getLocale();
}

ButterKnife的工作原理

  1. 编译的时候扫描注解,并做相应的处理,生成java代码,生成Java代码是调用 javapoet 库生成的
  2. 调用ButterKnife.bind(this);方法的时候,将ID与对应的上下文绑定在一起

相关内容

热门资讯

伊朗外长:建议美国尊重伊朗人民... 格隆汇12月15日|伊朗外交部长阿拉格齐说,伊朗从不寻求战争,而是希望通过外交途径解决问题。伊朗现在...
原创 官... 中国主导成立国际调解院,是一件对世界和平事业有着极大推动力的大好事,该国际组织一经成立,就有不少国家...
政策利好!保险股大涨! 中国平安刷新近4年新高。 A股市场今天(12月15日)上午整体窄幅震荡,保险股大涨,成为领涨市场最主...
西甲-姆巴佩破门罗德里戈建功 ... 2025年12月15日凌晨4点,2025-2026赛季西甲联赛第16轮展开一场焦点战役,皇马前往客场...
原创 俄... 在乌克兰东部的红军城,战斗依旧如火如荼。随着俄乌冲突的深入,红军城成为了一个微缩的战场,象征着更为复...
“线上平台+线下空间”一站式护... 深圳新闻网2025年12月13日讯(深圳特区报记者 戚金城)近日,深圳市委政法委联合市前海管理局搭建...
36岁男子发现女友虚报年龄,称... 据@小莉帮忙,12月13日,河南荥阳的马先生表示,自己今年36岁了,和一位43岁的姐姐恋爱,去领证的...
主动投案,国家税务总局浙江省税... 据中央纪委国家监委驻国家税务总局纪检监察组、浙江省纪委监委消息:国家税务总局浙江省税务局原党委书记、...
澳大利亚海滩枪击事件两名枪手身... 澳大利亚邦迪海滩当地时间14日发生枪击事件,目前已造成16人死亡。15日早间,新南威尔士州警察局长在...
伊朗外长:“建议美国尊重伊朗人... 新华社德黑兰12月14日电(记者沙达提)据伊朗伊斯兰共和国通讯社14日报道,伊朗外交部长阿拉格齐说,...