##### Overview

- Download PMD 4.2.5
- What's new in PMD 4.2.5
- 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
- Mvn 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
- Rule guidelines

##### For example

- Run PMD on a Sourceforge project

##### Rule Sets

- Index
- Android
- 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
- Project Info
- Project Reports
- Development Process

##### Legend

- External Link
- Opens in a new window

## Finalizer Rules

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

### EmptyFinalizer

Since: PMD 1.5

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]

Example:

    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().

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"]
          ]

Example:

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

### FinalizeOverloaded

Since: PMD 1.5

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]]

Example:

    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.

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

Example:

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

### FinalizeShouldBeProtected

Since: PMD 1.1

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

Example:

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

### AvoidCallingFinalize

Since: PMD 3.0

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

Example:

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