##### Overview

- Download PMD 3.9
- What's new in PMD 3.9
- PMD in the news
- PMD-related products and books
- Best practices
- Future directions
- Similar projects
- Credits
- License
- What does 'PMD' mean?

##### Usage

- Installation
- Command line usage
- Ant task usage
- Maven plugin usage
- IDE plugin usage
- Suppressing warnings
- Finding duplicated code
- JSP support

##### Customizing PMD

- Compiling PMD
- How to write a rule
- Writing XPath rules
- How to make a rule set
- How it works

##### For example

- Run PMD on a Sourceforge project

##### Rule Sets

- Index
- Basic
- Braces
- Code Size
- Clone
- Controversial
- Coupling
- Design
- Finalizers
- Import Statements
- J2EE
- Javabeans
- JUnit Tests
- Logging (Java)
- Logging (Jakarta)
- Migrating
- Naming
- Optimizations
- Strict Exceptions
- Strings
- Sun Security
- Unused Code
- Java Server Pages
- Java Server Faces

##### Project Documentation

- About PMD
- Project Info
- Project Reports
- Development Process

## 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(*)&gt;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();
     }
    }