https://kotlinlang.org logo
#reflect
Title
# reflect
v

vmironov

12/18/2015, 3:54 PM
Copy code
import kotlin.jvm.internal.KotlinClass
import kotlin.reflect.KClass
import kotlin.reflect.jvm.internal.impl.serialization.Flags
import kotlin.reflect.jvm.internal.impl.serialization.jvm.JvmProtoBufUtil

public fun main(args: Array<String>) {
  println("Foo isData = ${Foo::class.isData()}")
  println("Bar isData = ${Bar::class.isData()}")
  println("Magic isData = ${Magic::class.isData()}")
}

public class Magic(val magic: String)

public data class Foo(val foo: String)
public data class Bar(val bar: String)

public fun KClass<*>.isData(): Boolean {
  val data = java.getAnnotation(KotlinClass::class.java)
  val proto = JvmProtoBufUtil.readClassDataFrom(data.data, data.strings)

  return Flags.IS_DATA.get(proto.classProto.flags)
}
3 Views