• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

argument type for remote method

 
Carlos Llona
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

for instance Can I have this method signature in a component interface for a sateless??

public java.util.List getXXX(String s) {

}

I have a doubt because List doesn't extend Serializable. This method is correct or no corcerning the specification. I assume that there is no problem with that , but I need other opinions

thanks a lot
 
Kathy Sierra
Cowgirl and Author
Rancher
Posts: 1589
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you absolutely can, and you're right to question the issue... while the collections interfaces are *not* themselves Serializable, virtually *all* of the API implementation classes ARE. And the RMI subsystem checks for whether something passed to or from a remote method is serializable at runtime, not compile time. So while List is not Serializable, the ArrayList class *is*. Since the ArrayList is what you're actually passing back at runtime, it won't matter that your declared type of List is not Serializable. All you need to worry about is that all of the things IN the List are also Serializable.

There is one tiny special case that may have even been fixed, but I haven't checked. The implementation class returned from a call to the values() method on Hashtable (and I think HashMap too), used to return a collection of objects that is NOT itself Serializable! This would fail even *if* only Serializable things were in the collection. The values() method, remember, returns a list of the values (rather than a map of the key/value pairs) from the map.

But to answer your question again, you have NOTHING to worry about using List (or any other collection interface) as a return value or argument in a remote method declaration. You must worry ONLY about whether the things you put in the collection are Serializable at runtime.

Cheers,
Kathy
 
Philippe Maquet
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kathy,

Kathy:
There is one tiny special case that may have even been fixed, but I haven't checked. The implementation class returned from a call to the values() method on Hashtable (and I think HashMap too), used to return a collection of objects that is NOT itself Serializable! This would fail even *if* only Serializable things were in the collection. The values() method, remember, returns a list of the values (rather than a map of the key/value pairs) from the map.


I'didn't know it, I just checked and ... you're right (as usual ).
Maps values() is implemented in AbstractMap and its implementation consists of returning an anonymous class extending AbstractCollection which itself is *not* serializable.

Now the issue is easy to solve:

If one needs to write this:



writing this instead:



solves the problem.

Thank you for having pointed that problem out, Kathy!

Best regards,

Phil.

PS: You cannot imagine how much I regret to not be living in the Michigan...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic