19

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

hotkey
  • 140,743
  • 39
  • 371
  • 326
Ragunath Jawahar
  • 19,513
  • 22
  • 110
  • 155

2 Answers2

21

Since 1.1 there is an isData property on the class

MyDataClass::class.isData
Jake Coxon
  • 4,958
  • 1
  • 24
  • 15
6

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.

bashor
  • 8,073
  • 4
  • 34
  • 33