Reflection: add KTypeProjection and KType.arguments

#KT-8998 In Progress
This commit is contained in:
Alexander Udalov
2016-07-12 16:20:44 +03:00
parent c1dd831e65
commit f69cc01f8e
7 changed files with 246 additions and 5 deletions
@@ -0,0 +1,34 @@
// WITH_REFLECT
// FULL_JDK
import java.lang.reflect.ParameterizedType
import kotlin.reflect.jvm.javaType
import kotlin.test.assertEquals
import kotlin.test.assertTrue
class Bar
fun arrayOfInvBar(): Array<Bar> = null!!
fun arrayOfInBar(): Array<in Bar> = null!!
fun arrayOfOutBar(): Array<out Bar> = null!!
fun arrayOfInvList(): Array<List<String>> = null!!
fun arrayOfInList(): Array<in List<String>> = null!!
fun arrayOfOutList(): Array<out List<String>> = null!!
fun box(): String {
// NB: in "Array<in X>", Java type of X is always Any::class.java because this is the JVM signature generated by the compiler
assertEquals(Bar::class.java, ::arrayOfInvBar.returnType.arguments.single().type!!.javaType)
assertEquals(Any::class.java, ::arrayOfInBar.returnType.arguments.single().type!!.javaType)
assertEquals(Bar::class.java, ::arrayOfOutBar.returnType.arguments.single().type!!.javaType)
val invList = ::arrayOfInvList.returnType.arguments.single().type!!.javaType
assertTrue(invList is ParameterizedType && invList.rawType == List::class.java, invList.toString())
assertEquals(Any::class.java, ::arrayOfInList.returnType.arguments.single().type!!.javaType)
val outList = ::arrayOfOutList.returnType.arguments.single().type!!.javaType
assertTrue(outList is ParameterizedType && outList.rawType == List::class.java, outList.toString())
return "OK"
}
@@ -0,0 +1,22 @@
// WITH_REFLECT
import kotlin.reflect.jvm.javaType
import kotlin.test.assertEquals
fun listOfStrings(): List<String> = null!!
class Foo<T>
class Bar
fun fooOfInvBar(): Foo<Bar> = null!!
fun fooOfInBar(): Foo<in Bar> = null!!
fun fooOfOutBar(): Foo<out Bar> = null!!
fun box(): String {
assertEquals(String::class.java, ::listOfStrings.returnType.arguments.single().type!!.javaType)
assertEquals(Bar::class.java, ::fooOfInvBar.returnType.arguments.single().type!!.javaType)
assertEquals(Bar::class.java, ::fooOfInBar.returnType.arguments.single().type!!.javaType)
assertEquals(Bar::class.java, ::fooOfOutBar.returnType.arguments.single().type!!.javaType)
return "OK"
}
@@ -0,0 +1,19 @@
// WITH_REFLECT
// FILE: J.java
import java.util.List;
public interface J {
List foo();
}
// FILE: K.kt
import kotlin.reflect.jvm.javaType
import kotlin.test.assertEquals
fun box(): String {
assertEquals(Any::class.java, J::foo.returnType.arguments.single().type!!.javaType)
return "OK"
}
@@ -0,0 +1,42 @@
// WITH_REFLECT
import kotlin.reflect.KTypeProjection
import kotlin.test.assertEquals
import kotlin.test.assertTrue
fun string(): String = null!!
class Fourple<A, B, C, D>
fun projections(): Fourple<String, in String, out String, *> = null!!
fun array(): Array<out Number> = null!!
fun list(): List<String> = null!!
fun box(): String {
val string = ::string.returnType
assertEquals(listOf(), string.arguments)
assertEquals(
listOf(
KTypeProjection.Invariant(string),
KTypeProjection.In(string),
KTypeProjection.Out(string),
KTypeProjection.Star
),
::projections.returnType.arguments
)
assertEquals(
listOf(string, string, string, null),
::projections.returnType.arguments.map(KTypeProjection::type)
)
val outNumber = ::array.returnType.arguments.single()
assertTrue(outNumber is KTypeProjection.Out)
assertEquals(Number::class, outNumber.type?.classifier)
// There should be no use-site projection, despite the fact that the corresponding parameter has 'out' variance
assertTrue(::list.returnType.arguments.single() is KTypeProjection.Invariant)
return "OK"
}
@@ -12431,6 +12431,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
doTest(fileName);
}
@TestMetadata("genericArrayElementType.kt")
public void testGenericArrayElementType() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/reflection/mapping/types/genericArrayElementType.kt");
doTest(fileName);
}
@TestMetadata("memberFunctions.kt")
public void testMemberFunctions() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/reflection/mapping/types/memberFunctions.kt");
@@ -12443,6 +12449,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
doTest(fileName);
}
@TestMetadata("parameterizedTypeArgument.kt")
public void testParameterizedTypeArgument() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/reflection/mapping/types/parameterizedTypeArgument.kt");
doTest(fileName);
}
@TestMetadata("parameterizedTypes.kt")
public void testParameterizedTypes() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/reflection/mapping/types/parameterizedTypes.kt");
@@ -12455,6 +12467,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
doTest(fileName);
}
@TestMetadata("rawTypeArgument.kt")
public void testRawTypeArgument() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/reflection/mapping/types/rawTypeArgument.kt");
doTest(fileName);
}
@TestMetadata("topLevelFunctions.kt")
public void testTopLevelFunctions() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/reflection/mapping/types/topLevelFunctions.kt");
@@ -12968,6 +12986,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/reflection/types/platformTypeToString.kt");
doTest(fileName);
}
@TestMetadata("typeArguments.kt")
public void testTypeArguments() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/reflection/types/typeArguments.kt");
doTest(fileName);
}
}
}