Class<T> type on an template Class of type T, why?


#1

I the class bellow I had to make abstract because I would not find a why to to have the “type” method implemented I has expecting to be able to do something like:

abstract fun type(): T::class.java

But it does not work. and I ended up to have to redefine this method in several classes.

package com.integrando.jurigest.wire.codec

import io.vertx.core.eventbus.MessageCodec

abstract class AutoDiscoverableMessageCodec<T> : MessageCodec<T, T> {
    override fun name(): String = javaClass.simpleName
    override fun transform(payload: T): T = payload
    abstract fun type(): Class<T>
    override fun systemCodecID(): Byte = -1
}

#2

Read this.

TLDR; you don’t know what T is at runtime thus you must implement it differently, e.g. pass the class in a constructor