public static void premain(String arg, Instrumentation instrumentation) {
System.out.println("Agent for time measure:" + arg);
}
我想通过以下方法获取arg,否则不使用arg。最好,在OnMethodExit方法中获取arg。
public class TimerAdvice3 {
@Advice.OnMethodEnter
static long enter(@Advice.Origin String method, @Advice.AllArguments Object[] args) throws Exception {
HttpUriRequest httpUriRequest = (HttpUriRequest) args[1];
System.out.println(httpUriRequest.toString());
long start = System.currentTimeMillis();
return start;
}
@Advice.OnMethodExit
static void exit(@Advice.Origin String method, @Advice.Enter long start) throws Exception {
long end = System.currentTimeMillis();
System.out.println("拦截======》" + method + " took " + (end - start) + " milliseconds ");
}
}
您是否尝试应用所写的建议?您需要为此使用AgentBuilder
API:
new AgentBuilder.Default()
.type(...)
.transform((builder, type, classLoader, module) -> builder.visit(Advice
.to(TimerAdvice.class)
.on(...))
.installOn(instrumentation);
如果您只想阅读@Advice.Argument(0)
,则可以使用HttpUriRequest
。