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

## J2EE Rules

These are rules for J2EE

### UseProperClassLoader

Since: PMD 3.7

In J2EE getClassLoader() might not work as expected. Use Thread.currentThread().getContextClassLoader() instead.

This rule is defined by the following XPath expression:

    //PrimarySuffix[@Image='getClassLoader']

Example:

    public class Foo {
     ClassLoader cl = Bar.class.getClassLoader();
    }

### MDBAndSessionBeanNamingConvention

Since: PMD 4.0

The EJB Specification state that any MessageDrivenBean or SessionBean should be suffixed by Bean.

This rule is defined by the following XPath expression:

    //TypeDeclaration/ClassOrInterfaceDeclaration
    [
        (
            (./ImplementsList/ClassOrInterfaceType[ends-with(@Image,'SessionBean')])
            or
            (./ImplementsList/ClassOrInterfaceType[ends-with(@Image,'MessageDrivenBean')])
        )
        and
        not
        (
            ends-with(@Image,'Bean')
        )
    ]

Example:

                /* Proper name */
                public class SomeBean implements SessionBean{}

Example:

                /* Bad name */
                public class MissingTheProperSuffix implements SessionBean {}

### RemoteSessionInterfaceNamingConvention

Since: PMD 4.0

Remote Home interface of a Session EJB should be suffixed by 'Home'.

This rule is defined by the following XPath expression:

    //ClassOrInterfaceDeclaration
    [
        (
            (./ExtendsList/ClassOrInterfaceType[ends-with(@Image,'EJBHome')])
        )
        and
        not
        (
            ends-with(@Image,'Home')
        )
    ]

Example:

                /* Proper name */
                public interface MyBeautifulHome extends javax.ejb.EJBHome {}

Example:

                /* Bad name */
                public interface MissingProperSuffix extends javax.ejb.EJBHome {}

### LocalInterfaceSessionNamingConvention

Since: PMD 4.0

The Local Interface of a Session EJB should be suffixed by 'Local'.

This rule is defined by the following XPath expression:

    //ClassOrInterfaceDeclaration
    [
        (
            (./ExtendsList/ClassOrInterfaceType[ends-with(@Image,'EJBLocalObject')])
        )
        and
        not
        (
            ends-with(@Image,'Local')
        )
    ]

Example:

                /* Proper name */
                public interface MyLocal extends javax.ejb.EJBLocalObject {}

Example:

                /* Bad name */
                public interface MissingProperSuffix extends javax.ejb.EJBLocalObject {}

### LocalHomeNamingConvention

Since: PMD 4.0

The Local Home interface of a Session EJB should be suffixed by 'LocalHome'.

This rule is defined by the following XPath expression:

    //ClassOrInterfaceDeclaration
    [
        (
            (./ExtendsList/ClassOrInterfaceType[ends-with(@Image,'EJBLocalHome')])
        )
        and
        not
        (
            ends-with(@Image,'LocalHome')
        )
    ]

Example:

                /* Proper name */
                public interface MyBeautifulLocalHome extends javax.ejb.EJBLocalHome {}

Example:

                /* Bad name */
                public interface MissingProperSuffix extends javax.ejb.EJBLocalHome {}

### RemoteInterfaceNamingConvention

Since: PMD 4.0

Remote Interface of a Session EJB should NOT be suffixed.

This rule is defined by the following XPath expression:

    //ClassOrInterfaceDeclaration
    [
        (
            (./ExtendsList/ClassOrInterfaceType[ends-with(@Image,'EJBObject')])
        )
        and
        (
            ends-with(@Image,'Session')
            or
            ends-with(@Image,'EJB')
            or
            ends-with(@Image,'Bean')
        )
    ]

Example:

                /* Bad Session suffix */
                public interface BadSuffixSession extends javax.ejb.EJBObject {}

Example:

                /* Bad EJB suffix */
                public interface BadSuffixEJB extends javax.ejb.EJBObject {}

Example:

                /* Bad Bean suffix */
                public interface BadSuffixBean extends javax.ejb.EJBObject {}

### DoNotCallSystemExit

Since: PMD 4.1

Web applications should not call System.exit(), since only the web container or the application server should stop the JVM.

This rule is defined by the following XPath expression:

    //Name[starts-with(@Image,'System.exit')]

Example:

    public class Foo {
        public void bar() {
            // NEVER DO THIS IN A APP SERVER !!!
            System.exit(0);
        }
    }

### StaticEJBFieldShouldBeFinal

Since: PMD 4.1

According to the J2EE specification (p.494), an EJB should not have any static fields with write access. However, static read only fields are allowed. This ensures proper behavior especially when instances are distributed by the container on several JREs.

This rule is defined by the following XPath expression:

    //ClassOrInterfaceDeclaration[
        (
        (./ImplementsList/ClassOrInterfaceType[ends-with(@Image,'SessionBean')])
        or
        (./ImplementsList/ClassOrInterfaceType[ends-with(@Image,'EJBHome')])
        or
        (./ImplementsList/ClassOrInterfaceType[ends-with(@Image,'EJBLocalObject')])
        or
        (./ImplementsList/ClassOrInterfaceType[ends-with(@Image,'EJBLocalHome')])
        or
        (./ExtendsList/ClassOrInterfaceType[ends-with(@Image,'EJBObject')])
        )
        and
        (./ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration[
             (./FieldDeclaration[@Static = 'true'])
             and
             (./FieldDeclaration[@Final = 'false'])
        ])
    ]

Example:

    public class SomeEJB extends EJBObject implements EJBLocalHome {
    	private static int BAD_STATIC_FIELD;

    	private static final int GOOD_STATIC_FIELD;
    }

### DoNotUseThreads

Since: PMD 4.1

The J2EE specification explicitly forbid use of threads.

This rule is defined by the following XPath expression:

                            //ClassOrInterfaceType[@Image = 'Thread' or @Image = 'Runnable']

Example:

                // This is not allowed
                public class UsingThread extends Thread {

                }
    			// Neither this,
    			public class OtherThread implements Runnable {
    				// Nor this ...
    				public void methode() {
    					Runnable thread = new Thread(); thread.run();
    				}
    			}