byte-buddy icon indicating copy to clipboard operation
byte-buddy copied to clipboard

MethodDelegation.to(Object target) not work intercept mode?

Open lucas-myx opened this issue 2 years ago • 2 comments

hi, I'm not sure if this is a problem, I want to intercept a method(MyClass#test), using byte-buddy, simple example code as follow:

public class MyClass {
    public String test(){
        return "test"
    }
}

interceptor as follow:

package com.test.lucas
public class MyInterceptor {
    public String test(){
        return "intercept"
    }
}

when I use this api MethodDelegation to(Class<?> type) is OK: builder.transform((b, t, c, m) -> b.method(inst.getMethodMatcher()).intercept(MethodDelegation.to(MyInterceptor.class)))

but is not work if I change MethodDelegation.to(Object target): builder.transform((b, t, c, m) -> b.method(inst.getMethodMatcher()).intercept(MethodDelegation.to("com.test.lucas.MyInterceptor"))) and the following exception will be thrown:

java.lang.IllegalArgumentException: Cannot resolve ambiguous delegation of public java.lang.String com.test.lucas.MyClass.test() to public java.lang.String java.lang.String.stripTrailing() or public native java.lang.String java.lang.String.intern()
	at io.arex.agent.net.bytebuddy.implementation.bind.MethodDelegationBinder$BindingResolver$Default.doResolve(MethodDelegationBinder.java:639)
	at io.arex.agent.net.bytebuddy.implementation.bind.MethodDelegationBinder$BindingResolver$Default.doResolve(MethodDelegationBinder.java:658)
	at io.arex.agent.net.bytebuddy.implementation.bind.MethodDelegationBinder$BindingResolver$Default.doResolve(MethodDelegationBinder.java:658)
	...

The version I use is 1.12.8,thanks

lucas-myx avatar May 31 '22 03:05 lucas-myx

You cannot delegate to an instance of String.

What are you trying to do? Delegate to a non-loaded class? You'd need a TypeDescription then and use a TypePool to resolve it.

raphw avatar May 31 '22 06:05 raphw

Thank you for your reply. I understand what you said

lucas-myx avatar May 31 '22 08:05 lucas-myx