Is there a way to identify a Kotlin data class fro

2019-06-16 08:40发布

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

2条回答
孤傲高冷的网名
2楼-- · 2019-06-16 09:18

Since 1.1 there is an isData property on the class

MyDataClass::class.isData
查看更多
冷血范
3楼-- · 2019-06-16 09:24

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.

查看更多
登录 后发表回答