Ketlin reified type parameter as function parameter

I have a built-in function with a parameter of type reified with lambda that returns a new instance of an anonymous class that has one method that takes this reified type parameter and passes it to lambda.

inline fun <reified T : Any> eventHandler(crossinline handler: (T) -> Unit) = object {
    @Handler
    fun event(event: T) = handler(event)
}

      

Used like eventHandler<ExampleEvent> { println(it.data); }

, and everything compiles.

However, by examining the generated bytecode of the class generated by the specified call, I get this for the event method:

public final void event(example.ExampleEvent);
    descriptor: (Ljava/lang/Object;)V
    [...]
    Signature: #53 // (Lexample.ExampleEvent;)V

      

Therefore, when it correctly remembers the type in the Signature, it discards it in the descriptor; so thatEventMethod.getParameterTypes()

will have Object

instead of ExampleEvent

.

Is this a bug or intended behavior? Also, if this is intended, could there be some other way to achieve my goal (to prevent this clumsy object from being created all over the place, add a dummy method with @Handler, etc.)?

+3


source to share





All Articles