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

故事扮演 提交于 2019-12-08 14:55:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!