...
If addToList()
is externally defined (such as in a library or is an upcall method) and cannot be changed, the same compliant method printOne()
can be used as written above, but there will be no warnings if addToList(1)
is used instead of addToList(1.0)
. Great care must be taken to ensure type safety when generics are mixed with non-generic code.
Exceptions
EX1: Raw types must be used in class literals. For example, since List<Integer>.class
is illegal, one can use raw type List.class
. [Bloch 08]
EX2: The instanceof
operator cannot be used with generic types. Here, it is permissible to mix generic and raw code. [Bloch 08]
Code Block |
---|
if(o instanceof Set) { // Raw type
Set<?> m = (Set<?>) o; // Wildcard type
...
}
|
Risk Assessment
Mixing generic and non-generic code may produce unexpected results and exceptional conditions.
...