![](/img/trans.png)
[英]Is MethodDelegation intercept with ByteBuddy possible on Android?
[英]ByteBuddy MethodDelegation not working in Java Agent
我有一个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
为了实现我想做的事情,进行了以下更改:
在主要方面:
CountingInterception ci = new CountingInterception();
new AgentBuilder.Default()
.type(declaresMethod(isAnnotatedWith(Counted.class)))
.transform((builder, type, classLoader, module) -> builder
.method(isAnnotatedWith(Counted.class))
.intercept(MethodDelegation.to(ci).andThen(SuperMethodCall.INSTANCE))
).installOn(instrumentation);
在CountingInterception中:
public void interceptor(@Origin final Method method) throws Exception {
String name = method.getAnnotation(Counted.class).name();
if (name != null) {
// do something
}
}
我假设您正在尝试执行与Java 8默认方法调用不同的操作。 您是要使用@SuperCall
来调用super方法吗?
我建议您:1.减少拦截器不执行任何操作。 创建一个拦截器,该拦截器将您的MethodDelegation
与SuperMethodCall
。 2.注册一个AgentBuilder.Listener
以将错误写入控制台。
我确信Byte Buddy不能绑定您的方法,因为您的拦截器只能应用于提供默认方法实现的类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.