Java Instrumentation小结

实验代码参考 https://gitee.com/woopokyuk/instrumentation-test

字节码修改三要素

  • Agent类
  • Transformer类
  • 目标测试类

注意事项

  • 在POM中添加依赖
1
2
3
4
5
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.8.0.GA</version>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<configuration>
<archive>
<manifestEntries>
<Premain-Class>com.AopAgentTest</Premain-Class>
<Boot-Class-Path>D:/Download/javassist-3.8.0.GA.jar</Boot-Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
  • 配置MF文件
1
2
3
4
5
6
Premain-Class: com.AopAgentTest
Agent-Class: com.AopAgentTest
Can-Redefine-Classes: true
Class-Path: D:/Download/javassist-3.8.0.GA.jar
Can-Retransform-Classes: true

最后通过执行

1
java -javaagent:D:\Projects\2022SEU\instrumentationTest\target\instrumentationTest.jar com.AopAgentTest

即可得到结果



----------- 本文结束 -----------




0%