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

javac does not reject private method reference with type-variable receiver

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P3 P3
    • 22
    • tools
    • None
    • source
    • minimal
    • There could be programs using private method reference with type variable receivers. Those programs will be rejected by the compiler if they are recompiled.
    • Language construct
    • Implementation

      Summary

      The javac compiler should not accept private method references with a type variable receiver.

      Problem

      JLS 4.4 specifies that:

      The members of a type variable X with bound T & I 1 & ... & I n are the members of
      the intersection type (ยง4.9) T & I 1 & ... & I n appearing at the point where the type
      variable is declared.

      So code like:

      import java.util.function.*;
      class Test {
          private String asString() {
              return "bar";
          }
      
          static <T extends Test> Function<T, String> foo() {
              return T::asString;
          }
      }

      should be rejected by javac as private method asString is not a member of type variable T

      Solution

      The proposed solution is to sync javac with the specification so that private method references with type variable receiver are rejected by javac

      Specification

      No specification changes are required.

            vromero Vicente Arturo Romero Zaldivar
            mcimadamore Maurizio Cimadamore
            Maurizio Cimadamore
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: