Reflection: add KClassifier.createType

#KT-8998 In Progress
This commit is contained in:
Alexander Udalov
2016-07-12 16:38:45 +03:00
parent 153e837a84
commit 1edf3c7809
8 changed files with 301 additions and 0 deletions
@@ -0,0 +1,16 @@
// WITH_REFLECT
import kotlin.test.assertEquals
class A<T1> {
inner class B<T2, T3> {
inner class C<T4>
}
}
fun foo(): A<Int>.B<Double, Float>.C<Long> = null!!
fun box(): String {
assertEquals("A<kotlin.Int>.B<kotlin.Double, kotlin.Float>.C<kotlin.Long>", ::foo.returnType.toString())
return "OK"
}
@@ -0,0 +1,40 @@
// WITH_REFLECT
import kotlin.reflect.createType
import kotlin.reflect.KTypeProjection
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals
class Foo<T>
fun box(): String {
assertEquals(String::class.createType(), String::class.createType())
assertEquals(
Foo::class.createType(listOf(KTypeProjection.Star)),
Foo::class.createType(listOf(KTypeProjection.Star))
)
val i = Int::class.createType()
assertEquals(
Foo::class.createType(listOf(KTypeProjection.Invariant(i))),
Foo::class.createType(listOf(KTypeProjection.Invariant(i)))
)
assertNotEquals(
Foo::class.createType(listOf(KTypeProjection.In(i))),
Foo::class.createType(listOf(KTypeProjection.Out(i)))
)
assertNotEquals(
Foo::class.createType(listOf(KTypeProjection.Out(Any::class.createType(nullable = true)))),
Foo::class.createType(listOf(KTypeProjection.Star))
)
assertNotEquals(
Foo::class.createType(listOf(KTypeProjection.Star), nullable = false),
Foo::class.createType(listOf(KTypeProjection.Star), nullable = true)
)
return "OK"
}
@@ -0,0 +1,26 @@
// WITH_REFLECT
import kotlin.reflect.createType
import kotlin.reflect.KClass
import kotlin.reflect.KTypeProjection
import kotlin.test.assertEquals
class A<T1> {
inner class B<T2, T3> {
inner class C<T4>
}
class D
}
fun foo(): A<Int>.B<Double, Float>.C<Long> = null!!
fun box(): String {
fun KClass<*>.inv() = KTypeProjection.Invariant(this.createType())
val type = A.B.C::class.createType(listOf(Long::class.inv(), Double::class.inv(), Float::class.inv(), Int::class.inv()))
assertEquals("A<kotlin.Int>.B<kotlin.Double, kotlin.Float>.C<kotlin.Long>", type.toString())
assertEquals("A.D", A.D::class.createType().toString())
return "OK"
}
@@ -0,0 +1,18 @@
// WITH_REFLECT
import kotlin.reflect.createType
import kotlin.reflect.KTypeProjection
import kotlin.test.assertEquals
class Foo
class Bar<T>
fun box(): String {
assertEquals("Foo", Foo::class.createType().toString())
assertEquals("Foo?", Foo::class.createType(nullable = true).toString())
assertEquals("Bar<kotlin.String>", Bar::class.createType(listOf(KTypeProjection.Invariant(String::class.createType()))).toString())
assertEquals("Bar<kotlin.Int>?", Bar::class.createType(listOf(KTypeProjection.Invariant(Int::class.createType())), nullable = true).toString())
return "OK"
}
@@ -0,0 +1,29 @@
// WITH_REFLECT
import kotlin.reflect.createType
import kotlin.test.assertEquals
class Foo<T> {
fun nonNull(): T = null!!
fun nullable(): T? = null
}
fun box(): String {
val tp = Foo::class.typeParameters.single()
assertEquals(
Foo::class.members.single { it.name == "nonNull" }.returnType,
tp.createType()
)
assertEquals(
Foo::class.members.single { it.name == "nullable" }.returnType,
tp.createType(nullable = true)
)
assertEquals(tp.createType(), tp.createType())
assertEquals(tp.createType(nullable = true), tp.createType(nullable = true))
assertEquals("T", tp.createType().toString())
assertEquals("T?", tp.createType(nullable = true).toString())
return "OK"
}
@@ -0,0 +1,49 @@
// WITH_REFLECT
import kotlin.reflect.createType
import kotlin.reflect.KClassifier
import kotlin.reflect.KTypeProjection
fun test(classifier: KClassifier, arguments: List<KTypeProjection>) {
try {
classifier.createType(arguments)
throw AssertionError("createType should have thrown IllegalArgumentException")
}
catch (e: IllegalArgumentException) {
// OK
}
}
class Outer<O> {
inner class Inner<I>
class Nested<N>
}
fun box(): String {
val p = KTypeProjection.Star
test(String::class, listOf(p))
test(String::class, listOf(p, p))
test(List::class, listOf())
test(List::class, listOf(p, p))
test(Map::class, listOf())
test(Map::class, listOf(p))
test(Map::class, listOf(p, p, p))
test(Array<Any>::class, listOf())
test(Outer::class, listOf())
test(Outer::class, listOf(p, p))
// Outer.Inner takes two arguments: first for O, second for I
test(Outer.Inner::class, listOf())
test(Outer.Inner::class, listOf(p))
test(Outer.Inner::class, listOf(p, p, p))
// Outer.Nested takes one argument for N
test(Outer.Nested::class, listOf())
test(Outer.Nested::class, listOf(p, p))
test(Outer::class.typeParameters.single(), listOf(p))
return "OK"
}