Finalizer

These rules deal with different problems that can occur with finalizers.

EmptyFinalizer

Since: PMD 1.5

Empty finalize methods serve no purpose and should be removed.


//MethodDeclaration[MethodDeclarator[@Image='finalize'][not(FormalParameters/*)]]
  /Block[count(*)=0]

                
Example(s):

public class Foo {
   protected void finalize() {}
}

       

FinalizeOnlyCallsSuperFinalize

Since: PMD 1.5

If the finalize() is implemented, it should do something besides just calling super.finalize().


//MethodDeclaration[MethodDeclarator[@Image="finalize"][not(FormalParameters/*)]]
   /Block[count(BlockStatement)=1]
     /BlockStatement[
       Statement/StatementExpression/PrimaryExpression
      /PrimaryPrefix[@Image="finalize"]
      ]

                
Example(s):
           
protected void finalize() {
	super.finalize();
}
           
       

FinalizeOverloaded

Since: PMD 1.5

Methods named finalize() should not have parameters. It is confusing and most likely an attempt to overload Object.finalize(). It will not be called by the VM.


//MethodDeclaration
 /MethodDeclarator[@Image='finalize'][FormalParameters[count(*)>0]]

            
Example(s):

public class Foo {
   // this is confusing and probably a bug
   protected void finalize(int a) {
   }
}

   

FinalizeDoesNotCallSuperFinalize

Since: PMD 1.5

If the finalize() is implemented, its last action should be to call super.finalize.



//MethodDeclaration[MethodDeclarator[@Image='finalize'][not(FormalParameters/*)]]
   /Block
      /BlockStatement[last()]
      [not(Statement/StatementExpression/PrimaryExpression/PrimaryPrefix[@Image='finalize'])]
      [not(Statement/TryStatement/FinallyStatement
       /Block/BlockStatement/Statement/StatementExpression
        /PrimaryExpression/PrimaryPrefix[@Image='finalize'])]

                
Example(s):

protected void finalize() {
	something();
	// neglected to call super.finalize()
}

       

FinalizeShouldBeProtected

Since: PMD 1.1

When overriding the finalize(), the new method should be set as protected. If made public, other classes may invoke it at inappropriate times.

                    
//MethodDeclaration[@Protected="false"]
  /MethodDeclarator[@Image="finalize"]
  [not(FormalParameters/*)]
                    
                
Example(s):
  
public void finalize() {
	// do something
}
  
      

AvoidCallingFinalize

Since: PMD 3.0

The method Object.finalize() is called by the garbage collector on an object when garbage collection determines that there are no more references to the object. It should not be invoked by application logic.

This rule is defined by the following Java class:net.sourceforge.pmd.lang.java.rule.finalizers.AvoidCallingFinalizeRule

Example(s):

void foo() {
	Bar b = new Bar();
	b.finalize();
}