I cant seem to find a way to detect whenever a `KP...
# announcements
e
I cant seem to find a way to detect whenever a
KProperty1.returnType
is an Array type via reflection
d
It seems like there is a bug with
Array<T>
where
T
is a primitive. For a
val foo: Array<Int>
returnType.classifier
gives
kotlin.IntArray
, but
Array<Int>::class
gives
kotlin.Array
.
I.e., a primitive array is reported by the return type, even though it's an array of boxed ints.
e
uh, isnt there any workaround at the moment to fix that?
that's critical for me
d
Check for both
Array<Int>::class
and
IntArray::class
, that should work.
e
what shall I check against in my
when
?
I need to distinguish everything, when is a primitive or a plain field, when is an array and which kind of array
d
Do you need to distinguish between
Array<Int>
and
IntArray
? If so, you'll probably have to go to Java reflection.
e
no, I dont
but I need to be able to fetch the items to write them to native direct buffers
d
Then just grab the value in the field and do
is
checks on that.
e
ok, but how can I detect in the first place if it is an array?
d
You mean before fetching the value?
e
yes
check the code I posted in #C0922A726
this looks working
returnType.jvmErasure.java.isArray
d
Yes, but you'll then have to keep using java reflection (
java.lang.reflect.Array
) to access it.
Because there is no uniform interface between reference arrays and primitive arrays.
e
I couldnt find anything else at the moment
or?
d
Yeah, I don't think there is, except checking for every type of primitive array manually
e
what shall I pass if I want to know its length?
java.lang.reflect.Array.getLength(..)
?
d
Yes, and you pass in the array.
e
how from
KProperty1
?
I tried
this
and many others, but it always throws exc
d
Copy code
val array = property.get(<instance to get property from>)
val length = java.lang.reflect.Array.getLength(array)
e
I keep getting
Caused by: java.lang.IllegalAccessException: class kotlin.reflect.jvm.internal.FunctionCaller$Method cannot access a member of class vkk.Test with modifiers "public final"
d
Sorry, I have no idea about that. Could be different classloaders? Looks like a very strange error.
e
I'll ask on SO
thanks for your help so far