dimanche 22 septembre 2019

How to create a proxy of an unbound generic type with ByteBuddy?

As an exercise, I need to extract the method name from a method reference. For example:

interface Marker {}

interface Foo extends Marker {

    String foo();
}

class MethodNameExtractor {

    static <T extends Marker> String extract(Function<T, String> methodReference) throws Exception {
        var proxy = (T) new ByteBuddy()
            .subclass(Marker.class)
            .method(not(isDeclaredBy(Object.class)).and(returns(String.class)))
            .intercept(InvocationHandlerAdapter.of(MethodNameExtractor::invoke))
            .make()
            .load(MethodNameExtractor.class.getClassLoader()).getLoaded().getConstructor()
            .newInstance();
        return methodReference.apply(proxy);
    }

    static Object invoke(Object proxy, Method method, Object[] args) {
        return method.getName();
    }
}


class MethodNameExtractorTest {

    @Test
    public void itExtractsTheMethodName() {
        assertEquals("foo", extract(Foo::foo));
    }
}

But the test throws:

java.lang.ClassCastException: class Marker$ByteBuddy$EHoz8bj6 cannot be cast to class Foo (Marker$ByteBuddy$EHoz8bj6 is in unnamed module of loader net.bytebuddy.dynamic.loading.ByteArrayClassLoader @4009e306; Foo is in unnamed module of loader 'app')
    at MethodNameExtractor.extract(MethodNameExtractor.java:3)
    at MethodNameExtractorTest.itExtractsTheMethodName(MethodNameExtractorTest.java:5)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:532)
    ...

So I cannot cast the proxy to T? How can I produce a proxy that can be passed as the argument of the method reference?





Aucun commentaire:

Enregistrer un commentaire