Reflection: add KTypeParameter.toString

The test covers different type parameters with and without bounds, in case we
decide to also output upper bounds in toString
This commit is contained in:
Alexander Udalov
2016-07-22 16:40:27 +03:00
parent 170ea4dead
commit e760b5ed53
4 changed files with 38 additions and 4 deletions
@@ -0,0 +1,15 @@
// WITH_REFLECT
import kotlin.test.assertEquals
interface Variance<A, in B, out C, D>
class OneBound<T : Enum<T>>
class SeveralBounds<T : Cloneable> where T : Enum<T>, T : Variance<String, Int?, Double?, Number>
fun box(): String {
assertEquals("[A, in B, out C, D]", Variance::class.typeParameters.toString())
assertEquals("[T]", OneBound::class.typeParameters.toString())
assertEquals("[T]", SeveralBounds::class.typeParameters.toString())
return "OK"
}
@@ -12573,6 +12573,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
doTest(fileName);
}
@TestMetadata("typeParametersToString.kt")
public void testTypeParametersToString() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/reflection/methodsFromAny/typeParametersToString.kt");
doTest(fileName);
}
@TestMetadata("typeToString.kt")
public void testTypeToString() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/reflection/methodsFromAny/typeToString.kt");
@@ -45,4 +45,7 @@ internal class KTypeParameterImpl(override val descriptor: TypeParameterDescript
override fun hashCode() =
descriptor.hashCode()
override fun toString() =
ReflectionObjectRenderer.renderTypeParameter(descriptor)
}
@@ -16,12 +16,10 @@
package kotlin.reflect.jvm.internal
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
import org.jetbrains.kotlin.descriptors.ReceiverParameterDescriptor
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.renderer.DescriptorRenderer
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.Variance
import kotlin.reflect.KParameter
internal object ReflectionObjectRenderer {
@@ -107,6 +105,18 @@ internal object ReflectionObjectRenderer {
}
}
fun renderTypeParameter(typeParameter: TypeParameterDescriptor): String {
return buildString {
when (typeParameter.variance) {
Variance.INVARIANT -> {}
Variance.IN_VARIANCE -> append("in ")
Variance.OUT_VARIANCE -> append("out ")
}
append(typeParameter.name)
}
}
fun renderType(type: KotlinType): String {
return renderer.renderType(type)
}