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 non-generic types are used together correctly, these warnings are not catastrophic, but can be ignored; at other times, these warnings may denote potentially unsafe operations. If generic and non-generic code must be used together, these warnings should not be simply ignored.

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

...

If a parameterized type tries to access an object that is not of the parameterized type, heap pollution occurs. For instance, consider the code snippet below.

Code Block
List l = new ArrayList<Integer>ArrayList();
List<String> ls = l; // Produces unchecked warning

...

Extending legacy classes and generifying the overriding methods is not a panacea as fails because this is made illegal disallowed by the Java Language Specification [JLS 2005]. It is best to avoid mixing generic and non-generic code.

Noncompliant Code Example

...

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

...

Compliant Solution (Legacy Code)

While the recommended previous compliant solution eliminates usage use of raw collections, this may not always be possible infeasible when using interoperating with legacy code.

Suppose that the addToList() method was legcay legacy code and that could not be changed. The following compliant solution creates a checked view of the list by using the Collections.checkedList() method. This method returns a wrapper collection that performs runtime type checking in its implementation of the add() method before delegating to the backend List<String>. This The wrapper collection may be safely passed to the legacy addToList() method.

Code Block
bgColor#ccccff
class MixedTypes {
  private static void addToList(List list, Object obj) {
    list.add(obj); // Unchecked warning
  }

  public static void main(String[] args) {
    List<String> list = new ArrayList<String> ();
    List<String> checkedList = Collections.checkedList( list, String.class);
    addToList( checkedList, 1);
    System.out.println(list.get(0));
  }
}

...

If the method 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, 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 non-generic code.

Exceptions

OBJ12-EX1: Raw types must be used in class literals. For example, as List<Integer>.class is illegal, it is permissible to use the raw type List.class [Bloch 2008].

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

...

Guideline

Severity

Likelihood

Remediation Cost

Priority

Level

OBJ10-J

low

probable

medium

P4

L3

Bibliography

Wiki Markup
\[[Langer 2008|AA. Bibliography#Langer 08]\] Topic 3, "[Coping with Legacy|http://www.angelikalanger.com/GenericsFAQ/FAQSections/ProgrammingIdioms.html#Topic3]"
[[Bloch 2008|AA. Bibliography#Bloch 08]\] Item 23: "Don't use raw types in new code"
[[Bloch 2007|AA. Bibliography#Bloch 07]\] Generics, 1. "Avoid Raw Types in New Code"
\[[Bloch 2005|AA. Bibliography#Bloch 05]\] Puzzle 88: Raw Deal
\[[Darwin 2004|AA. Bibliography#Darwin 04]\] 8.3 Avoid Casting by Using Generics
\[[JavaGenerics 2004|AA. Bibliography#JavaGenerics 04]\]
\[[JLS 2005\]|AA. Java References#JLS 05] The Java Language Specification, Conversions and Promotions, also 4.8 "Raw types" and 5.1.9 "Unchecked Conversion"
\[[Langer 2008|AA. Bibliography#Langer 08]\] Topic 3, "[Coping with Legacy|http://www.angelikalanger.com/GenericsFAQ/FAQSections/ProgrammingIdioms.html#Topic3]"
\[[Naftalin 2006|AA. Bibliography#Naftalin 06]\] Chapter 8, "Effective Generics"
\[[Naftalin 2006b|AA. Bibliography#Naftalin 06b]\] "Principle of Indecent Exposure"
\[[Schildt 2007|AA. Bibliography#Schildt 07]\] "Create a checked collection"

...