【发布时间】:2017-03-02 15:06:19
【问题描述】:
我有一个 premain() ,其中所有使用某个注释注释的方法都应该委托给某个类。一般来说,我看起来像这样:
public static void premain( final String agentArguments, final Instrumentation instrumentation ) {
CountingInterception ci = new CountingInterception();
new AgentBuilder.Default()
.type(ElementMatchers.isAnnotatedWith(com.codahale.metrics.annotation.Counted.class))
.transform((builder, type, classLoader, module) ->
builder.method(ElementMatchers.any())
.intercept(MethodDelegation.to(ci))
).installOn(instrumentation);
}
使用调试器显示这部分已被处理,但如果调用了带注释的方法,则没有任何反应。
CountingInterception 看起来像这样
public class CountingInterception {
@RuntimeType
public Object intercept(@DefaultCall final Callable<?> zuper, @Origin final Method method, @AllArguments final Object... args) throws Exception {
String name = method.getAnnotation(Counted.class).name();
if (name != null) {
// do something
}
return zuper.call();
}
}
感谢任何提示!
使用 ByteBuddy 1.6.9
【问题讨论】:
标签: java byte-buddy