我正在尝试使用byte buddy lib为方法的度量执行时间创建一个java代理,而不更改main方法。我按照教程并创建以下代码。执行时……
您也可以通过使用来实现这一点 Advice 它使用代码内联,应该会带来更好的运行时性能:
Advice
class TimerAdvice { @Advice.OnMethodEnter static long enter() { return System.currentTimeMillis(); } @Advice.OnMethodExit(onException = Throwable.class) static void exit(@Advice.Origin String method, @Advice.Enter long start) { System.out.println(method + " took " + (System.currentTimeMillis() - start)); } }
并通过提出建议
new AgentBuilder.Default() .type(ElementMatchers.nameEndsWith("Timed")) .transform((builder, type, classLoader, module) -> builder.visit(Advice.to(TimerAdvice).on(ElementMatchers.any())); );
这样,时间也不会出现在堆栈跟踪中。
我找到了很好的解决方案,由Rafael Winterhalter给出。转到以下链接[ https://github.com/raphw/byte-buddy/issues/257 ]