river-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Peter Firmstone <j...@zeus.net.au>
Subject Service's and Generics
Date Sat, 10 Jul 2010 04:20:01 GMT
Just thought I'd start the discussion, seeing as the next release will 
be Java 5 based.

Generics are subject to erasure, the compiler replaces them with type 
casts and the type at the top of a hierarchy.


For example a method

public List<Apple> getApples();

Erases to:

public List getApples();

All operations on the returned lists will have type casts, casting 
Object's from the List to Apple's.

This works well when the same compiler compiles all the code.

However it might not work when components are compiled separately.

One might argue that you could use an annotation or discover the type 
from the bytecode, however the serialized form of MarshalledObject is 
used to identify lookup services, bytecode is not involved.

If you use generic interfaces in your services, be careful, your clients 
could use the type that would remain after erasure, then you must use 
instanceof and typecast.  But then in your service itself, it will still 
use the generic unchecked type casts, since it was compiled separately, 
so it'll break at some point.

However there is an occasion where you may use Generics in a Service 
Interface.  It's in the case where your type is always going to be the 
same, as per the List<Apple> above, if Apple is used by all at compile 
time, then the type is Guaranteed to be the same across all implementations.

So you can't use List<T> type generics, but you can be specific, with 
List<Apple>

Cheers,

Peter.

Mime
View raw message