Another case of Casting Exceptions in EJB
843829Nov 7 2001 — edited Mar 11 2002Hello,
I have another case of casting problems in an EJB (in addition to the problem in the previous topic).
Consider two classes available to both client and EJB (EJB is a Session Bean).
class A implements java.io.Serializable {};
class B extends A {};
Part 1.
Consider a method of the EJB that accepts as a formal parameter an object of type A.
EJB.searchMethod( A formalParam );
The client calls EJB.searchMethod() (through the remote interface) and
passes as an actual parameter an instance of class B. Should be legal, right?
No problem yet.
Now, in the Bean implementation, I would like to determine whether the client gave
me an instanceof B instead of an instanceof A. Should be able to do this right?
No such luck! instanceof always returns false! And though the client really does
call the method with an instance of B, attempting to cast the actual parameter to
an instanceof B results in a ClassCastException.
Part 2.
When the EJB is created, it loads into a private memory location a list of classes; some
of which are A and some of which are B. These instances are created by code running
internally in the Bean -- they are not passed to the Bean from somewhere else.
Now in response to the searchMethod() call, I would like to run down the list treating
all members of the list as objects of type A, and find those that are actually instanceof
B. Same problem -- ( obj instanceof B ) always returns false whether obj actually
is a B or not.
If I run the code outside of the EJB container, everything works fine. Running the code
in the EJB fails. Whassupwitdat?