I am doing some reflection work and go to a little problem.
I am trying to print objects to some GUI tree and have problem detecting arrays in a generic way.
I suggested that :
object instanceof Iterable
Would make the job ,but it does not, (obviously applies only to Lists and Set and whoever implements it.)
So how is that i would recognice an Array Some Object[]
, Or long[]
or Long[]
.. ?
Thanks
Do you mean
Object.getClass().isArray()
?You can do
First of all, @Bozho's answer is perfectly correct.
If you want to make this to be easier useable, I've just created a method in our little OSS utility molindo-utils that turns an array of unknown type into an Iterable: ArrayUtils.toIterable(Object)
This way, you can do:
See README of molindo-utils on how to get molindo-utils or feel free to copy the code if you like, just as you see fit.
If you don't want only to check whether the object is an array, but also to iterate it:
(the class above is
java.lang.reflect.Array
)