• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can i use the return values of finder method directly

 
Narasimha Rao B.
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

A finder method is defined in the remote home interface, which returns remote interface type. After getting the remote interface type, do i need to do narrowing or type casting to use the object or not...

Thanks in Advance,
 
Kathy Sierra
Cowgirl and Author
Rancher
Posts: 1589
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you don't need to do anything if the return type is of the Remote interface type. Just assign it to that Remote interface type in your client. Just think of it as you would any other method... the declared return type is all you need to worry about.

With the Home, of course, the return type of the JNDI lookup method is NOT the Home interface type, so you must cast it. And because it must be IIOP-compliant, you must ALSO do the narrow to *force* it to be the correct object type.

But you must use narrowing ONLY when you are getting something back that is not declared with the exact interface type that you need.

With the EJB home and component interfaces, you'll never have a scenario in which you must do narrowing but not casting--if it's a *stub* coming back from a method, and the declared return type is NOT the Remote interface for that stub, then you must do both a cast and a narrow.

You *could* have a situation, however, where you must do a cast but NOT a narrow--this is true when multiple entity finders. They are coming in as a Collection, but we know that (assuming the bean was built correctly) the Right Thing (i.e. the component interface type) is IN the Collection. So we still have to cast, of course, as we would with *anything* in a Collection that we want to assign to something other than Object, but we do not have to do a narrow... we can simply trust that the right thing was indeed placed into the Collection.

cheers,
Kathy
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic