Ensuring finalize() methods are always called

Background

unlike constructors, the JVM does not enforce chained calling of the finalizers of superclasses. To ensure that a finalize() method always gets called on object finalization, it either has to be declared final (or in a final class), or, preferably, the finalizer code is placed in a so-called 'finalizer guardian': A private instantiation of a subclass of Object with just the finalizer method inside.

Since 0.7

Example

Bad code:

Good code:



public class TestClass {
  protected void finalize() {
    //finalize something
  }
}
public class TestClass {
  private final Object finalizerGuardian = 
    new Object() {
      protected void finalize() {
        //finalize something
      }
    };
}


Requires bindings

This code check requires Eclipse to resolve bindings. Enabling this code check increases build time.

Available quick fixes

The quick fix for this code check replaces the finalizer method with a finalizer guardian.