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

Inappropriate compiler error when creating Comparator with method reference

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P4
    • Resolution: Duplicate
    • Affects Version/s: 8u45, 9
    • Fix Version/s: 9
    • Component/s: tools
    • Labels:
    • Subcomponent:
    • Resolved In Build:
      b47
    • CPU:
      x86_64
    • OS:
      windows_7

      Description

      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 ----------

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                Created:
                Updated:
                Resolved: