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,10 +61,11 @@ 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 ->
classBuilder.withMethod(methodModel.methodName(), methodModel.methodType(), methodModel.flags().flagsMask(),
methodBuilder -> methodBuilder ->
methodBuilder.transformCode(mm.code().get(), ((builder, element) -> { methodBuilder.transformCode(code, ((builder, element) -> {
if (element instanceof ReturnInstruction) { if (element instanceof ReturnInstruction) {
builder.aload(0); // load `this` on the stack, ie the Throwable instance builder.aload(0); // load `this` on the stack, ie the Throwable instance
builder.invokestatic( // call my code builder.invokestatic( // call my code
@ -73,9 +73,9 @@ public class Agent {
MethodTypeDesc.ofDescriptor("(Ljava/lang/Throwable;)V")); MethodTypeDesc.ofDescriptor("(Ljava/lang/Throwable;)V"));
builder.return_(); builder.return_();
} else { } else {
builder.with(element); builder.with(element); // leave any other bytecode in place
} }
}) })
)); )));
} }
} }