Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Edited by sciSpider Java v3.0

...

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

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

...

Code Block
bgColor#FFCCCC
public class MixedTypes {
  @SuppressWarnings("unchecked"&quot;unchecked&quot;)
  private static void addToList(List list, Object obj) {
    list.add(obj); // Unchecked warning
  }
  private static void print() {
    List<String>List&lt;String&gt; list = new ArrayList<String>ArrayList&lt;String&gt; ();
    addToList(list, 1);
    System.out.println(list.get(0));
  }
  public static void main(String[] args) {
    MixedTypes.print();
  }
}

...

Code Block
Exception in thread "main"&quot;main&quot; java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
	at Raw.print(Test.java:11)
	at Raw.main(Test.java:14)

...

Code Block
bgColor#ccccff
class Parameterized {
  private static void addToList(List<String>List&lt;String&gt; list, String str) {
    list.add(str);     // Unchecked warning
  }
  private static void print() {
    List<String>List&lt;String&gt; list = new ArrayList<String>ArrayList&lt;String&gt; ();
    addToList(list, "1"&quot;1&quot;);
    System.out.println(list.get(0));
  }
  public static void main(String[] args) {
    Parameterized.print();
  }
}

...

Code Block
bgColor#FFCCCC
class BadListAdder {
  @SuppressWarnings("unchecked"&quot;unchecked&quot;)
  private static void addToList(List list, Object obj) {
    list.add(obj);     // Unchecked warning
  }
  private static <T>&lt;T&gt; void printOne(T type) {
    if (!(type instanceof Integer || type instanceof Double)) {
      System.out.println("&quot;Cannot print in the supplied type"&quot;);
    }
    List<T>List&lt;T&gt; list = new ArrayList<T>ArrayList&lt;T&gt;();
    addToList(list, 1);
    System.out.println(list.get(0));
  }
  public static void main(String[] args) {
    double d = 1;
    int i = 1;
    System.out.println(d); 
    BadListAdder.printOne(d);
    System.out.println(i);
    BadListAdder.printOne(i);
  }
}

...

Code Block
bgColor#ccccff
class GoodListAdder {
  private static void addToList(List<Integer>List&lt;Integer&gt; list, Integer i) {
    list.add(i);
  }
  private static void addToList(List<Double>List&lt;Double&gt; list, Double d) {
    list.add(d);
  }
  private static <T>&lt;T&gt; void printOne(T type) {
    if (type instanceof Integer) {
      List<Integer>List&lt;Integer&gt; list = new ArrayList<Integer>ArrayList&lt;Integer&gt;();
      addToList(list, 1);
      System.out.println(list.get(0));
    }
    else if (type instanceof Double) {
      List<Double>List&lt;Double&gt; list = new ArrayList<Double>ArrayList&lt;Double&gt;();

      // This will not compile if addToList(list, 1) is used
      addToList(list, 1.0);

      System.out.println(list.get(0));
    }
    else {
      System.out.println("&quot;Cannot print in the supplied type"&quot;);
    }
  }
  public static void main(String[] args) {
    double d = 1;
    int i = 1;
    System.out.println(d);
    GoodListAdder.printOne(d);
    System.out.println(i);
    GoodListAdder.printOne(i);
  }
}

...

Code Block
if(o instanceof Set) { // Raw type
Set<?>Set&lt;?&gt; m = (Set<?>Set&lt;?&gt;) o; // Wildcard type 
...
}

...

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

References

Wiki Markup
\[[Langer 08|AA. Java References#Langer 08]\] Topic 3, "&quot;[Coping with Legacy|http://www.angelikalanger.com/GenericsFAQ/FAQSections/ProgrammingIdioms.html#Topic3]"&quot;
[[Bloch 08|AA. Java References#Bloch 08]\] Item 23: "&quot;Don't use raw types in new code"&quot;
[[Bloch 07|AA. Java References#Bloch 07]\] Generics, 1. "&quot;Avoid Raw Types in New Code"&quot;
\[[Naftalin 06b|AA. Java References#Naftalin 06b]\] "&quot;Principle of Indecent Exposure"&quot;
[[JLS 05|AA. Java References#JLS 05]\] 4.8 "&quot;Raw types"&quot; and 5.1.9 "&quot;Unchecked Conversion"&quot;

...

MSC04-J. Carefully design interfaces before releasing them      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;49. Miscellaneous (MSC)      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IDS15-J. Library methods should validate their parameters