Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Earlier, we showed how new and legacy code can interoperate. Now, it's time to look at the harder problem of "generifying" old code.If you decide to convert old code to use generics, you need to think carefully about how you modify the API.
You need to make certain that the generic API is not unduly restrictive; it must continue to support the original contract of the API. Consider again some examples from
java.util.Collection
. The pre-generic API looks like:A naive attempt to generify it would be the following:interface Collection { public boolean containsAll(Collection c); public boolean addAll(Collection c); }While this is certainly type safe, it doesn't live up to the API's original contract. Theinterface Collection<E> { public boolean containsAll(Collection<E> c); public boolean addAll(Collection<E> c); }containsAll()
method works with any kind of incoming collection. It will only succeed if the incoming collection really contains only instances ofE
, but:In the case of
- The static type of the incoming collection might differ, perhaps because the caller doesn't know the precise type of the collection being passed in, or perhaps because it is a
Collection<S>
,whereS
is a subtype ofE
.- It's perfectly legitimate to call
containsAll()
with a collection of a different type. The routine should work, returningfalse
.addAll()
, we should be able to add any collection that consists of instances of a subtype ofE
. We saw how to handle this situation correctly in section Generic Methods.You also need to ensure that the revised API retains binary compatibility with old clients. This implies that the erasure of the API must be the same as the original, ungenerified API. In most cases, this falls out naturally, but there are some subtle cases. We'll examine one of the subtlest cases we've encountered, the method
Collections.max()
. As we saw in section More Fun with Wildcards, a plausible signature formax()
is:This is fine, except that the erasure of this signature is:public static <T extends Comparable<? super T>> T max(Collection<T> coll)which is different than the original signature ofpublic static Comparable max(Collection coll)max()
:One could certainly have specified this signature forpublic static Object max(Collection coll)max()
, but it was not done, and all the old binary class files that callCollections.max()
depend on a signature that returnsObject
.We can force the erasure to be different, by explicitly specifying a superclass in the bound for the formal type parameter
T
.This is an example of giving multiple bounds for a type parameter, using the syntaxpublic static <T extends Object & Comparable<? super T>> T max(Collection<T> coll)T1 & T2 ... & Tn
. A type variable with multiple bounds is known to be a subtype of all of the types listed in the bound. When a multiple bound is used, the first type mentioned in the bound is used as the erasure of the type variable.Finally, we should recall that
max
only reads from its input collection, and so is applicable to collections of any subtype ofT
.This brings us to the actual signature used in the JDK:
It's very rare that anything so involved comes up in practice, but expert library designers should be prepared to think very carefully when converting existing APIs.public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll)Another issue to watch out for is covariant returns, that is, refining the return type of a method in a subclass. You should not take advantage of this feature in an old API. To see why, let's look at an example.
Assume your original API was of the form:
Taking advantage of covariant returns, you modify it to:public class Foo { public Foo create() { ... } // Factory. Should create an instance of whatever class it is declared in. } public class Bar extends Foo { public Foo create() { ... } // Actually creates a Bar. }Now, assume a third party client of your code wrote the following:public class Foo { public Foo create() { ... } // Factory. Should create an instance of whatever class it is declared in. } public class Bar extends Foo { public Bar create() { ... } // Actually creates a Bar. }The Java virtual machine does not directly support overriding of methods with different return types. This feature is supported by the compiler. Consequently, unless the classpublic class Baz extends Bar { public Foo create() { ... } // Actually creates a Baz. }Baz
is recompiled, it will not properly override thecreate()
method ofBar
. Furthermore,Baz
will have to be modified, since the code will be rejected as written--the return type ofcreate()
inBaz
is not a subtype of the return type ofcreate()
inBar
.
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.