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

Incorrect method reference type inference

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 17, 18, 19
    • tools
    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      Compiling the provided snippet results in an error. Compilation with `-Xdiags:verbose` succeeds.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      javac Test.java
      javac -Xdiags:verbose Test.java

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      class files created
      ACTUAL -
      First command results in an error:
      Test.java:13: error: incompatible types: invalid method reference
                      .forEach(CorridorProperties::toJson);
                               ^
          method toJson in class CorridorProperties cannot be applied to given types
            required: no arguments
            found: Object
            reason: actual and formal argument lists differ in length
      Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
      1 error

      Second command succeeds.

      ---------- BEGIN SOURCE ----------
      import java.util.List;
      import java.util.function.Function;

      public class Test {

          public static class CorridorProperties { public void toJson() {} }
          public interface CorridorPropertiesResolver { CorridorProperties resolveFor(Object ctx); }

          private void corridorProperties() {
              wrap(user -> {
                  List.<CorridorPropertiesResolver>of().stream()
                      .map(resolver -> resolver.resolveFor(user))
                      .forEach(CorridorProperties::toJson);
                  return null;
              });
          }


          private static Object wrap(Function<String, Void> handler) {
              return null;
          }
      }

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

      FREQUENCY : always


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

              Created:
              Updated:
              Resolved: