JDK-8027351 : (ref) Private finalize method invoked in preference to protected superclass method
  • Type: Bug
  • Component: core-libs
  • Affected Version: 5.0-pool
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2013-10-25
  • Updated: 2017-05-17
  • Resolved: 2013-11-08
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availabitlity Release.

To download the current JDK release, click here.
JDK 7 JDK 8 Other
7u60Fixed 8 b117Fixed port-stage-ppc-aixFixed
Related Reports
Relates :  
Relates :  
Relates :  
Relates :  
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