網站首頁 編程語言 正文
簡介
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
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-11-04 深入了解Python中Lambda函數的用法_python
- 2022-08-02 Python面試之os.system()和os.popen()的區別詳析_python
- 2022-11-10 golang?常用定時任務匯總_Golang
- 2022-07-30 jQuery?UI工具提示框部件Tooltip?Widget_jquery
- 2022-09-20 Python處理時間戳和時間計算等的腳本分享_python
- 2022-04-09 給原生html中添加水印遮罩層
- 2022-05-11 C++程序代碼優化的方法實例大全_C 語言
- 2022-09-29 Python3中map(),reduce(),filter()的詳細用法_python
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支