Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java reflection; how to use Method.invoke() to retrieve an Object array?

I need some help using reflection in Java. I need to use reflection to call a method that returns an Object array. Method.invoke() only returns an Object. How is this done?

Many thanks for any insights!

like image 200
user1084636 Avatar asked May 12 '26 12:05

user1084636


1 Answers

You just have to cast the return value of Method.invoke() to whatever you happen to know it is. Not very robust, but such are the risks of reflection! So in this case, it would be:

Object[] result = (Object[]) method.invoke(...);

Btw, note that if the method returns a primitive (int, double, etc), Method.invoke will return its boxed equivalent (Integer, Double, etc).

like image 182
yshavit Avatar answered May 15 '26 03:05

yshavit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!