bpereira
bpereira

Reputation: 1016

Kotlin: Check if a KTypeProperty1 is a data class

Using reflection I can iterate through my attributes:

data class AnotherDataClass(
    val property: String,
    val dataClass: DataClass
)


AnotherDataClass::class.memberProperties.filter {
    return it::class.isData
}

But it::class.isData is always false since the type of it is jvm.internal.KProperty1Impl. Is there a way to check if this class is a data class?

Upvotes: 0

Views: 1682

Answers (1)

ardenit
ardenit

Reputation: 3890

Use

AnotherDataClass::class.memberProperties.filter {
    (it.returnType.classifier as? KClass<*>)?.isData ?: false
}

Upvotes: 2

Related Questions