kotlin.jvm.internal.TypeReference.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-stdlib Show documentation
Show all versions of kotlin-stdlib Show documentation
Kotlin Standard Library for JVM
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package kotlin.jvm.internal
import kotlin.reflect.*
@SinceKotlin("1.4")
public class TypeReference(
override val classifier: KClassifier,
override val arguments: List,
override val isMarkedNullable: Boolean
) : KType {
override val annotations: List
get() = emptyList()
override fun equals(other: Any?): Boolean =
other is TypeReference &&
classifier == other.classifier && arguments == other.arguments && isMarkedNullable == other.isMarkedNullable
override fun hashCode(): Int =
(classifier.hashCode() * 31 + arguments.hashCode()) * 31 + isMarkedNullable.hashCode()
override fun toString(): String =
asString() + Reflection.REFLECTION_NOT_AVAILABLE
private fun asString(): String {
val javaClass = (classifier as? KClass<*>)?.java
val klass = when {
javaClass == null -> classifier.toString()
javaClass.isArray -> javaClass.arrayClassName
else -> javaClass.name
}
val args =
if (arguments.isEmpty()) ""
else arguments.joinToString(", ", "<", ">") { it.asString() }
val nullable = if (isMarkedNullable) "?" else ""
return klass + args + nullable
}
private val Class<*>.arrayClassName
get() = when (this) {
BooleanArray::class.java -> "kotlin.BooleanArray"
CharArray::class.java -> "kotlin.CharArray"
ByteArray::class.java -> "kotlin.ByteArray"
ShortArray::class.java -> "kotlin.ShortArray"
IntArray::class.java -> "kotlin.IntArray"
FloatArray::class.java -> "kotlin.FloatArray"
LongArray::class.java -> "kotlin.LongArray"
DoubleArray::class.java -> "kotlin.DoubleArray"
else -> "kotlin.Array"
}
// This is based on KTypeProjection.toString, but uses [asString] to avoid adding the
// "reflection not supported" suffix to each type argument.
private fun KTypeProjection.asString(): String {
if (variance == null) return "*"
val typeString = (type as? TypeReference)?.asString() ?: type.toString()
return when (variance) {
KVariance.INVARIANT -> typeString
KVariance.IN -> "in $typeString"
KVariance.OUT -> "out $typeString"
}
}
}