Reflection Bug?

#1

Please have a look at the following program:

open class OpenClass
class FinalClass

class Demo(
    val llo: List<OpenClass>,
    val llf: List<FinalClass>,
    val lmo: MutableList<OpenClass>,
    val lmf: MutableList<FinalClass>,
    var rlo: List<OpenClass>,
    var rlf: List<FinalClass>,
    var rmo: MutableList<OpenClass>,
    var rmf: MutableList<FinalClass>
)

fun main() {
    Demo::class.java.declaredFields.forEach {
        println("${it.name}: ${it.genericType}")
    }
}

It prints:

llo: java.util.List<OpenClass>
llf: java.util.List<FinalClass>
lmo: java.util.List<OpenClass>
lmf: java.util.List<FinalClass>
rlo: java.util.List<? extends OpenClass>
rlf: java.util.List<FinalClass>
rmo: java.util.List<OpenClass>
rmf: java.util.List<FinalClass>

Why has only property ‘rlo’ a WildcardType?

#2

That’s how Kotlin generates generic types for a more convenient usage from Java. It emulates declaration site variance with the use site variance. See more details about it and how to change that behavior here: https://kotlinlang.org/docs/reference/java-to-kotlin-interop.html#variant-generics

#3

Thanks a lot for the quick explanation.
So it’s obviously not a bug but a feature :slight_smile: