I came up with a strange behaviour with Kotlin reified. Following is an example test class.
data class User(var name: String = "name", var age: Int = 0)
private val objectMapper = ObjectMapper()
private val userArray: String = """[{"name":"Alice","age":1},{"name":"Bob","age":2}]"""
private val user: String = """{"name":"Alice","age":1}"""
private inline fun <reified V> getUserObjects(jsonString: String): List<V> = objectMapper
.readValue(jsonString, Array<V>::class.java).toList()
private inline fun <reified V> getUserObject(jsonString: String): V = objectMapper
.readValue(jsonString, V::class.java)
@Test
fun single_object_success() {
val actual: User = getUserObject(user)
Assertions.assertEquals("Alice", (actual as User).name)
}
@Test
fun object_list_error() {
val actual: List<User> = getUserObjects(userArray)
Assertions.assertEquals("Alice", (actual.first() as User).name)
// java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class MockTests$User
}
While the first test case passes as expected the second test case fails with the above mentioned exception. By looking at the java decompiled code (below) it seems reified is incapable of populating the type in the first inline function (getUserObjects).
@Test
public final void single_object_success() {
String jsonString$iv = this.user;
int $i$f$getUserObject = false;
User actual = (User)access$getObjectMapper$p(this).readValue(jsonString$iv, User.class);
if (actual == null) {
throw new NullPointerException("null cannot be cast to non-null type MockTests.User");
} else {
Assertions.assertEquals("Alice", actual.getName());
}
}
@Test
public final void object_list_error() {
String jsonString$iv = this.userArray;
int $i$f$getUserObjects = false;
Object var10000 = access$getObjectMapper$p(this).readValue(jsonString$iv, Object[].class);
Intrinsics.checkNotNullExpressionValue(var10000, "objectMapper\n .re…ng, Array<V>::class.java)");
List actual = ArraysKt.toList((Object[])var10000);
Object var10001 = CollectionsKt.first(actual);
if (var10001 == null) {
throw new NullPointerException("null cannot be cast to non-null type MockTests.User");
} else {
Assertions.assertEquals("Alice", ((User)var10001).getName());
}
}
Is this the default behaviour with Kotlin reified? If so can someone explain the rationale behind this?
Because myself and IntelliJ believe the Object array should be an User array.

Thanks in advance.