# Tuesday, 29 October 2013
« Java Method Overriding Is FUBAR Part 7 o... | Main | Publicly Reported OpenJDK Vulnerability ... »
Java Method Overriding Is FUBAR Part 8 of ∞

Due to my laziness and lameness my previous post wasn't as convincing as it should have been, so I'm going to try again.

Take this code:

class A {
  public static void main(String[] args) {
    A obj = new B();
    obj = null;

  protected void finalize() {

class B extends A {
  private void fin_lize() {

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

What is the expected output of this program? In my opinion it is:


This is also the output that both JDK 1.1 and IKVM.NET give. However, on JDK 1.5 and up the output is:


This did not change in the 7u45 update.

To me the above is already enough to obviously demonstrate the problem, but since I'm apparently somewhat atypical I'm going to try to explain a bit more carefully.

  1. The Java 7 vmspec claims that private methods can override base class methods.
  2. JVMS section 5.4.5. Method overriding was added in the Java 7 edition of the JVMS. It is clearly incorrect as it disagrees with both previous and current behavior of all known JVMs.
  3. Given that private methods do not in fact override any methods, the check for final base class methods that was introduced in 7u45 is bogus.
  4. What should have been fixed in 7u45 is the native code that calls the finalize method, as it clearly needs to do a virtual invocation of Object.finalize() not call the private B.finalize method.

I hope I've done a better job now of making clear way the 7u45 update is bogus, the JVMS spec change is wrong and that finalization still needs to be fixed.

Tuesday, 29 October 2013 13:53:34 (W. Europe Standard Time, UTC+01:00)  #    Comments [1]
Saturday, 02 November 2013 13:25:01 (W. Europe Standard Time, UTC+01:00)
For what it is worth, JamVM also gives:


The finalizer thread is native (hangover from GNU Classpath days) and does a virtual invocation via the method table using the index of Object's finalize method. Private methods are skipped when constructing the method table as they can't be invoked virtually.
Home page

I apologize for the lameness of this, but the comment spam was driving me nuts. In order to be able to post a comment, you need to answer a simple question. Hopefully this question is easy enough not to annoy serious commenters, but hard enough to keep the spammers away.

Anti-Spam Question: What method on java.lang.System returns an object's original hashcode (i.e. the one that would be returned by java.lang.Object.hashCode() if it wasn't overridden)? (case is significant)

Comment (HTML not allowed)  

Live Comment Preview