Is there a way to identify a Kotlin data class fro

2019-06-16 09:00发布

问题:

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.