moderner this way
This commit is contained in:
parent
0123d5f7b4
commit
f275d0de62
1 changed files with 5 additions and 20 deletions
|
|
@ -1,8 +1,5 @@
|
||||||
package com.github.shautvast.exceptional;
|
package com.github.shautvast.exceptional;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.lang.classfile.ClassElement;
|
import java.lang.classfile.ClassElement;
|
||||||
import java.lang.classfile.ClassFile;
|
import java.lang.classfile.ClassFile;
|
||||||
import java.lang.classfile.ClassModel;
|
import java.lang.classfile.ClassModel;
|
||||||
|
|
@ -27,34 +24,22 @@ public class Agent {
|
||||||
instrumentation.addTransformer(new ClassFileTransformer() {
|
instrumentation.addTransformer(new ClassFileTransformer() {
|
||||||
@Override
|
@Override
|
||||||
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) {
|
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) {
|
||||||
return instrumentTheThrowable(className, classfileBuffer);
|
return instrumentThrowable(className, classfileBuffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public byte[] transform(Module module, ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
|
public byte[] transform(Module module, ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
|
||||||
return instrumentTheThrowable(className, classfileBuffer);
|
return instrumentThrowable(className, classfileBuffer);
|
||||||
}
|
}
|
||||||
}, true);
|
}, true);
|
||||||
|
|
||||||
InputStream stream = Agent.class.getResourceAsStream("/java/lang/Throwable.class");
|
byte[] bytecode = Files.readAllBytes(
|
||||||
byte[] bytecode = readFully(stream);
|
Path.of(Agent.class.getResource("/java/lang/Throwable.class").toURI()));
|
||||||
|
|
||||||
instrumentation.redefineClasses(new ClassDefinition(Throwable.class, bytecode));
|
instrumentation.redefineClasses(new ClassDefinition(Throwable.class, bytecode));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static byte[] readFully(InputStream stream) throws IOException {
|
private static byte[] instrumentThrowable(String className, byte[] classfileBuffer) {
|
||||||
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
|
||||||
int nRead;
|
|
||||||
byte[] data = new byte[1024];
|
|
||||||
while ((nRead = stream.read(data, 0, data.length)) != -1) {
|
|
||||||
buffer.write(data, 0, nRead);
|
|
||||||
}
|
|
||||||
|
|
||||||
buffer.flush();
|
|
||||||
return buffer.toByteArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static byte[] instrumentTheThrowable(String className, byte[] classfileBuffer) {
|
|
||||||
if (className.equals("java/lang/Throwable")) {
|
if (className.equals("java/lang/Throwable")) {
|
||||||
ClassFile classFile = ClassFile.of();
|
ClassFile classFile = ClassFile.of();
|
||||||
ClassModel classModel = classFile.parse(classfileBuffer);
|
ClassModel classModel = classFile.parse(classfileBuffer);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue