Elements.overrides is confused by package access

XMLWordPrintable

    • Type: Bug
    • Resolution: Unresolved
    • Priority: P4
    • 27
    • Affects Version/s: 21
    • Component/s: 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

            Assignee:
            Joe Darcy
            Reporter:
            Pavel Rappo
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: