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

Unable to match methods

Open akshay-nagpal opened this issue 2 years ago • 2 comments

I am new to ByteBuddy and trying to replace a cglib implementation. Thanks for creating ByteBuddy!! Here is my implementation for bytebuddy proxy Interceptor Class:

public class Interceptor {
	private final Object targetObject;

	public Interceptor(Object targetObject) {
		super();
		this.targetObject = targetObject;
	}
	
	  public Object log(@Pipe Forwarder<Object,Object> pipe) {
		    System.out.println("Calling database");
		    try {
		      return pipe.to(targetObject);
		    } finally {
		      System.out.println("Returned from database");
		    }
		  }
}

Forwarder Class:

public interface Forwarder<T, S> { T to(S target); }

Bytebuddy Creation:

adapter = new ByteBuddy() .subclass(clazz) .method(ElementMatchers.isAbstract()) .intercept(MethodDelegation.withDefaultConfiguration() .withBinders(Pipe.Binder.install(Forwarder.class)) .to(new Interceptor(targetObject))) .make() .load(Source.class.getClassLoader(), ClassLoadingStrategy.Default.INJECTION) .getLoaded().newInstance();

This is the exception message i am getting though the method prepareStatement is implemented in the target object. It is not a protected method. The class is extended by target object is a package local abstract class. Why it is trying to look at only the methods from Object.class and not from the actual class of the target object?

None of [protected void java.lang.Object.finalize() throws java.lang.Throwable, public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException, public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException, public final void java.lang.Object.wait() throws java.lang.InterruptedException, public boolean java.lang.Object.equals(java.lang.Object), public java.lang.String java.lang.Object.toString(), public native int java.lang.Object.hashCode(), public final native java.lang.Class java.lang.Object.getClass(), protected native java.lang.Object java.lang.Object.clone() throws java.lang.CloneNotSupportedException, public final native void java.lang.Object.notify(), public final native void java.lang.Object.notifyAll(), public java.lang.Object com.demo.common.util.Interceptor.log(com.demo.common.util.Forwarder)] allows for delegation from public abstract java.sql.PreparedStatement java.sql.Connection.prepareStatement(java.lang.String,int) throws java.sql.SQLException

akshay-nagpal avatar Apr 13 '22 19:04 akshay-nagpal

You probably need to annotate the method with @RuntimeType.

raphw avatar Apr 13 '22 19:04 raphw

This worked for me. I was facing a similar issue

guru1306 avatar Apr 18 '22 09:04 guru1306