Is there a way to identify a Kotlin data class from a regular Kotlin class?

前端 未结 2 962
我寻月下人不归
我寻月下人不归 2021-01-17 11:05

Is there a way to identify a Kotlin data class from a regular Kotlin class? Like using reflection maybe?

相关标签:
2条回答
  • Since Kotlin 1.1 use isData property on KClass. (docs)

    Before Kotlin 1.1 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

    Note these implementation details could be changed in the future.

    0 讨论(0)
  • 2021-01-17 11:28

    Since 1.1 there is an isData property on the class

    MyDataClass::class.isData
    
    0 讨论(0)
提交回复
热议问题