ClassCastException occurs when casting an element of an Array, or collection of java.langObject passed as a parameter, within an EJB remote method invocation
 Technote (troubleshooting)
 
Problem(Abstract)
When casting an element of an Array of Object within an Enterprise JavaBeans™ method implementation, the cast fails with a ClassCastException. This exception occurs even though the class of the object element is an instance of the cast target type. This problem also occurs when casting elements in collections of Objects, such as Vectors.
 
Cause

The Object Request Broker (ORB) does not translate the classloader for the classes of the object array elements to that of the server-side application module class loader before dispatching the remote invocation to the EJB method implementation. When the cast is performed, it fails with a ClassCastException because the class loader that loaded the cast target class is different than the class loader that loaded the type (class) of the cast source object (for example, the element of the array or collection.)


Problem details
To confirm this problem, modify the EJB method implementation to determine whether the classloader of the cast target class is the same as the classloader of the Object element in the array or collection. Assuming a cast statement like the following:

(TargetClass)sourceObject;

Obtain class loader information for the classes of the cast target class and source object by adding the following statements to the application before the cast statement that causes the exception:

java.lang.ClassLoader ctxCl =
    Thread.currentThread().getContextClassLoader();
java.lang.ClassLoader tcCl =

    TargetClass.class.getClassLoader();
java.lang.ClassLoader soCl =

    sourceObject.getClass().getClassLoader();
System.out.println("ctxCl=" +

    ((ctxCl == null) ? "null" : ctxCl.toString()));
System.out.println("tcCl=" +

    ((tcCl  == null) ? "null" : tcCl.toString()));
System.out.println("soCl=" +

    ((soCl  == null) ? "null" : soCl.toString()));

If tcCl and soCl are different, the following solution will resolve the problem.
 
Resolving the problem
An immediate workaround is to modify the EJB interface method to pass an array or collection of a particular type other than java.lang.Object. The problem has been repaired, however, in JDK™ 1.3.1, SR13 (HP-UX), SR12 (Solaris™), and SR8 (AIX®, Windows®, Linux®, Linux for zSeries®) . For details and downloads, visit the download documents:
 
 
Cross Reference information
Segment Product Component Platform Version Edition
Application Servers Runtimes for Java Technology Java SDK
 
 


Document Information


Product categories: Software > Application Servers > Distributed Application & Web Servers > WebSphere Application Server > Java SDK
Operating system(s): Windows
Software version: 5.0.2.16
Software edition:
Reference #: 1188873
IBM Group: Software Group
Modified date: Jan 19, 2005