![](/img/trans.png)
[英]ByteBuddy invoke constructor in same class with argument transformation
[英]Instrumenting multiple methods within same class with ByteBuddy
我目前正在嘗試制作一個記錄器代理,目前正在其中攔截類PrepareStatement。 在PrepareStatement中,我希望跟蹤多種方法,但是我感覺自己做錯了。
通常,我現在要做的是攔截希望監視的每種方法,並將該agentbuilder安裝到工具中,如下所示:
private static void Install(String className, String methodName,
Instrumentation instr) {
new AgentBuilder.Default().disableClassFormatChanges()
.with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION)
.type(ElementMatchers.hasSuperType(ElementMatchers.named(className)))
.transform((builder, typeDescription, classLoader, module) -> {
return builder.visit(Advice.to(MyInterceptor.class)
.on(ElementMatchers.named(methodName)));
}).installOn(instrumentation);
}
.installOn(instr); 感覺不對,因為我真正想做的是將許多建議應用於同一個變壓器,然后將其安裝到儀器。
我正在嘗試實現的一些偽代碼:
private static AgentBuilder Install(String className,
Instrumentation instr) {
return new AgentBuilder.Default().disableClassFormatChanges()
.with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION)
.type(ElementMatchers.hasSuperType(ElementMatchers.named(className)))
.transform((builder, typeDescription, classLoader, module) -> {
builder.visit(Advice.to(MyFirstInterceptor.class)
.on(ElementMatchers.named("first method")));
builder.visit(Advice.to(MySecondInterceptor.class)
.on(ElementMatchers.named("second method")));
});
}
我將如何更優雅地實現這一目標? 如何通過多個類完成此操作?
感謝任何幫助! 親切的問候。
如果使用Advice
作為訪客,則裝飾物可以組成:
return builder
.visit(Advice.to(MyFirstInterceptor.class)
.on(ElementMatchers.named("first method")))
.visit(Advice.to(MySecondInterceptor.class)
.on(ElementMatchers.named("second method")));
如果要定位多個類型,則還可以在安裝之前鏈接多個type
語句。 如果多個這樣的匹配器匹配一個類型,則除非您在DSL中指定了asDecorator
,否則只有最后一個類型被匹配。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.