Quantcast
Channel: Active questions tagged methodhandle - Stack Overflow
Viewing all articles
Browse latest Browse all 36

Why a WrongMethodTypeException from MethodHandle? Is my object type incorrect?

$
0
0

I have encountered a problem while I am trying to switch my event system from reflection to MethodHandle.

I am using an event bus (version 3.0.0) by KyoriPowered on Github (https://github.com/KyoriPowered/event).

My code is following:

public class EventExecutorFactory implements EventExecutor.Factory<Event, Listener> {    @Override    public @NonNull EventExecutor<Event, Listener> create(@NonNull Object object, @NonNull Method method) throws Exception { // object is Listener        method.setAccessible(true);        Class<? extends Event> actualEventType = method.getParameterTypes()[0].asSubclass(Event.class);        MethodHandle handle = MethodHandles.lookup().unreflect(method);        return new EventExecutor<Event,Listener>() {            @Override            public void invoke(@NonNull Listener listener, @NonNull Event event) throws Throwable {                if (!actualEventType.isInstance(event)) return; // many different event types defined in my system, so I should check it first.                handle.invoke(actualEventType.cast(event)); // WrongMethodTypeException thrown here            }        }    }}

I expected this to work fine, but the result is:

java.lang.invoke.WrongMethodTypeException: cannot convert MethodHandle(,UserOnlineEvent)void to (Event)void

UserOnlineEvent is the event type that used in test.

The problem is that I cannot get the real type of the event.


Edit: This problem has been solved. I should use two arguments.just add the listener as the first argument to handle.invoke, and it works.


Viewing all articles
Browse latest Browse all 36

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>