問題描述
1、我們意圖在實際操作之前加入一些字節(jié)碼進行監(jiān)控,例如對 慢sql, dubbo 并發(fā) ,開發(fā)發(fā)布對時候?qū)ybatis/ibatis對配置文件做檢查
問題出現(xiàn)的環(huán)境背景及自己嘗試過哪些方法
2、我們使用 Instrumentation 在啟動的時候,將我們監(jiān)控開發(fā)的 Agent jar 包導入即可,但是發(fā)現(xiàn)使用 javassist 的如下代碼不起作用
ClassPool pool = new ClassPool();//這一行之后就沒有運行了,
byte[] transformed = new byte[0];
CtClass cl = null;
try {
cl = pool.get(className.replaceAll("/", "."));
System.out.println("four");
CtBehavior[] methods = cl.getDeclaredBehaviors();
for (CtBehavior method : methods) {
if (!method.isEmpty()) {
doMethod(method);
}
}
transformed = cl.toBytecode();
} catch (CannotCompileException | NotFoundException | IOException io) {
io.printStackTrace();
} finally {
if (cl != null) {
cl.detach();
}
}
return transformed;
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">
<modelVersion>4.0.0</modelVersion>
<groupId>top.huzhurong.agent</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.23.1-GA</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<target>1.8</target>
<source>1.8</source>
<encoding>utf-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
<manifestEntries>
<Premain-Class>test.Agent</Premain-Class>
<Boot-Class-Path>javassist-3.23.1-GA.jar</Boot-Class-Path>
<Can-Redefine-Classes>true</Can-Redefine-Classes>
<Can-Set-Native-Method-Prefix>true</Can-Set-Native-Method-Prefix>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
google,百度也沒有找到什么,本身也不太好調(diào)試,以前使用 ASM 是可以的。提前感謝
添加回答
舉報
0/150
提交
取消