https://kotlinlang.org logo
Title
e

elect

05/17/2018, 8:24 AM
I cant seem to find a way to detect whenever a
KProperty1.returnType
is an Array type via reflection
d

diesieben07

05/17/2018, 8:43 AM
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

elect

05/17/2018, 8:48 AM
uh, isnt there any workaround at the moment to fix that?
that's critical for me
d

diesieben07

05/17/2018, 8:50 AM
Check for both
Array<Int>::class
and
IntArray::class
, that should work.
e

elect

05/17/2018, 8:52 AM
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

diesieben07

05/17/2018, 8:53 AM
Do you need to distinguish between
Array<Int>
and
IntArray
? If so, you'll probably have to go to Java reflection.
e

elect

05/17/2018, 8:53 AM
no, I dont
but I need to be able to fetch the items to write them to native direct buffers
d

diesieben07

05/17/2018, 8:54 AM
Then just grab the value in the field and do
is
checks on that.
e

elect

05/17/2018, 8:56 AM
ok, but how can I detect in the first place if it is an array?
d

diesieben07

05/17/2018, 8:57 AM
You mean before fetching the value?
e

elect

05/17/2018, 8:57 AM
yes
check the code I posted in #general
this looks working
returnType.jvmErasure.java.isArray
d

diesieben07

05/17/2018, 9:00 AM
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

elect

05/17/2018, 9:01 AM
I couldnt find anything else at the moment
or?
d

diesieben07

05/17/2018, 9:02 AM
Yeah, I don't think there is, except checking for every type of primitive array manually
e

elect

05/17/2018, 9:22 AM
what shall I pass if I want to know its length?
java.lang.reflect.Array.getLength(..)
?
d

diesieben07

05/17/2018, 9:22 AM
Yes, and you pass in the array.
e

elect

05/17/2018, 9:22 AM
how from
KProperty1
?
I tried
this
and many others, but it always throws exc
d

diesieben07

05/17/2018, 9:24 AM
val array = property.get(<instance to get property from>)
val length = java.lang.reflect.Array.getLength(array)
e

elect

05/17/2018, 9:41 AM
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

diesieben07

05/17/2018, 9:43 AM
Sorry, I have no idea about that. Could be different classloaders? Looks like a very strange error.
e

elect

05/17/2018, 10:50 AM
I'll ask on SO
thanks for your help so far