Versions Compared

Key

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

...

Noncompliant Code Example

TODOThis code ordinarily produces an unchecked warning because the raw type of the List.add method is being used instead of the parameterized type. To make this compile cleanly, the SuppressWarnings annotation is used.

Code Block
bgColor#FFCCCC

import java.util.*;

public class Test {
  @SuppressWarnings("unchecked")
  public static void addToList(List list, Object obj) {
    list.add(obj);     // "unchecked" warning
  }
  public static <T> void printOne(T type) {
    if (!(type instanceof Integer || type instanceof Double))
      System.out.println("Cannot print in the supplied type");
    List<T> list = new ArrayList<T> ();
    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);
    Test.printOne(d);
    System.out.println(i);
    Test.printOne(i);
  }
}

This code will produce this output

Code Block

1.0
1
1
1 TODO

Compliant Solution

TODO

Code Block
bgColor#ccccff
 TODO

...