CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-jetbrains-kotlin--kotlin-reflect

Runtime component for full Kotlin reflection support, enabling inspection and manipulation of Kotlin classes, functions, properties, and types at runtime

Pending
Overview
Eval results
Files

type-system.mddocs/

Type System

Comprehensive type representation and manipulation including type creation, variance handling, projection operations, and subtype relationships for advanced type-level programming.

Capabilities

Type Projections

Handle type variance and projections including star projections for generic type safety.

/**
 * Represents a type projection with optional variance
 */
data class KTypeProjection(
  /** The use-site variance specified in the projection, or null for star projection */
  val variance: KVariance?,
  /** The type specified in the projection, or null for star projection */
  val type: KType?
) {
  companion object {
    /** Star projection, denoted by the * character */
    val STAR: KTypeProjection
    
    /** Creates an invariant projection of a given type */
    fun invariant(type: KType): KTypeProjection
    
    /** Creates a contravariant projection (in modifier) */
    fun contravariant(type: KType): KTypeProjection
    
    /** Creates a covariant projection (out modifier) */
    fun covariant(type: KType): KTypeProjection
  }
}

/**
 * Represents type variance
 */
enum class KVariance {
  INVARIANT, IN, OUT
}

Usage Examples:

// Create type projections
val stringType = typeOf<String>()
val invariantProjection = KTypeProjection.invariant(stringType)
val covariantProjection = KTypeProjection.covariant(stringType)
val contravariantProjection = KTypeProjection.contravariant(stringType)
val starProjection = KTypeProjection.STAR

// Inspect projections
println(covariantProjection.variance)      // OUT
println(contravariantProjection.variance)  // IN
println(starProjection.type)               // null

// Use in type construction
val listClass = List::class
val projectedListType = listClass.createType(listOf(covariantProjection))

Type Creation

Programmatically create KType instances with specific type arguments, nullability, and annotations.

/**
 * Creates a KType instance with the given classifier, type arguments, and nullability
 */
fun KClassifier.createType(
  arguments: List<KTypeProjection> = emptyList(),
  nullable: Boolean = false,
  annotations: List<Annotation> = emptyList()
): KType

/**
 * Creates an instance of KType with the given classifier, 
 * substituting all type parameters with star projections
 */
val KClassifier.starProjectedType: KType

/**
 * Returns a type corresponding to the given class with type parameters 
 * substituted as corresponding arguments
 * @deprecated Use starProjectedType or createType() for clearer semantics
 */
@Deprecated("This function creates a type which rarely makes sense for generic classes")
val KClass<*>.defaultType: KType

Usage Examples:

// Create simple types
val stringClass = String::class
val stringType = stringClass.createType()
val nullableStringType = stringClass.createType(nullable = true)

// Create generic types
val listClass = List::class
val stringProjection = KTypeProjection.invariant(stringType)
val listOfStringType = listClass.createType(listOf(stringProjection))

// Create star projected types
val rawListType = listClass.starProjectedType  // List<*>

// Inspect created types
println(nullableStringType.isMarkedNullable)  // true
println(listOfStringType.arguments.size)      // 1

Type Operations

Perform advanced type operations including nullability changes and subtype checking.

/**
 * Returns a new type with the same classifier and arguments but given nullability
 */
fun KType.withNullability(nullable: Boolean): KType

/**
 * Returns true if this type is the same or is a subtype of other
 */
fun KType.isSubtypeOf(other: KType): Boolean

/**
 * Returns true if this type is the same or is a supertype of other
 */
fun KType.isSupertypeOf(other: KType): Boolean

Usage Examples:

val stringType = typeOf<String>()
val nullableStringType = typeOf<String?>()
val anyType = typeOf<Any>()

// Change nullability
val madeNullable = stringType.withNullability(true)
val madeNonNull = nullableStringType.withNullability(false)

println(madeNullable.isMarkedNullable)  // true
println(madeNonNull.isMarkedNullable)   // false

// Check type relationships
println(stringType.isSubtypeOf(anyType))     // true
println(anyType.isSupertypeOf(stringType))   // true
println(stringType.isSubtypeOf(nullableStringType))  // true

Type Inspection

Deep inspection of type structure including classifier access and argument analysis.

/**
 * Access type components for detailed analysis
 */
interface KType {
  /** The declaration of the classifier used in this type */
  val classifier: KClassifier?
  /** Type arguments passed for the classifier parameters */
  val arguments: List<KTypeProjection>
  /** True if this type was marked nullable in source */
  val isMarkedNullable: Boolean
}

Usage Examples:

// Analyze complex generic types
val mapType = typeOf<Map<String, List<Int>>>()

// Inspect classifier
println(mapType.classifier)  // interface kotlin.collections.Map

// Analyze type arguments
val typeArgs = mapType.arguments
println(typeArgs.size)  // 2

// First argument (String)
val keyType = typeArgs[0].type
println(keyType?.classifier)  // class kotlin.String

// Second argument (List<Int>)
val valueType = typeArgs[1].type
val valueArgs = valueType?.arguments
println(valueArgs?.size)  // 1
println(valueArgs?.get(0)?.type?.classifier)  // class kotlin.Int

// Check nullability at each level
println(mapType.isMarkedNullable)        // false
println(keyType?.isMarkedNullable)       // false
println(valueType?.isMarkedNullable)     // false

Type Parameter Information

Access information about type parameters including bounds and variance.

/**
 * Represents a type parameter of a class or function
 */
interface KTypeParameter : KClassifier {
  /** Name of the type parameter */
  val name: String
  /** Upper bounds of the type parameter */
  val upperBounds: List<KType>
  /** Variance of the type parameter */
  val variance: KVariance
  /** Whether the type parameter is reified */
  val isReified: Boolean
}

Usage Examples:

// Access type parameters from a class
val listClass = List::class
val typeParams = listClass.typeParameters

if (typeParams.isNotEmpty()) {
    val tParam = typeParams.first()
    println(tParam.name)          // "E"
    println(tParam.variance)      // OUT
    println(tParam.upperBounds)   // [kotlin.Any?]
    println(tParam.isReified)     // false
}

Install with Tessl CLI

npx tessl i tessl/maven-org-jetbrains-kotlin--kotlin-reflect

docs

callable-system.md

class-introspection.md

core-reflection.md

index.md

jvm-interoperability.md

type-system.md

tile.json