Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6638712

Inference with wildcard types causes selection of inapplicable method

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 7
    • 6u10, 6u23, 7
    • tools
    • None
    • b64
    • generic, x86
    • generic, windows_xp
    • Verified

        From Neal Gafter:

        The following demonstrates a bug in javac's handling of wildcard types that results in a hole in the type system. javac accepts this program without even a warning but it blows up with a class cast error at runtime. The Java compiler for Eclipse rejects this code.

        import java.util.*;

        class Comp {
          static <T> Comparator<T> compound(final Iterable<? extends Comparator<? super T>> it) {
            return new Comparator<T>() {
              public int compare(T t1, T t2) {
                for (Comparator<? super T> c : it) {
                  int r = c.compare(t1, t2);
                  if (r != 0) return r;
                }
                return 0;
              }
            };
          }
          public static void main(String[] args) {
            List<Comparator<?>> x = new ArrayList<Comparator<?>>();
            Comparator<Integer> c1 = new Comparator<Integer>() {
              public int compare(Integer i1, Integer i2) {
                return i1.compareTo(i2);
              }
            };
            x.add(c1);
            x.add(String.CASE_INSENSITIVE_ORDER);
            Comparator<String> c3 = compound(x);
            c3.compare("foo", "bar");
          }
        }

              mcimadamore Maurizio Cimadamore
              jjg Jonathan Gibbons
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: