当前位置: 首页 > news >正文

Java编译期注解处理器AbstractProcessor使用

我们接触的注解主要分为以下两类

  1. 运行时注解:通过反射在运行时动态处理注解的逻辑
  2. 编译时注解:通过注解处理器在编译期动态处理相关逻辑

编译期注解我们常用的有Lombok,在class文件中自动生成get和set方法

解编译期处理流程最关键的一个类就是javax.annotation.processing.Processor注解处理器的接口类,遵循SPI规约进行拓展,我们所有需要对编译期处理注解的逻辑都需要实现这个Processor接口,当然,AbstractProcessor 抽象类帮我们写好了大部分都流程,所以我们只需要实现这个抽象类就可以很方便的定义一个注解处理器

自定义注解处理器

(1)tools.jar加入到idea的类路径

注意:在idea自定义编译处理器前,需要把tools.jar加入到idea的类路径中(文件--》项目结构--》平台设置--》SDK-->类路径--》把tools.jar加入进去),如下图:

(2)建maven父子项目

1)父项目demo的pom.xml配置如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.3</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.wbq</groupId><artifactId>demo</artifactId><version>0.0.1</version><packaging>pom</packaging><name>demo</name><description>demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><modules><module>child1</module><module>child2</module><module>child3</module></modules><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId><exclusions><!-- 去掉默认配置 --><exclusion><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-logging</artifactId></exclusion></exclusions></dependency><!--  log4j2依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-log4j2</artifactId><version>2.7.3</version></dependency><!-- hutool --><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.6</version></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding></configuration></plugin></plugins></build></project>
2)子项目child3的pom.xml如下
  • pom.xml
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://maven.apache.org/POM/4.0.0"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>demo</artifactId><groupId>com.wbq</groupId><version>0.0.1</version></parent><modelVersion>4.0.0</modelVersion><artifactId>child3</artifactId><dependencies><!-- https://mvnrepository.com/artifact/javax.annotation/javax.annotation-api --><dependency><groupId>javax.annotation</groupId><artifactId>javax.annotation-api</artifactId><version>1.3.2</version></dependency></dependencies><build><!-- 指定JDK编译版本 --><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding><compilerArgs><arg>-proc:none</arg></compilerArgs><compilerArguments><bootclasspath>${java.home}/lib/rt.jar;${java.home}/lib/jce.jar;${java.home}/../lib/tools.jar</bootclasspath></compilerArguments></configuration></plugin></plugins></build></project>
  • 注解类
package com.wbq.child3.processor;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;//源码级注解,编译结束就丢弃
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyFieldAnnotation {}
  • AbstractProcessor实现类
    在有MyFieldAnnotation注解的类的方法中添加代码:System.out.println("hello world");
package com.wbq.child3.processor;import com.sun.tools.javac.api.JavacTrees;
import com.sun.tools.javac.processing.JavacProcessingEnvironment;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.TreeMaker;
import com.sun.tools.javac.tree.TreeTranslator;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.Names;import javax.annotation.processing.*;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;
import java.util.Set;/*** 在有MyFieldAnnotation注解的类的方法中添加代码:System.out.println("hello world");*/
@SupportedAnnotationTypes("com.wbq.child3.processor.MyFieldAnnotation")
@SupportedSourceVersion(SourceVersion.RELEASE_8)
//@AutoService(Processor.class)
public class MyFieldProcessor extends AbstractProcessor {/*** 抽象语法树*/private JavacTrees javacTrees;/*** AST*/private TreeMaker treeMaker;/*** 标识符*/private Names names;/*** 日志处理*/private Messager messager;private Filer filer;public synchronized void init(ProcessingEnvironment processingEnv) {System.out.println("初始化");processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, "初始化 ");javacTrees = JavacTrees.instance(processingEnv);Context context = ((JavacProcessingEnvironment)processingEnv).getContext();this.treeMaker = TreeMaker.instance(context);super.init(processingEnv);this.names = Names.instance(context);}@Overridepublic boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {System.out.println("注解处理器");annotations.stream().flatMap(t -> roundEnv.getElementsAnnotatedWith(t).stream()).forEach(t -> {JCTree tree = javacTrees.getTree(t);tree.accept(new TreeTranslator() {@Overridepublic void visitMethodDef(JCTree.JCMethodDecl tree) {System.out.println("hello world");JCTree.JCStatement sysout = treeMaker.Exec(treeMaker.Apply(List.nil(),select("System.out.println"),List.of(treeMaker.Literal("hello world"))));//覆盖原有的语句块tree.body.stats = tree.body.stats.append(sysout);super.visitMethodDef(tree);}});});return true;}private JCTree.JCFieldAccess select(JCTree.JCExpression selected,String expressive){return treeMaker.Select(selected, names.fromString(expressive));}private JCTree.JCFieldAccess select(String expressive){String[] exps=expressive.split("\\.");JCTree.JCFieldAccess access=treeMaker.Select(ident(exps[0]),names.fromString(exps[1]));int index=2;while(index<exps.length){access=treeMaker.Select(access, names.fromString(exps[index++]));}return access;}private JCTree.JCIdent ident(String name){return treeMaker.Ident(names.fromString(name));}}
  • 指定插入式注解类

在resources下新建META-INF文件夹,META-INF文件夹下新建services文件夹,services文件夹下新建文件(文件名=javax.annotation.processing.Processor)

src/main/resources/META-INF/services/javax.annotation.processing.Processor

javax.annotation.processing.Processor文件中写入

com.wbq.child3.processor.MyFieldProcessor

3)新建子项目child2

child2使用child3中的注解

结构如下:

  • pom.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>demo</artifactId><groupId>com.wbq</groupId><version>0.0.1</version></parent><modelVersion>4.0.0</modelVersion><artifactId>child2</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>com.wbq</groupId><artifactId>child3</artifactId><version>0.0.1</version></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding></configuration></plugin></plugins></build></project>
  • 测试类如下:
package com.wbq.child2;import com.wbq.child3.processor.MyFieldAnnotation;
//child3中的注解
@MyFieldAnnotation
public class TestChild3Main {public static void main(String[] args) {System.out.println("main");}
}

(3)测试运行

  • 在maven中先编译child3,依次:clean、compile、package、install

在maven中编译child2,依次:clean、compile

在child2\target\classes\com\wbq\child2中生成了TestChild3Main.class文件,使用idea打开这个class文件,Main方法中添加了一行代码System.out.println("hello world");

http://www.lryc.cn/news/339640.html

相关文章:

  • JetBrains相关的IDE有哪些?
  • Git-常规用法-含解决分支版本冲突解决方法
  • 基于springboot实现大型商场应急预案管理系统项目【项目源码+论文说明】
  • 系统学c#:1、基础准备(软件下载与安装)
  • 解决CSS中鼠标移入到某个元素其子元素被遮挡的问题
  • 【华为OD机试】虚拟理财游戏【C卷|100分】
  • ssh 使用
  • Springboot+Vue项目-基于Java+MySQL的母婴商城系统(附源码+演示视频+LW)
  • Android多线程:Handler runOnUiThread 异步消息处理机制
  • AndroidStudio 导出aar包,并使用
  • python与设计模式之工厂模式的那些事儿
  • 什么是区块链?
  • 2022年电赛F题23年电赛D题-信号调制度测量装置说明中提到带通采样定律。
  • Rust面试宝典第2题:逆序输出整数
  • Linux笔记之查看docker容器目录映射
  • ​​​​网络编程探索系列之——广播原理剖析
  • jar包解压和重新打包
  • Python基于Django的微博热搜、微博舆论可视化系统
  • Flink SQL:debezium-json 格式的表一定是数据库的 CDC 数据吗?
  • 基于STM32的RFID智能门锁系统
  • 测试用例的编写评审
  • 二叉树的前、中、后序遍历【c++】
  • Hadoop HDFS:海量数据的存储解决方案
  • Leetcode二十三题:合并K个升序链表【22/1000 python】
  • 03-echarts如何画立体柱状图
  • 2024蓝桥A组E题
  • Java单例模式
  • 04—常用方法和正则表达式
  • Python异常处理机制详解及示例
  • 解决:Java后端返回给前端的Date格式数据相差8小时的问题