2
votes

I use Byte Buddy (v0.5.2) to dynamically create a "subclass" of an interface (actually, I want to create a class that implements that interface). All methods invoked on an instance of this class should be redirected to another (interceptor) class. I used the following code (with "TestInterface" being an interface that declares exactly one method "sayHello"):

final Interceptor interceptor = new Interceptor();
Class<?> clazz = new ByteBuddy()
        .subclass(TestInterface.class)
        .method(any()).intercept(MethodDelegation.to(interceptor))
        .make()
        .load(TestInterface.class.getClassLoader(), ClassLoadingStrategy.Default.INJECTION)
        .getLoaded();
TestInterface instance = (TestInterface) clazz.newInstance();
instance.sayHello();

The interceptor class looks like this:

public class Interceptor {

    public Object intercept(@Origin MethodHandle method, @AllArguments Object[] args) throws Throwable {
        ...
    }       

}

However, when I try to call the "sayHello" method (last line of my code example), I get an "IncompatibleClassChangeError". The stack trace is as follows:

Exception in thread "main" java.lang.IllegalAccessError: no such method: byteuddytest.TestInterface.sayHello()void/invokeVirtual
    at java.lang.invoke.MethodHandleNatives.linkMethodHandleConstant(MethodHandleNatives.java:448)
    at bytebuddytest.TestInterface$ByteBuddy$0E9xusGs.sayHello(Unknown Source)
    at bytebuddytest.Main.main(Main.java:32)
Caused by: java.lang.IncompatibleClassChangeError: Found interface bytebuddytest.TestInterface, but class was expected
    at java.lang.invoke.MethodHandleNatives.resolve(Native Method)
    at java.lang.invoke.MemberName$Factory.resolve(MemberName.java:965)
    at java.lang.invoke.MemberName$Factory.resolveOrFail(MemberName.java:990)
    at java.lang.invoke.MethodHandles$Lookup.resolveOrFail(MethodHandles.java:1387)
    at java.lang.invoke.MethodHandles$Lookup.linkMethodHandleConstant(MethodHandles.java:1732)
    at java.lang.invoke.MethodHandleNatives.linkMethodHandleConstant(MethodHandleNatives.java:442)
... 2 more

The problem seems to be related to the use of the "MethodHandle" parameter in my interceptor method. When I change the type to "Method", everything works fine. But according to the docs, "MethodHandle" should be preferred to "Method" because of performance reasons.

Is the error caused by a bug in Byte Buddy, or should I actually use a "Method" parameter in this case?

2

2 Answers

1
votes

Use a Method parameter and enable caching. That should solve most of your performance issues, if you have any in the first place.

See javadoc for @Origin:

public abstract boolean cacheMethod

If this value is set to true and the annotated parameter is a Method type, the value that is assigned to this parameter is cached in a static field. Otherwise, the instance is looked up from its defining Class on every invocation of the intercepted method.

Method look-ups are normally cached by its defining Class what makes a repeated look-up of a method little expensive. However, because Method instances are mutable by their AccessibleObject contact, any looked-up instance needs to be copied by its defining Class before exposing it. This can cause performance deficits when a method is for example called repeatedly in a loop. By enabling the method cache, this performance penalty can be avoided by caching a single Method instance for any intercepted method as a static field in the instrumented type.

1
votes

See the answer of Jeor which is totally correct (you should mark it as accepted). Just two remarks that do not fit into a comment:

You should of course only use a MethodHandle instead of a Method if the former allows you what you do. Invoking MethodHandles implies some JVM magic. Handles are resolved with a polymorphic signature by a JVM, i.e. their arguments must not be boxed as the JVM will simply replace the call site with a method call. In your case, this does therefore not work. The advantage of a method handle is however that it can be stored in the constant pool of a class. It is a native concept that can be accessed by a byte code instruction. Compared to that, a Method reference needs to be produced explicitly.

You should therefore rather cache the Method instance (which is mutable!). Also, note that you are currently also intercepting the methods of Object. You can clean up your code a bit by:

Class<? extends TestInterface> clazz = new ByteBuddy()
        .subclass(TestInterface.class)
        .method(isDeclaredBy(TestInterface.class))
        .intercept(MethodDelegation.to(interceptor))
        .make()
        .load(TestInterface.class.getClassLoader(), 
              ClassLoadingStrategy.Default.INJECTION)
        .getLoaded();

TestInterface instance = clazz.newInstance();