mirror of
https://github.com/xiaozhou233/JuiceAgent.git
synced 2026-12-05 13:05:53 +08:00
- add Java tests for getClassBytes(ByName), redefineClass(ByName), retransformClass(ByName) and defineClass - ignore tests/tmp/ directory for captured bytecode output
37 lines
1.2 KiB
Java
37 lines
1.2 KiB
Java
import cn.xiaozhou233.juiceagent.api.JuiceAgent;
|
|
|
|
import java.io.File;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
|
|
public class GetClassBytesByName {
|
|
public static void main(String[] args) throws IOException {
|
|
System.load(new File("resources/libagent.dll").getAbsolutePath());
|
|
JuiceAgent.init("");
|
|
|
|
String className = "Target";
|
|
byte[] bytes = JuiceAgent.getClassBytesByName(className);
|
|
|
|
if (bytes == null || bytes.length == 0) {
|
|
System.out.println("Failed to get class bytes: " + className);
|
|
return;
|
|
}
|
|
|
|
// Convert JVM internal class name to a file path.
|
|
String fileName = className.replace('.', '/').replace('$', '_') + ".class";
|
|
File output = new File("tmp", fileName);
|
|
|
|
File parent = output.getParentFile();
|
|
if (parent != null && !parent.exists()) {
|
|
parent.mkdirs();
|
|
}
|
|
|
|
try (FileOutputStream out = new FileOutputStream(output)) {
|
|
out.write(bytes);
|
|
}
|
|
|
|
System.out.println("Class: " + className);
|
|
System.out.println("Bytes: " + bytes.length);
|
|
System.out.println("Saved: " + output.getAbsolutePath());
|
|
}
|
|
} |