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

Method reference can't see package-access tvar method

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • 9
    • 8u51
    • tools
    • b54
    • x86_64
    • linux

      FULL PRODUCT VERSION :
      java version "1.8.0_51"
      Java(TM) SE Runtime Environment (build 1.8.0_51-b16)
      Java HotSpot(TM) 64-Bit Server VM (build 25.51-b03, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Linux ** 4.0.0-2-amd64 #1 SMP Debian 4.0.8-2 (2015-07-22) x86_64 GNU/Linux

      Java not taken from Debian repositories but self installed

      also reproduced on a current Windows 7


      A DESCRIPTION OF THE PROBLEM :
      When having a generic typed argument, specified to be a subclass via extends, I cannot access member method from this type. For example, in "<C extends Field> void test(C field)", I cannot access fields member methods

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      try to compile the attached source code

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I would expect the code to compile successfully, like it does with ECJ, since there isn't any ambiguous references or something.
      ACTUAL -
      The code does not compile. The compiler

      Test.java:10: error: invalid method reference
              Consumer<String> setter = field::setValue;
                                        ^
        cannot find symbol
          symbol: method setValue(String)
          location: bound of type variable C
        where C is a type-variable:
          C extends Field declared in method <C>test(C)
      1 error


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.util.function.Consumer;
       
      class Field {
          void setValue(String t) {}
      }
       
      class Test {
          protected <C extends Field> void test(C field) {
              Consumer<String> setter = field::setValue;
          }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      One simple work around would be assigning the generic variable to a new, not generic variable first:

      --------
        Field f = field;
        Consumer<String> setter = f::setValue;
      --------

      or using a lambda instead of a method reference:

      --------
        Consumer<String> setter = val -> field.setValue(val);
      --------

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

              Created:
              Updated:
              Resolved: