Improve toString of platform types created by typeOf
In the stdlib implementation, render "!" if the type is only nullability-flexible. Otherwise, render "($lower..$upper)". Note that full kotlin-reflect has a much more complicated logic (see `DescriptorRendererImpl.renderFlexibleType`) that renders things like `(Mutable)List` and so on. It is not a goal of the stdlib implementation to replicate all of that, since it requires copying a large amount of code, namely the entirety of `JavaToKotlinClassMap` to map Java class names to Kotlin.
This commit is contained in:
@@ -53,7 +53,22 @@ public class TypeReference /* @SinceKotlin("1.6") constructor */(
|
||||
else arguments.joinToString(", ", "<", ">") { it.asString() }
|
||||
val nullable = if (isMarkedNullable) "?" else ""
|
||||
|
||||
return klass + args + nullable
|
||||
val result = klass + args + nullable
|
||||
|
||||
return when (val upper = platformTypeUpperBound) {
|
||||
is TypeReference -> {
|
||||
when (val renderedUpper = upper.asString(true)) {
|
||||
result -> {
|
||||
// Rendered upper bound can be the same as rendered lower bound in cases when the type is mutability-flexible,
|
||||
// but not nullability-flexible, since both MutableCollection and Collection are rendered as "java.util.Collection".
|
||||
result
|
||||
}
|
||||
"$result?" -> "$result!"
|
||||
else -> "($result..$renderedUpper)"
|
||||
}
|
||||
}
|
||||
else -> result
|
||||
}
|
||||
}
|
||||
|
||||
private val Class<*>.arrayClassName
|
||||
|
||||
Reference in New Issue
Block a user