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

Compile error when chaining Comparator.thenComparing

XMLWordPrintable

    • x86_64
    • linux
    • Verified

      FULL PRODUCT VERSION :
      java version "1.8.0_45"
      Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
      Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Linux 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt9-3~deb8u1 (2015-04-24) x86_64 GNU/Linux

      A DESCRIPTION OF THE PROBLEM :
      The compilation fails with an error when chaining calls to Comparator.thenComparing. For example, this code fails:

      Comparator<Person> c = Comparator.comparing(p -> p.name).thenComparing(p -> p.age);

      but this code doesn't:
      Comparator<Person> d = Comparator.comparing(p -> p.name);
      d = d.thenComparing(p -> p.age);


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      import java.util.Comparator;

      public class Test {
          static class Person {
              String name;
              int age;
          }

          public static void main(String[] args) {
              // Does not compile
              Comparator<Person> c = Comparator.comparing(p -> p.name).thenComparing(p -> p.age);
          }
      }


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The code should compile.
      ACTUAL -
      The code does not compile.


      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      cannot find symbol
      symbol: variable name
      location: variable p of type java.lang.Object


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class Test {
          static class Person {
              String name;
              int age;
          }

          public static void main(String[] args) {
              // Does not compile
              Comparator<Person> c = Comparator.comparing(p -> p.name).thenComparing(p -> p.age);
          }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      public class Test {
          static class Person {
              String name;
              int age;
          }

          public static void main(String[] args) {
              // Does compile
              Comparator<Person> d = Comparator.comparing(p -> p.name);
              d = d.thenComparing(p -> p.age);
          }
      }


            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: