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

Inappropriate compiler error when creating Comparator with method reference

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • 9
    • 8u45, 9
    • tools
    • b47
    • x86_64
    • windows_7

      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 :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      When:
      - creating a Comparator by using the static method Comparator.comparing
      - and a method reference is used as the "keyExtractor" argument
      - and the return type of the method reference does not implement Comparable
      then javac gives an inappropriate error message saying "non-static method XXX cannot be referenced from a static context". This is not helpful at all. Instead it should ideally give an error message saying "the key type K does not implement Comparable<K>".

      Please see "Steps to Reproduce" for further information.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      save the test case to Issue.java
      run javac Issue.java

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      A compiler error saying something like:
      the key type K does not implement Comparable<K>
      ACTUAL -
      A long compiler error, at the very end saying:

      invalid method reference
      non-static method getInputStream() cannot be referenced from a static context


      REPRODUCIBILITY :
      This bug can be reproduced always.

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

      public class Issue {

      Comparator<Process> cmp = Comparator.comparing(Process::getInputStream);

      }

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

            dlsmith Dan Smith
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: