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

(ref) Private finalize method invoked in preference to protected superclass method

XMLWordPrintable

    • b117

        To invoke the finalize method, it is currently done through JNI since the method is protected. However, an existing bug in JNI_GetMethodID that doesn't skip private method (JDK-8027270) causes the non-private finalize method in a subclass not being invoked.

        class A {
          public static void main(String[] args) {
            A obj = new B();
            obj.finalize();
            obj = null;
            System.gc();
            System.runFinalization();
          }

          protected void finalize() {
            System.out.println("A.finalize");
          }
        }

        class B extends A {
          private void fin_lize() {
            System.out.println("B.finalize");
          }
        }

        Compile this and patch B.class to replace fin_lize with finalize.

        A suggestion from Jeroen Frijters [1] to replace the native finalization implementation with shared secrets in one way for performance improvement that will avoid the expense of native code and will fix this issue since the bytecode behavior is to dispatch to the non-private instance method (i.e. A.finalize).

        [1] http://weblog.ikvm.net/PermaLink.aspx?guid=0f35cb7d-e3b3-400b-b829-6c975fa97646

              mchung Mandy Chung
              mchung Mandy Chung
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

                Created:
                Updated:
                Resolved: