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

Elements.overrides is confused by package access

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • 25
    • 21
    • core-libs
    • None

      Consider this example:

          package p1; public class A { void m() { System.out.println("A.m"); } }

          package p2; public class B extends p1.A { } // different package

          package p1; public class C extends p2.B {
              @Override public void m() { System.out.println("C.m"); }
              public static void main(String[] args) { ( (A) new C() ).m(); }
          }

      Compile and run it:

          % javac -sourcepath . p1/*.java p2/*.java
          % java -classpath . p1.C
          C.m

      Printout "C.m" is expected and correct because C.m overrides A.m from C. This case is covered by JLS 8.4.8.1. Overriding (by Instance Methods):

      > * mA is declared with package access in the same package as C, and either C declares mC or mA is a member of the direct superclass type of C.

      Indeed, while C declares mC (here m), mA (here m) is not a member of the direct superclass type of C (here B) because A.m has package access in p1, which is different from p2, where B is declared.

      However, when examined programmatically, using javax.lang.model, the call below incorrectly returns false; pseudocode:

          Elements.overrides(C.m, A.m, C) == false

            darcy Joe Darcy
            prappo Pavel Rappo
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: