kotlinkotlin-interop

Primitive array Type in Kotlin


I have a Java library with a function like this:

fun f(t: java.lang.reflect.Type)

Now, I want to check, if the type is an array of bytes. So I'd do something like this:

override fun f(t: Type) {
  if (t == ByteArray::class) doSomething()
}

But the condition fails!

If I try to println(t) I get byte[]. But println(ByteArray::class) is class [B. (And it's the same if I use ::class.java.)

So, how can I check for these primitive arrays in Kotlin?


Solution

  • if ((t as? GenericArrayType)?.genericComponentType == Byte::class.java)