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:
Alexander Udalov
2021-07-08 22:34:43 +02:00
parent ddfa94e7e9
commit a7e48c3af1
10 changed files with 157 additions and 12 deletions
@@ -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