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.