![](/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.