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

Bridge methods change overriding behavior

    XMLWordPrintable

    Details

      Description

      This behavior has been around since bridge methods were first used (javac 5), but it's a bug: a bridge method may "accidentally" override a package-access method, changing overriding behavior.

      -----

      package a; public interface I { public Object m(); }
      package a; public class A { /*package*/ Object m() { return "a"; } }
      package b; public class B extends a.A { public String m() { return "b"; } }
      package a; public class C extends b.B implements I {}

      Test:
      a.A a = new a.C(); System.out.println(a.m()); // expected: "a"; actual: "b"

      -----

      Per JLS 8.4.8.1, B.m does not override A.m, and so when A.m is invoked, B.m should not be selected (15.12.4.4). That works correctly _until_ C implements an interface that happens to declare a method with the same descriptor as A.m. Because then a bridge method will be generated, and the bridge method _does_ override A.m.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              dlsmith Dan Smith
              Reporter:
              dlsmith Dan Smith
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated: