You are currently viewing A strange CompilingError of kotlin.reflect, why and how?

A strange CompilingError of kotlin.reflect, why and how?

import org.testng.annotations.Test import kotlin.reflect.cast import kotlin.reflect.full.memberProperties import kotlin.reflect.jvm.isAccessible import kotlin.test.assertEquals import decompose.mcve.Decompose // package decompose.mcve // // class Decompose(n: Int) { // private val decomposed: Decomposed = IntValue(n) // } // // private sealed class Decomposed // // private class IntValue(val n: Int) : Decomposed() class TestDecompose { @Test fun testReflect() { val expectedInt = 42 val decompose = Decompose(expectedInt) val decomposedProp = Decompose::class.memberProperties.first { it.name == "decomposed" } decomposedProp.isAccessible = true val d = decomposedProp(decompose) val intValueClass = Class.forName("decompose.mcve.IntValue").kotlin assert(intValueClass.isInstance(d)) val intValue = intValueClass.cast(d) val nProp2 = intValue.javaClass.kotlin.memberProperties.first { it.name == "n" } nProp2.isAccessible = true val nProp = intValueClass.memberProperties.first { it.name == "n" } nProp.isAccessible = true assertEquals(intValueClass, intValue.javaClass.kotlin) assertEquals(nProp2, nProp) assertEquals(expectedInt, nProp2(intValue)) /// CompilingError: Type mismatch: inferred type is Any! but Nothing was expected // assertEquals(expectedInt, nProp(intValue)) } } 

submitted by /u/jhk9x
[link] [comments]