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

Can't use super::x method reference when x is protected

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 9
    • 8
    • tools
    • b89
    • x86
    • generic
    • Verified

        FULL PRODUCT VERSION :
        java version "1.8.0_60"
        Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
        Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)

        ADDITIONAL OS VERSION INFORMATION :
        Linux Mint (Rebecca, 3.13.0-37-generic #64-Ubuntu SMP)
        Mac OS X Yosemite

        A DESCRIPTION OF THE PROBLEM :
        Attempting to create a method reference super::Identifier fails when:

        - Superclass is declared in a different package
        - Method being referenced is declared protected in Superclass

        This compiles properly in Eclipse, but fails in Oracle JDK 8 javac.

        According to JLS 6.6.2, a protected method can be accessed in the body of a subclass, except in the cases mentioned in the bullet points. super::identifier matches none of the cases, "super" not being an ExpressionName, a Primary expression, nor a ReferenceType.

        Therefore such access should be allowable, exactly as the invocation super.x() would be.


        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Create the two classes in the test case below, and compile them with

        javac package1/Parent.java package2/Child2.java

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        Compilation should succeed.
        ACTUAL -
        Compilation error:



        ERROR MESSAGES/STACK TRACES THAT OCCUR :
        package2/Child2.java:8: error: incompatible types: invalid method reference
                Runnable r = super::protectedMethod;
                             ^
            protectedMethod() has protected access in Parent
        1 error


        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        package package1;
         
        public class Parent {
         
            protected void protectedMethod() {
                System.out.println("Parent's protected method");
            }
        }



        package package2;
         
        public class Child extends package1.Parent {
         
            public void protectedMethod() {
                System.out.println( "In Child's protected method, calling Parent's" );
                Runnable r = super::protectedMethod;
                r.run();
            }
         
            public static void main(String[] args) {
                Child child = new Child();
                child.protectedMethod();
            }
        }

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

        CUSTOMER SUBMITTED WORKAROUND :
        Compiling with eclipse

              sadayapalam Srikanth Adayapalam (Inactive)
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              7 Start watching this issue

                Created:
                Updated:
                Resolved: