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

main.reflection.kType.kt Maven / Gradle / Ivy

There is a newer version: 0.77.0
Show newest version
package de.peekandpoke.ultra.common.reflection

import kotlin.reflect.KClass
import kotlin.reflect.KType
import kotlin.reflect.KTypeProjection
import kotlin.reflect.full.createType
import kotlin.reflect.typeOf

/**
 * Creates a [TypeRef] from the given class [T]
 *
 * The given type [T] must not be generic, otherwise an exception will be thrown
 */
inline fun  kType(): TypeRef {

    val type: KType = typeOf()

    return TypeRef.createForKType(type = type)
}

/**
 * Creates a [TypeRef] from the given [Class]
 */
fun  Class.kType(): TypeRef = kotlin.kType()

/**
 * Creates a [TypeRef] from the given [KClass]
 */
fun  KClass.kType(): TypeRef = TypeRef.createForKClass(cls = this, nullable = false)

/**
 * Creates a [TypeRef] for a List type of the given type
 */
inline fun  kListType(): TypeRef> = kType().list

/**
 * Creates a [TypeRef] for a Map type with the given [KEY] and [VAL] types
 */
inline fun  kMapType(): TypeRef> =
    TypeRef.createForKType(
        Map::class.createType(
            arguments = listOf(
                KTypeProjection.invariant(kType().type),
                KTypeProjection.invariant(kType().type)
            )
        )
    )

/**
 * Creates a [TypeRef] for a MutableMap type with the given [KEY] and [VAL] types
 */
inline fun  kMutableMapType(): TypeRef> =
    TypeRef.createForKType(
        MutableMap::class.createType(
            arguments = listOf(
                KTypeProjection.invariant(kType().type),
                KTypeProjection.invariant(kType().type)
            )
        )
    )




© 2015 - 2024 Weber Informatics LLC | Privacy Policy