Finalizer Rules

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

EmptyFinalizer

If the finalize() method is empty, then it does not need to exist.

This rule is defined by the following XPath expression:


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

                

Here's an example of code that would trigger this rule:

			

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

       
		

FinalizeOnlyCallsSuperFinalize

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

This rule is defined by the following XPath expression:


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

                

Here's an example of code that would trigger this rule:

			
           
public class Foo {
   protected void finalize() {
     super.finalize();
   }
}
           
       
		

FinalizeOverloaded

Methods named finalize() should not have parameters. It is confusing and probably a bug to overload finalize(). It will not be called by the VM.

This rule is defined by the following XPath expression:


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

            

Here's an example of code that would trigger this rule:

			

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

   
		

FinalizeDoesNotCallSuperFinalize

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

This rule is defined by the following XPath expression:



//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'])]

                

Here's an example of code that would trigger this rule:

			

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

       
		

FinalizeShouldBeProtected

If you override finalize(), make it protected. If you make it public, other classes may call it.

This rule is defined by the following XPath expression:

                    
//MethodDeclaration[@Protected="false"]
  /MethodDeclarator[@Image="finalize"]
  [not(FormalParameters/*)]
                    
                

Here's an example of code that would trigger this rule:

			
  
public class Foo {
 public void finalize() {
  // do something
 }
}
  
      
		

AvoidCallingFinalize

Object.finalize() is called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

This rule is defined by the following Java class: net.sourceforge.pmd.rules.AvoidCallingFinalize

Here's an example of code that would trigger this rule:

			

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