Reflection API Method.invoke() return values

Status
Not open for further replies.

gumbo

Ars Tribunus Angusticlavius
6,107
From the 1.3 API docs:

If the method completes normally, the value it returns is returned to the caller of invoke; if the value has a primitive type, it is first appropriately wrapped in an object. If the underlying method return type is void, the invocation returns null.

My method int[] getActiveMotors() obviously returns int[]. According to the documentation, this should be wrapped in it's appropriate class, then returned. Okay, so I can cast the returned Object to Integer[], right?
No.

The returned Object is in fact [I, by doing getClass{}.getName(). What class is [I exactly? It will not cast to anything useful, and appears to contain no useful data. Yes, my method returns valid data - it is hard coded into that method.

Any ideas on how to cast it correctly? It was working before, but adding a reference to the calling class caused the whole project to explode.

Edit: I cannot cast to anything - it appears the Object returned is invalid, but non-null.

gubmo

[This message was edited by gubmo on April 20, 2002 at 22:43.]
 

wb

Ars Legatus Legionis
25,208
Subscriptor
The returned Object is in fact [I, by doing getClass{}.getName(). What class is [I exactly? It will not cast to anything useful, and appears to contain no useful data. Yes, my method returns valid data - it is hard coded into that method.
IIRC [I is an int[]. Arrays in java behave like pseudo-objects as near as I can tell. E.g. they all have a length property, and pass as references into methods.
 

gumbo

Ars Tribunus Angusticlavius
6,107
Heh, it will cast to something - an int[] like you said. :)

Thank you so much.

/me goes to look for primitive object class names

Edit: From the Java Language Specification, Second Edition:
Every array also has a class; the method getClass, when invoked for an array object, will return a class object (of class Class) that represents the class of the array.
The classes for arrays have strange names that are not valid identifiers; for example, the class for an array of int components has the name "[I" and so the value of the expression:

new int[10].getClass().getName()

is the string "[I"; see the specification of Class.getName for details.

gubmo

[This message was edited by gubmo on April 21, 2002 at 08:55.]
 
Status
Not open for further replies.