Ok, I think I've solved it. Here is your original sample code snippet:

Vector myVec ;
MyCustomObject mco = new MyCustomObject() ;
myVec.add(mco) ;
Object[] o = myVec.toArray() ;
return (MyCustomObject)o ;

This looks to me like the same problem that went by on this list a few weeks
ago. It has nothing to do with being in a JSP. The problem is that you'll
get a class cast exception on that return statement. Objects in Java "know"
what they are, and you can't cast something to a type that it isn't. Arrays
in Java are really just objects, whose type is array-of-<whatever>. the
toArray method returns an object whose type is array-of-Objects. Even though
each element of that array is, in fact, of type MyCustomObject, it does NOT
follow that the array is automatically of type array-of-MyCustomObjects. An
array is of whatever type it was created as, not array-of-<type of elements
in the array>. So you can't cast an array whose type is array-of-Objects to
the type array-of-MyCustomObjects, because the array just isn't of that
type.

This is the whole reason why there's a toArray method in the Vector class
that takes an array as a parameter. That parameter is defined as an
array-of-Objects, which means that you can actually pass in an array of any
type (array-of-Objects is the superclass of every array type, just like
Object is the superclass of every class; remember you can always cast up the
superclass hierarachy, but above you're trying to cast down). Since the
passed-in parameter "knows" what its actually type is, the method can create
an array of THAT type rather than of type array-of-Objects. This array then,
even though it's returned (as above) as a generic array-of-Objects will, in
fact, be an array-of-MyCustomObjects, and the cast will therefore work. So,
change the last two lines to the following, and it should work:

MyCustomObject[] dummy = new MyCustomObject[0];
MyCustomObject[] returnValue = (MyCustomObject[])myVec.toArray(dummy);
return returnValue;

--Jim Preston

-----Original Message-----
From: A mailing list about Java Server Pages specification and reference
[mailto:[EMAIL PROTECTED]]On Behalf Of George Ludwig
Sent: Thursday, June 29, 2000 4:46 PM
To: [EMAIL PROTECTED]
Subject: Re: bug with array casting within a JSP process


Shrisha,

Shrisha Radhakrishna wrote:

> Here is the answer!  You are trying to instantiate a custom class that is
> not inside any package from within a .java that is inside a package.  When
> your JSP is translated into a java file, the compiler automatically puts
it
> in a package.  (in weblogic, it is jsp_package;).

Thanks for taking the time to look at the example code. However, once again
I have
to say that your comment on the sample code is not valid for the actual code
that
I'm executing. The custom object is in fact in a pacakge. My sample code was
only
there to show conceptually what was going on, rather than to be analyzed for
coding
errors.

There are no code errors in the actual code.

In the code as it is working now, within the jsp page I invoke the method
that
returns a vanilla Object array. As I access each element of the array I have
to
typecast it to the class of my custom object. That works with no problem.
The jsp
engine understand my custom object perfectly well. It just won't let me do
class
casting of arrays.

If anyone is doing class casting of entire arrays within a jsp execution
context, I
would really like to hear about it.

Cheers,

George

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to