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

Byte-Buddy系列 - 第3讲 byte-buddy与jacoco agent冲突问题

目录

    • 一、原因分析
    • 二、解决方案

一、原因分析

使用byte-buddy对代码进行增强后,在通过mvn test执行测试时报如下错误:

class redefinition failed: attempted to delete a method

查看相关issue确定是因为集成了jacoco导致的:
https://github.com/raphw/byte-buddy/issues/1248
https://github.com/jacoco/jacoco/issues/1470

ByteBuddy 与 JaCoCo 同时使用时出现 class redefinition failed: attempted to delete a method 错误是由于两个 Java Agent 同时对同一个类进行修改造成的冲突。

Agent 1:jacoco agent(先执行,premain):

# 执行mvn test时的jacoco相关agent设置日志
[INFO] surefireArgLine set to 
-javaagent:E:\\mavenRepo\\org\\jacoco\\org.jacoco.agent\\0.8.12\\org.jacoco.agent-0.8.12-runtime.jar
=destfile=E:\\ideaWorkspace\\my-extend\\target\\jacoco.exec,
excludes=**/config/*:**/constant/*:**/bean/**

Agent 2:byte-buddy agent(后执行,agentmain):

// 通过编程方式安装byte-buddy agent
ByteBuddyAgent.install();

具体可能的 冲突原因 如下:

  1. 重复的类转换: 两个 Agent 可能尝试修改相同的类
  2. 类转换顺序: JaCoCo 在 ByteBuddy 之前运行,造成兼容性问题
  3. 字节码转换限制: Java 对已转换类的再次转换有严格限制

二、解决方案

配置Jacoco插件避开 ByteBuddy 要处理的类,即在jacoco中通过<exclude/>排除掉需要通过byte-buddy进行增强的类,避免二者对同一个类进行处理而产生冲突:

<plugin><groupId>org.jacoco</groupId><artifactId>jacoco-maven-plugin</artifactId><configuration><excludes><!-- 排除掉需要通过byte-buddy进行增强的类 --><exclude>**/YourModifedClass</exclude></excludes></configuration>
</plugin>

我在项目中的相关pom的详细配置如下:

<build><plugins><!-- Maven surefire插件 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>3.3.1</version><configuration><parallel>classes</parallel><threadCount>2</threadCount><testFailureIgnore>false</testFailureIgnore><!-- 解决jacoco插件无法生成报告 - Skipping JaCoCo execution due to missing execution data file --><argLine>-Djdk.net.URLClassPath.disableClassPathURLCheck=true -Duser.language=zh -Duser.country=CN -Duser.timezone=Asia/Shanghai -Dfile.encoding=UTF-8 ${surefireArgLine}</argLine><excludes><!-- 排除byte-buddy相关测试类(避免多次对同一个类进行增强导致冲突) --><exclude>**/ExtendBeanMethodTest.java</exclude><exclude>**/ExtendBeanPropTest.java</exclude></excludes></configuration></plugin><plugin><groupId>org.jacoco</groupId><artifactId>jacoco-maven-plugin</artifactId><version>${jacoco-maven.version}</version><configuration><excludes><exclude>**/config/*</exclude><exclude>**/constant/*</exclude><exclude>**/bean/**</exclude><!-- 额外排除掉需要通过byte-buddy进行增强的类 --><exclude>com.luo.MyModifiedObj</exclude></excludes></configuration><executions><execution><id>prepare-agent</id><goals><goal>prepare-agent</goal></goals><configuration><!-- 解决jacoco插件无法生成报告 - Skipping JaCoCo execution due to missing execution data file --><propertyName>surefireArgLine</propertyName></configuration></execution><execution><id>report</id><goals><goal>report</goal></goals></execution></executions></plugin></plugins>
</build>

注意如上配置中的maven-surefire-plugin插件的<exclude/>配置,我通过这段配置排除了个别测试类,避免我的多个测试都通过byte-buddy对同一个类进行处理产生冲突:

<!-- Maven surefire插件 -->
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><configuration><excludes><!-- 排除byte-buddy相关测试类(避免多次对同一个类进行增强导致冲突) --><exclude>**/ExtendBeanMethodTest.java</exclude><exclude>**/ExtendBeanPropTest.java</exclude></excludes></configuration>
</plugin>
http://www.xdnf.cn/news/1432.html

相关文章:

  • Qt Creator中自定义应用程序的可执行文件图标
  • node.js 实战——(path模块 知识点学习)
  • 计算机视觉基础
  • 编程实现ESP8266分别作为服务端 客户端
  • 集结号海螺捕鱼服务器调度与房间分配机制详解:六
  • nginx部署前端项目时,正常访问前端页面成功后,浏览器刷新报404解决访问
  • ​​OSPF核心机制精要:选路、防环与设计原理​
  • 一篇文章学会开发第一个ASP.NET网页
  • 金融租赁质检的三重业务困境 质检LIMS系统的四大价值赋能场景
  • “时间”,在数据处理中的真身——弼马温一般『无所不能』(DeepSeek)
  • MCU开发学习记录11 - ADC学习与实践(HAL库) - 单通道ADC采集、多通道ADC采集、定时器触发连续ADC采集 - STM32CubeMX
  • Python jsonpath库终极指南:json数据挖掘的精准导航仪
  • 消息中间件RabbitMQ02:账号的注册、点对点推送信息
  • MySQL运算符
  • kafka安装、spark安装
  • 5.学习笔记-SpringMVC(P53-P60)
  • Spring Boot 的配置加载顺序
  • Elasticsearch学习
  • 【Hive入门】Hive基础操作与SQL语法:DDL操作全面指南
  • 国内ip地址怎么改?详细教程
  • AI搜索AI SEO排名:国际采购商的搜索行为正在被AI重塑
  • 高防IP是什么
  • 批量处理多个 Word 文档:插入和修改页眉页脚,添加页码的方法
  • 什么是量子计算?它能做什么?
  • JAVA同步器CyclicBarrier
  • 【Project】基于spark-App端口懂车帝数据采集与可视化
  • 【网络原理】TCP提升效率机制(一):滑动窗口
  • VBA批量读取access数据库(.mdb)
  • JAVA猜数小游戏
  • 面试篇:Java集合