Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Generically typed code can be freely used with raw types when attempting to preserve compatibility between non-generic legacy code and newer generic code. Using raw types with generic code causes most Java compilers to issue "unchecked" warnings but still compile the code. When generic and nongeneric types are used together correctly, these warnings can be ignored; at other times, these warnings can denote potentially unsafe operations.

According to the Java Language Specification, §4.8, "Raw Types," [JLS 2005]:

The use of raw types is allowed only as a concession to compatibility of legacy code. The use of raw types in code written after the introduction of genericity into the Java programming language is strongly discouraged. It is possible that future versions of the Java programming language will disallow the use of raw types.

An attempt by a parameterized type to access an object that is not of the parameterized type is called heap pollution (see the Parameterized classes have expectations of the objects that they reference; they expect certain objects to match their paramaterized types. Heap pollution occurs when a parameterized class references an object that it expects to be of the parameterized type, but the object is a different type. For more information on heap pollution, see the Java Language Specification, §4.12.2.1, "Heap Pollution," [JLS 2005]). For instance, consider the following code snippet.

...

Extending legacy classes and making the overriding methods generic fails because this is disallowed by the Java Language Specification..Do not use generic types and raw types in the same package, class, or method

Mixing generically typed code with raw typed code is one common source of heap pollution. Prior to Java 5, all code used raw types. Allowing mixing enabled developers to preserve compatibility between non-generic legacy code and newer generic code. Using raw types with generic code causes most Java compilers to issue "unchecked" warnings but still compile the code. When generic and nongeneric types are used together correctly, these warnings can be ignored; at other times, these warnings can denote potentially unsafe operations.

According to the Java Language Specification§4.8, "Raw Types," [JLS 2005]:

The use of raw types is allowed only as a concession to compatibility of legacy code. The use of raw types in code written after the introduction of genericity into the Java programming language is strongly discouraged. It is possible that future versions of the Java programming language will disallow the use of raw types.

Noncompliant Code Example

This noncompliant code example compiles but produces an unchecked warning because the raw type of the List.add() method is used (the list parameter in the addToList() method) rather than the parameterized type.

...

When executed, this code throws an exception. This happens not because a List<String> receives an Integer but because the value returned by list.get(0) is an improper type (an Integer rather than a String). In other words, the code throws an exception some time after the execution of the operation that actually caused the error, complicating debugging.

Compliant Solution (Parameterized Collection)

This compliant solution enforces type safety by changing the addToList() method signature to enforce proper type checking.

...

The compiler prevents insertion of an Object to the parameterized list because addToList() cannot be called with an argument whose type produces a mismatch. This code has consequently been changed to add a String to the list instead of an Integer.

Compliant Solution (Legacy Code)

While the previous compliant solution eliminates use of raw collections, it may be infeasible to implement this solution when interoperating with legacy code.

...

The compiler still issues the unchecked warning, which may still be ignored. However, the code now fails when it attempts to add the Integer to the list, consequently preventing the program from proceeding with invalid data.

Noncompliant Code Example

This noncompliant code example compiles and runs cleanly because it suppresses the unchecked warning produced by the raw List.add() method. The printOne() method intends to print the value 1, either as an int or as a double depending on the type of the variable type.

...

However, despite list being correctly parameterized, this method prints 1 and never 1.0 because the int value 1 is always added to list without being type checked. This code produces the following output:

Code Block
1.0
1
1
1

Compliant Solution

This compliant solution generifies the addToList() method, eliminating any possible type violations.

...

If the method addToList() is externally defined (such as in a library or as an upcall method) and cannot be changed, the same compliant method printOne() can be used, but no warnings result if addToList(1) is used instead of addToList(1.0). Great care must be taken to ensure type safety when generics are mixed with nongeneric code.

Exceptions

OBJ03-EX0: Raw types must be used in class literals. For example, because List<Integer>.class is invalid, it is permissible to use the raw type List.class [Bloch 2008].

OBJ03-EX1: The instanceof operator cannot be used with generic types. It is permissible to mix generic and raw code in such cases [Bloch 2008].

...

.

...

Risk Assessment

Mixing generic and nongeneric code can produce unexpected results and exceptional conditions.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

OBJ03-J

low

probable

medium

P4

L3

Bibliography

[Bloch 2008]

Item 23. Don't use raw types in new code

[Bloch 2007]

[Bloch 2005]

Puzzle 88. Raw Deal

[Darwin 2004]

8.3, Avoid Casting by Using Generics

[JavaGenerics 2004]

 

[JLS 2005]

Chapter 5, Conversions and Promotions

 

§4.8, Raw Types

 

§5.1.9, Unchecked Conversion

[Langer 2008]

Topic 3, Coping with Legacy

[Naftalin 2006]

Chapter 8, Effective Generics

[Naftalin 2006b]

Principle of Indecent Exposure

[Schildt 2007]

Create a checked collection

...