Home arrow static arrow Java Programming [Archive] - reflection & primitive arrays
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - reflection & primitive arrays
This topic has 3 replies on 1 page.

Posts:12
Registered: 6/2/04
reflection & primitive arrays  
Aug 5, 2004 2:20 PM



 
hello,
I call some methods with reflection.....As you know method.invoke(....) returns an instance of Object......I saw on javadoc that primitive data types are wrapped into their corrispondent object:es int->Integer......My problem is with primitive data types arrays: if I have int[] as return type and I cast Object to Integer[] I receive CastException...I tried to discover instance type with reflection, but nothing useful ( something as "[[I" )
How to solve it?
Many thanks.
Paolo
 

Posts:6,750
Registered: 1/25/04
Re: reflection & primitive arrays  
Aug 5, 2004 2:40 PM (reply 1 of 3)



 
int[] is a subclass of Object[], so cast to int[].
 

Posts:12
Registered: 6/2/04
Re: reflection & primitive arrays  
Aug 5, 2004 2:57 PM (reply 2 of 3)



 
ok,
seems to work properly......thanks :--)
 

Posts:1,577
Registered: 00-02-25
Re: reflection & primitive arrays  
Aug 6, 2004 6:22 AM (reply 3 of 3)



 
You may find this helpful ... it shows some of the methods that you can use to discover more about the type of an array via reflection.
import java.lang.reflect.Array; public class PrimArrayReflect{    public static void main(String[] argv)    throws Exception    {        int[] a = new int[10];                System.out.println(a.getClass().toString());        System.out.println(a.getClass().isArray());        System.out.println(a.getClass().getComponentType().toString());        System.out.println(a.getClass().getComponentType().equals(Integer.TYPE));                Array.setInt(a, 0, 123);        System.out.println(a[0]);    }}
 
This topic has 3 replies on 1 page.