javareflectionnettymethodhandle

Why do I get a WrongMethodTypeException on invokeExact even though MethodHandle is OK


I am trying to call an invoke method and there is an error that I cannot explain. I have heard that the Invoke Exact method has to return but it did not work even then.

public boolean exec(String id, ChannelHandlerContext ctx, ByteBuf byteBuf) {
        assert ctx != null;
        assert byteBuf != null;

        MethodHandle methodHandle = this.idMethods.get(id);
        if(methodHandle != null) {
            try {
                Object object = this.idObjects.get(id);

                Boolean b = (Boolean) methodHandle.invokeExact(object, ctx, byteBuf);

                return true;
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }
        return false;
}

on this code the Programm crash

Boolean b = (Boolean) methodHandle.invokeExact(object, ctx, byteBuf);

java.lang.invoke.WrongMethodTypeException: expected (,ChannelHandlerContext,ByteBuf)boolean but found (Object,ChannelHandlerContext,ByteBuf)Boolean
at java.lang.invoke.Invokers.newWrongMethodTypeException(Invokers.java:298)
at java.lang.invoke.Invokers.checkExactType(Invokers.java:309)
at de.moldiy.molnet.MessageExchangerManager.exec(MessageExchangerManager.java:54)
at de.moldiy.molnet.MessageHandler.channelRead0(MessageHandler.java:22)
at de.moldiy.molnet.MessageHandler.channelRead0(MessageHandler.java:7)
at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:99)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:324)
at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:296)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:714)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:650)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:576)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.lang.Thread.run(Thread.java:836)

This is the class that contains the method I want to call

s.loadMessageExchanger(new ServerMessageExchanger() {
    @TrafficID(id = "cords")
    public Boolean setCords(ChannelHandlerContext ctx,ByteBuf byteBuf) {
       System.out.println("jaaa");
       return true;
    }
});

Solution

  • i know now what was wrong, with invokeExact you can't define an anonymous class because the types have to be known from the beginning, so this is not possible:

            Object object = this.idObjects.get(id);
    
            methodHandle.invokeExact(object, ctx, byteBuf);
    

    But I can just use invoke:

            Object object = this.idObjects.get(id);
    
            methodHandle.invoke(object, ctx, byteBuf);