Skip to content

KaTypeProvider

@SubclassOptInRequired(markerClass = [KaSessionComponentImplementationDetail::class])
interface KaTypeProvider : KaSessionComponent

Inheritors

Properties

allSupertypes

abstract fun KaType.allSupertypes(shouldApproximate: Boolean): Sequence<KaType>

Returns all supertypes of the given KaType. The resulting sequence is ordered by a breadth-first traversal of the class hierarchy, without duplicates.

Parameters

All supertypes of the given KaType. The resulting sequence is ordered by a breadth-first traversal of the class hierarchy, without duplicates.

Denotable types are not approximated.

arrayElementType

abstract val KaType.arrayElementType: KaType?

The array type's element type if the given KaType is a primitive type array or Array, and null otherwise.

augmentedByWarningLevelAnnotations

A KaType derived from the given type by enforcing warning-level nullability annotations. If the derived type doesn't differ from the original type, the original type is used.

In general, Java type enhancement allows the Kotlin compiler to infer a more specific nullability for a Java type based on its nullability annotations. Normally, only strict nullability annotations have an impact on a resolved type's nullability. These annotations are already taken into account in KaType.

However, there are also warning-level nullability annotations, such as Android's RecentlyNullable and RecentlyNonNull. These annotations have weaker constraints and don't affect a resolved type's nullability. augmentedByWarningLevelAnnotations is a KaType with weak annotations treated as strict ones.

See the list of default report levels for different nullability annotations in NULLABILITY_ANNOTATION_SETTINGS

Examples

builtinTypes

KaType instances for built-in types.

commonSupertype

The common supertype of the given KaTypes.

Throws

IllegalArgumentException

If the collection of types is empty.

The common supertype of the given KaTypes.

Throws

IllegalArgumentException

If the array of types is empty.

defaultType

The representation of this in terms of KaType.

Type parameters are substituted with matching type parameter types, e.g. List<T> for the List class.

See also

defaultTypeWithStarProjections

The representation of this in terms of KaType.

Type parameters are substituted with KaStarTypeProjection, e.g. List<*> for the List class.

See also

directSupertypes

abstract fun KaType.directSupertypes(shouldApproximate: Boolean): Sequence<KaType>

Returns the direct supertypes of the given KaType.

For flexible types, direct supertypes of both the upper and lower bounds are returned. If that's not desirable, use directSupertypes on KaFlexibleType.upperBound or KaFlexibleType.lowerBound.

Example

Given MutableList<String>, directSupertypes returns List<String> and MutableCollection<String>

Parameters

  • shouldApproximate: Whether to approximate non-denotable types. For example, the supertype of List<out String> is Collection<CAPTURED out String>. With approximation set to true, Collection<out String> is returned instead.

The direct supertypes of the given KaType.

For flexible types, direct supertypes of both the upper and lower bounds are included. If that's not desirable, use directSupertypes on KaFlexibleType.upperBound or KaFlexibleType.lowerBound.

Denotable types are not approximated.

Example

Given MutableList<String>, directSupertypes contains List<String> and MutableCollection<String>

receiverType

Resolves the given KtDoubleColonExpression to the KaType of its receiver.

The result may be null if the resolution fails or the resolved callable reference is not a reflection type.

Example

class Foo {
    fun bar() { }
}

val foo = Foo()
foo::bar

Here, receiverType for foo::bar is Foo (the type of foo).

token

abstract val token: KaLifetimeToken

The KaLifetimeToken which determines the lifetime of the lifetime owner.

type

abstract val KtTypeReference.type: KaType

Resolves the given KtTypeReference to its corresponding KaType.

This may raise an exception if the resolution ends up with an unexpected result.

varargArrayType

The array type that represents the list of arguments passed to this parameter if this is a vararg parameter.

If this is not a vararg parameter, varargArrayType is null. If this is an invalid (e.g., in case of multiple vararg parameters) or useless (in anonymous functions) vararg parameter, varargArrayType still contains a type for it.

Functions

approximateToDenotableSubtype

Approximates KaType to a denotable subtype.

The function returns null if the type is already denotable and does not need approximation. Otherwise, for a type T, returns a denotable type S such that S <: T, with all type arguments of S also being denotable.

approximateToDenotableSubtypeOrSelf

Approximates KaType to a denotable subtype, or returns the given type itself if it is already denotable.

See also

approximateToDenotableSupertype

abstract fun KaType.approximateToDenotableSupertype(allowLocalDenotableTypes: Boolean): KaType?

Approximates KaType to a denotable supertype.

The function returns null if the type is already denotable and does not need approximation. Otherwise, for a type T, returns a denotable type S such that T <: S, with all type arguments of S also being denotable.

Parameters

  • allowLocalDenotableTypes: Whether locally declared types should be approximated to local supertypes instead of non-local ones. Local type approximation is sensible when the resulting KaType is analyzed in the same local context.

Approximates KaType to a denotable supertype based on the given position.

This position is used when approximating local types. If the given type is local, then the function returns the first supertype, which is visible from the given position. Note that position is required to be within KaAnalysisScopeProvider.analysisScope, otherwise, an exception is thrown.

The function returns null if the type is already denotable and does not need approximation. Otherwise, for a type T, returns a denotable type S such that T <: S, with all type arguments of S also being denotable.

Example:

<position_1>
fun foo() {
    open class <position_2> A

    fun bar() = <expr>object: A() {}</expr>
}

In the example above we are trying to approximate the type of object: A() {} expression, which is a local type <anonymous>: A. When this type is approximated using <position_2> the function returns A, as this type is visible from this position. However, when approximating from <position_1>, the function returns Any, as A is not visible from this position, so the only option left is Any.

approximateToDenotableSupertypeOrSelf

open fun KaType.approximateToDenotableSupertypeOrSelf(allowLocalDenotableTypes: Boolean): KaType

Approximates KaType to a denotable supertype, or returns the given type itself if it is already denotable.

See also

Approximates KaType to a denotable subtype based on the given position, or returns the given type itself if it is already denotable.

See also

assertIsValidAndAccessible

hasCommonSubtypeWith

abstract fun KaType.hasCommonSubtypeWith(that: KaType): Boolean

Checks whether this KaType is compatible with that other type. If they are compatible, the types can have a common subtype.

isValid

Whether the lifetime owner is still valid, i.e. we are still in the scope of the lifetime owner's regular lifetime.

lowerBoundIfFlexible

Returns the KaFlexibleType's lower bound, or the type itself if it is not flexible.

upperBoundIfFlexible

Returns the KaFlexibleType's upper bound, or the type itself if it is not flexible.

withNullability

abstract fun KaType.withNullability(isMarkedNullable: Boolean): KaType

Creates a new KaType based on the given type with the updated nullability specified by isMarkedNullable.

withValidityAssertion

inline fun <R> KaLifetimeOwner.withValidityAssertion(action: () -> R): R

Executes action only if the KaLifetimeOwner is still valid and accessible. Otherwise, throws a validity exception based on the concrete violation.