last nicer making

This commit is contained in:
Shautvast 2024-06-23 20:45:00 +02:00
parent 862aad4462
commit 3414c3d510

View file

@ -12,7 +12,6 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.security.ProtectionDomain; import java.security.ProtectionDomain;
@SuppressWarnings("ALL")
public class Agent { public class Agent {
private static final String MESSAGE = "--- Exceptional agent active"; private static final String MESSAGE = "--- Exceptional agent active";
@ -62,20 +61,21 @@ public class Agent {
}); });
} }
private static void instrumentMethodWithMyExceptionLogger(ClassBuilder classBuilder, MethodModel mm) { private static void instrumentMethodWithMyExceptionLogger(ClassBuilder classBuilder, MethodModel methodModel) {
classBuilder.withMethod(mm.methodName(), mm.methodType(), mm.flags().flagsMask(), methodModel.code().ifPresent(code ->
methodBuilder -> classBuilder.withMethod(methodModel.methodName(), methodModel.methodType(), methodModel.flags().flagsMask(),
methodBuilder.transformCode(mm.code().get(), ((builder, element) -> { methodBuilder ->
if (element instanceof ReturnInstruction) { methodBuilder.transformCode(code, ((builder, element) -> {
builder.aload(0); // load `this` on the stack, ie the Throwable instance if (element instanceof ReturnInstruction) {
builder.invokestatic( // call my code builder.aload(0); // load `this` on the stack, ie the Throwable instance
ClassDesc.of("com.github.shautvast.exceptional.ExceptionLogger"), "log", builder.invokestatic( // call my code
MethodTypeDesc.ofDescriptor("(Ljava/lang/Throwable;)V")); ClassDesc.of("com.github.shautvast.exceptional.ExceptionLogger"), "log",
builder.return_(); MethodTypeDesc.ofDescriptor("(Ljava/lang/Throwable;)V"));
} else { builder.return_();
builder.with(element); } else {
} builder.with(element); // leave any other bytecode in place
}) }
)); })
)));
} }
} }