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

generic type declaration of Comparator#naturalOrder seems broken

XMLWordPrintable

      FULL PRODUCT VERSION :
      paul@celebrimbor:~$ java -version
      openjdk version "1.8.0_131"
      OpenJDK Runtime Environment (build 1.8.0_131-8u131-b11-1~bpo8+1-b11)
      OpenJDK 64-Bit Server VM (build 25.131-b11, mixed mode)
      paul@celebrimbor:~$


      ADDITIONAL OS VERSION INFORMATION :
      paul@celebrimbor:~$ lsb_release -a
      No LSB modules are available.
      Distributor ID: Debian
      Description: Debian GNU/Linux 8.9 (jessie)
      Release: 8.9
      Codename: jessie
      paul@celebrimbor:~$


      A DESCRIPTION OF THE PROBLEM :
      The generic type of Comparator class' naturalOrder static method seems to be confused/broken.

      It seems to be impossible to create a "naturalOrder" Comparator where the type is an interface or where the type is Object.

      It seems that other methods in Comparator class may be affected.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Attempt to compile the supplied test case.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      paul@celebrimbor:~$ javac Test.java
      paul@celebrimbor:~$

      ACTUAL -
      paul@celebrimbor:~$ javac Test.java
      Test.java:12: error: incompatible types: inference variable T has incompatible bounds
              Comparator<Object> naturalObject = Comparator.naturalOrder();
                                                                        ^
          equality constraints: Object
          upper bounds: Comparable<? super T>
        where T is a type-variable:
          T extends Comparable<? super T> declared in method <T>naturalOrder()
      Test.java:13: error: incompatible types: inference variable T has incompatible bounds
              Comparator<MyInterface> naturalMyInterface = Comparator.naturalOrder();
                                                                                  ^
          equality constraints: MyInterface
          upper bounds: Comparable<? super T>
        where T is a type-variable:
          T extends Comparable<? super T> declared in method <T>naturalOrder()
      Test.java:16: error: method naturalOrder in interface Comparator<T> cannot be applied to given types;
              Comparator<Object> anotherNaturalObject = Comparator.<Object>naturalOrder();
                                                                  ^
        required: no arguments
        found: no arguments
        reason: explicit type argument Object does not conform to declared bound(s) Comparable<? super Object>
        where T is a type-variable:
          T extends Object declared in interface Comparator
      Test.java:17: error: method naturalOrder in interface Comparator<T> cannot be applied to given types;
              Comparator<MyInterface> anotherNaturalMyInterface = Comparator.<MyInterface>naturalOrder();
                                                                            ^
        required: no arguments
        found: no arguments
        reason: explicit type argument MyInterface does not conform to declared bound(s) Comparable<? super MyInterface>
        where T is a type-variable:
          T extends Object declared in interface Comparator
      4 errors
      paul@celebrimbor:~$


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.util.Comparator;

      public class Test
      {
          public interface MyInterface
          {
          }

          public static void main(String[] args)
          {
              Comparator<Object> naturalObject = Comparator.naturalOrder();
              Comparator<MyInterface> naturalMyInterface = Comparator.naturalOrder();

              Comparator<Object> anotherNaturalObject = Comparator.<Object>naturalOrder();
              Comparator<MyInterface> anotherNaturalMyInterface = Comparator.<MyInterface>naturalOrder();
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Using a type that works (Double, String, ...) and then explicitly cast it to a raw type; e.g.,

          Comparator<Object> natural = (Comparator) Comparator.<Double>naturalOrder();

      or

          Comparator.<Foo>comparingInt(f::someValue)
                      .thenComparing((Comparator) Comparator.<Double>naturalOrder());

      Although this yields warnings unless the method is annotated with @SuppressWarnings("unchecked")

            psonal Pallavi Sonal (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: