All Downloads are FREE. Search and download functionalities are using the official Maven repository.

utility.KType.kt Maven / Gradle / Ivy

There is a newer version: 0.9.24
Show newest version
package com.github.fluidsonic.fluid.json

import kotlin.reflect.KClass
import kotlin.reflect.KClassifier
import kotlin.reflect.KType
import kotlin.reflect.KTypeParameter
import kotlin.reflect.jvm.internal.KotlinReflectionInternalError

// from kotlin-reflect, modified, probably wrong in some edge cases, close enough in order to avoid kotlin-reflect dependency for now


/**
 * Returns the [KClass] instance representing the runtime class to which this type is erased to on JVM.
 */
@SinceKotlin("1.1")
val KType.jvmErasure: KClass<*>
	get() = classifier?.jvmErasure ?: throw KotlinReflectionInternalError("Cannot calculate JVM erasure for type: $this")


internal val KClassifier.jvmErasure: KClass<*>
	get() = when (this) {
		is KClass<*> -> this
		is KTypeParameter -> {
			val bounds = upperBounds
			val representativeBoundErasures = bounds.mapNotNull { it.classifier?.jvmErasure }
			representativeBoundErasures.firstOrNull { !(it.java.isInterface || it.java.isAnnotation) }
				?: representativeBoundErasures.firstOrNull()
				?: Any::class
		}
		else -> throw KotlinReflectionInternalError("Cannot calculate JVM erasure for type: $this")
	}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy