问题
Is there a way to identify a Kotlin data class from a regular Kotlin class? Like using reflection maybe?
回答1:
Since 1.1 there is an isData property on the class
MyDataClass::class.isData
回答2:
You can't read data
annotation by reflection because it has default retention(CLASS
).
You can try to use some heuristics, like check that it contains next methods:
public final copy
public final component{N}
public static copy$default
But note that somethings of this are implementation details and may be changed in the future.
来源:https://stackoverflow.com/questions/29069988/is-there-a-way-to-identify-a-kotlin-data-class-from-a-regular-kotlin-class