为什么我的 Javassist 类 没有在运行时执行?
Why aren't my Javassist classes executed at runtime?
我一直在编写一个 java 代理来修改加载的 classes 的字节码。
我的代码包含 4 classes:
- StackTraceAgent.java - java代理class同方法
预维护。
- StackTraceClassTransformer.java - 执行
java.lang.instrument.ClassFileTransformer.
- Student.java - 只是自定义 class 用于测试。
- AgentTest - 简单 class 方法 main.
使用的库:
- javassist-3.20.0-GA.jar
StackTraceAgent.java、ClassFileTransformer.java 和 javassist-3.20.0-GA.jar 打包在一个 jar 文件中。它是通过“-javaagent:MyJar.jar AgentTest”调用的。我的问题是代码是使用 javassist 编写的 classes 不会在运行时调用。它只是被一个运行时没有任何错误地向上传递,最后程序继续执行非javassist代码直到结束。
我是如何尝试解决问题的:
- 我查看了已编译的 classes。所有 classes 都包含准确的代码。
- 我感觉打包成jar有问题,但是网上冲浪了好几个小时也没发现什么有趣的东西。
MANIFEST.MF:
Manifest-Version: 1.0
Premain-Class: agent.StackTraceAgent
我的 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>com.belenov</groupId>
<artifactId>TraceAgent</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.javassist/javassist -->
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.20.0-GA</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/classes/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestFile>
src/main/resources/META-INF/MANIFEST.MF
</manifestFile>
<manifest>
<addClasspath>false</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
StackTraceAgent.java:
import java.lang.instrument.Instrumentation;
public class StackTraceAgent {
public static void premain(String args, Instrumentation instrumentation) {
System.out.println("instrument agent");
StackTraceClassTransformer transformer = new StackTraceClassTransformer();
instrumentation.addTransformer(transformer);
}
}
StackTraceClassTransformer.java:
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.security.ProtectionDomain;
public class StackTraceClassTransformer implements ClassFileTransformer {
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,
ProtectionDomain protectionDomain,
byte[] classfileBuffer) throws IllegalClassFormatException {
try {
//the code below is passed up
ClassPool classPool = ClassPool.getDefault();
CtClass ctClass = classPool.get(className);
CtMethod[] methods = ctClass.getMethods();
for (CtMethod method : methods) {
method.insertAfter("System.out.println(\"Method name: \"+" + method.getName() + "+\" \"+" + method.getSignature() + ");");
method.insertAfter("System.out.println(\"Object: \" + this.toString());");
}
byte[] byteCode = ctClass.toBytecode();
ctClass.detach();
return byteCode;
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
}
AgentTest.java
public class AgentTest {
public static void main(String[] args) {
System.out.println("Agent Tester");
Student student = new Student("vasia", "poopkin");
student.toString();
}
}
Student.java
public class Student {
private String name;
private String surname;
public Student(String name, String surname) {
this.name = name;
this.surname = surname;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", surname='" + surname + '\'' +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
}
基本上在你的清单文件中,你需要包括你的程序使用的每个库和资源的类路径,你可以在 MANIFEST.MF:
中添加这样一行
Class-Path: path/to/my/libraries
一种方法是简单地更新您的 pom.xml,maven jar 插件的清单部分:
<addClasspath>true</addClasspath>
<classpathPrefix>${project.build.directory}/classes/lib</classpathPrefix>
我一直在编写一个 java 代理来修改加载的 classes 的字节码。 我的代码包含 4 classes:
- StackTraceAgent.java - java代理class同方法 预维护。
- StackTraceClassTransformer.java - 执行 java.lang.instrument.ClassFileTransformer.
- Student.java - 只是自定义 class 用于测试。
- AgentTest - 简单 class 方法 main.
使用的库:
- javassist-3.20.0-GA.jar
StackTraceAgent.java、ClassFileTransformer.java 和 javassist-3.20.0-GA.jar 打包在一个 jar 文件中。它是通过“-javaagent:MyJar.jar AgentTest”调用的。我的问题是代码是使用 javassist 编写的 classes 不会在运行时调用。它只是被一个运行时没有任何错误地向上传递,最后程序继续执行非javassist代码直到结束。
我是如何尝试解决问题的:
- 我查看了已编译的 classes。所有 classes 都包含准确的代码。
- 我感觉打包成jar有问题,但是网上冲浪了好几个小时也没发现什么有趣的东西。
MANIFEST.MF:
Manifest-Version: 1.0
Premain-Class: agent.StackTraceAgent
我的 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>com.belenov</groupId>
<artifactId>TraceAgent</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.javassist/javassist -->
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.20.0-GA</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/classes/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestFile>
src/main/resources/META-INF/MANIFEST.MF
</manifestFile>
<manifest>
<addClasspath>false</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
StackTraceAgent.java:
import java.lang.instrument.Instrumentation;
public class StackTraceAgent {
public static void premain(String args, Instrumentation instrumentation) {
System.out.println("instrument agent");
StackTraceClassTransformer transformer = new StackTraceClassTransformer();
instrumentation.addTransformer(transformer);
}
}
StackTraceClassTransformer.java:
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.security.ProtectionDomain;
public class StackTraceClassTransformer implements ClassFileTransformer {
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,
ProtectionDomain protectionDomain,
byte[] classfileBuffer) throws IllegalClassFormatException {
try {
//the code below is passed up
ClassPool classPool = ClassPool.getDefault();
CtClass ctClass = classPool.get(className);
CtMethod[] methods = ctClass.getMethods();
for (CtMethod method : methods) {
method.insertAfter("System.out.println(\"Method name: \"+" + method.getName() + "+\" \"+" + method.getSignature() + ");");
method.insertAfter("System.out.println(\"Object: \" + this.toString());");
}
byte[] byteCode = ctClass.toBytecode();
ctClass.detach();
return byteCode;
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
}
AgentTest.java
public class AgentTest {
public static void main(String[] args) {
System.out.println("Agent Tester");
Student student = new Student("vasia", "poopkin");
student.toString();
}
}
Student.java
public class Student {
private String name;
private String surname;
public Student(String name, String surname) {
this.name = name;
this.surname = surname;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", surname='" + surname + '\'' +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
}
基本上在你的清单文件中,你需要包括你的程序使用的每个库和资源的类路径,你可以在 MANIFEST.MF:
中添加这样一行Class-Path: path/to/my/libraries
一种方法是简单地更新您的 pom.xml,maven jar 插件的清单部分:
<addClasspath>true</addClasspath>
<classpathPrefix>${project.build.directory}/classes/lib</classpathPrefix>