Search code examples
kotlinbyte-buddy

Bytebuddy: method interception doesn't work in Kotlin


Consider the following bytebuddy program to intercept method call load():

public class ByteJavaBuddyTest {
    public static class MemoryDatabase {
        public List<String> load(String info) {
            return Arrays.asList(info + ": foo", info + ": bar");
        }
    }

    public static class LoggerInterceptor {
        public static List<String> log(@SuperCall Callable<List<String>> zuper) throws Exception {
            System.out.println("Calling database");
            try {
                return zuper.call();
            } finally {
                System.out.println("Returned from database");
            }
        }
    }

    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        MemoryDatabase loggingDatabase = new ByteBuddy()
                .subclass(MemoryDatabase.class)
                .method(named("load")).intercept(MethodDelegation.to(LoggerInterceptor.class))
                .make()
                .load(MemoryDatabase.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
                .getLoaded()
                .getDeclaredConstructor()
                .newInstance();
        System.out.println(loggingDatabase.load("qux"));
    }
}

It runs and prints:

Calling database
Returned from database
[qux: foo, qux: bar]

After conversion to Kotlin it doesn't run the interceptor, nor does it throw any exceptions.

object ByteBuddyKotlinTest {
    open class MemoryDatabase {
        fun load(info: String): List<String> {
            return Arrays.asList("$info: foo", "$info: bar")
        }
    }

    object LoggerInterceptor {
        @Throws(Exception::class)
        fun log(@SuperCall zuper: Callable<List<String>>): List<String> {
            println("Calling database")
            try {
                return zuper.call()
            } finally {
                println("Returned from database")
            }
        }
    }

    @Throws(
        NoSuchMethodException::class,
        IllegalAccessException::class,
        InvocationTargetException::class,
        InstantiationException::class
    )
    @JvmStatic
    fun main(args: Array<String>) {
        val loggingDatabase = ByteBuddy()
            .subclass(MemoryDatabase::class.java)
            .method(ElementMatchers.named("load")).intercept(MethodDelegation.to(LoggerInterceptor::class.java))
            .make()
            .load(MemoryDatabase::class.java.classLoader, ClassLoadingStrategy.Default.WRAPPER)
            .loaded
            .getDeclaredConstructor()
            .newInstance()
        println(loggingDatabase.load("qux"))
    }
}

Prints:

[qux: foo, qux: bar]

Since it doesn't throw any errors I don't really know where to start digging.


Solution

  • Byte Buddy proxies methods by overriding them. If a method is not declared open in Kotlin, it is final in Java byte code. If you open your method, your logic should work again.