Title: Message
Hi folks,
            Think it's all sorted now. It's not so elegant as I usually don't like testing things by having to catch an exception but basically by just treating the result as a potential array and attempting to get it's length using the reflection API you can catch an IllegalArgumentException if the object returned isn't an array. In that case you just treat it like any other object.
 

nextObj = output.getObjectPart(potentialArrayObject);

int length = 0;

boolean array = true;

try{

        length = Array.getLength(nextObj);

}

catch(IllegalArgumentException iae){

        System.out.println("The output is not an array");

        array=false;

}

if(!array)

{

         //It's just an object

        // not an array

        System.out.println("It's not an array");

        System.out.println(nextObj);

}

else

{

        // It's an array

        System.out.println("It's an array");

        Object nextArrObj=null;

        for(int i =0;i<length;i++)

        {

                nextArrObj = Array.get(nextObj,i);

                System.out.println(nextArrObj.getClass());

                System.out.println(nextArrObj);

        }

}

 

Hope someone gets some use out of this. If there's a more graceful way of determining if an objects an array without catching the exception I'd love if someone could post it.

Thanks,

Mark.

 

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
Sent: 24 June 2003 18:04
To: [EMAIL PROTECTED]
Subject: Re: Array return types

Folks,
            I've done a little tinkering around and it turns out that what actually gets returned is an array of the primitive type.
So I've got an EJB bean that's exposed as a web service and it's returning a Double array yet when the call returns, the
output's ObjectPart corresponding to the right name only casts to the primitive type, (double [ ]).
 
I get a class cast exception thrown if I attempt to cast to the wrapper object (Double []).
This could unfortunately mean a series of instanceof if statements that could potentially be a bit of a nightmare.
 
There's a reflective Array class java.lang.reflect.Array that exists for arrays of objects unfortunately
there doesn't seem to be anything available for simple primitive arrays.
Has anyone any ideas on how to get around this reflectively?
 
Thanks in advance,
Mark.
 

Reply via email to