// TARGET_BACKEND: JVM // WITH_RUNTIME // FULL_JDK import kotlin.test.assertEquals import java.lang.reflect.ParameterizedType import java.lang.reflect.Type open class TypeLiteral { val type: Type get() = (javaClass.getGenericSuperclass() as ParameterizedType).getActualTypeArguments()[0] } inline fun typeLiteral(): TypeLiteral = object : TypeLiteral() {} fun box(): String { assertEquals("java.lang.String", (typeLiteral().type as Class<*>).canonicalName) assertEquals("java.util.List", typeLiteral>().type.toString()) //note that 'type' implementation for next cases is different on jdk 6 and 8: GenericArrayType and Class assertEquals("java.lang.String[]", typeLiteral>().type.canonicalName) assertEquals("java.lang.Integer[]", typeLiteral>().type.canonicalName) assertEquals("java.lang.String[][]", typeLiteral>>().type.canonicalName) return "OK" } val Type.canonicalName: String get() = when (this) { is Class<*> -> this.canonicalName is java.lang.reflect.GenericArrayType -> this.getGenericComponentType().canonicalName + "[]" else -> null!! }