United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-8027351 : (ref) Private finalize method invoked in preference to protected superclass method

Details
Type:
Bug
Submit Date:
2013-10-25
Status:
Resolved
Updated Date:
2013-12-14
Project Name:
JDK
Resolved Date:
2013-11-08
Component:
core-libs
OS:
Sub-Component:
CPU:
Priority:
P3
Resolution:
Fixed
Affected Versions:
5.0-pool
Fixed Versions:

Related Reports
Backport:
Backport:
Relates:
Relates:
Relates:
Relates:

Sub Tasks

Description
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
                                    

Comments
URL:   http://hg.openjdk.java.net/jdk8/tl/jdk/rev/41d7ce111bd8
User:  mchung
Date:  2013-11-08 15:55:36 +0000

                                     
2013-11-08
URL:   http://hg.openjdk.java.net/jdk8/jdk8/jdk/rev/41d7ce111bd8
User:  lana
Date:  2013-11-19 18:24:53 +0000

                                     
2013-11-19



Hardware and Software, Engineered to Work Together