Pitel
Pitel

Reputation: 5403

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?

Upvotes: 0

Views: 58

Answers (2)

Waseem Abbas
Waseem Abbas

Reputation: 865

fun f(t: java.lang.reflect.Type) {
  if (t is Class<*> && t.isArray && t.getComponentType() == Byte::class.javaPrimitiveType) {
    println("hhh")
  }
}

Upvotes: 0

Pitel
Pitel

Reputation: 5403

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

Upvotes: 2

Related Questions