mercredi 20 janvier 2021

Unable to call a KCallable after passing it to a constructor of another object

I'm trying to figure out what's wrong with this code. Basically I get a reference to a method of a class using reflection and then I try to store it in another object for later calls. The method is of the Listener object. The problem is that if try to call the function from inside that storing object, I get a mismatched type exception because it is not expecting a Listener instance but a Listener.myMethod instance.

typealias EventFunction = (Event)->Unit
val priority = method.getAnnotation(EventHandler::class.java).priority
val function = eventListener::class.members.find{it.name == methodName}!! as KFunction<EventFunction>
val eventQueueElement = EventQueueElement(priority, function, eventListener)
//Here's where the code brakes
eventQueueElement.function.call(eventQueueElement.listener, eventQueueElement.priority)

But if I call the function direclty without passing it to the EventQueueElement constructor it works

function.call(eventListener, priority)

Here's the definition of the storing object

private class EventQueueElement(val priority: EventPriority, val function: KFunction<EventFunction>, val listener: Listener) : Comparable<EventQueueElement>{
        override fun compareTo(other: EventQueueElement): Int {
            return priority.compareTo(other.priority)
        }
    }




Aucun commentaire:

Enregistrer un commentaire