Wednesday, September 28, 2011

PMD - J2EE Rules

This is the twenty first installment of explanation of PMD rules covering J2EE Rules.

J2EE Rules

These are rules for J2EE

UseProperClassLoader

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

Example

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

MDBAndSessionBeanNamingConvention

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

Example

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

RemoteSessionInterfaceNamingConvention

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

Example

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

LocalInterfaceSessionNamingConvention

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

Example

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

LocalHomeNamingConvention

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

Example

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

RemoteInterfaceNamingConvention

Remote Interface of a Session EJB should NOT be suffixed.

Example

/* Bad Session suffix */
public interface BadSuffixSession extends javax.ejb.EJBObject {}
/* Bad EJB suffix */
public interface BadSuffixEJB extends javax.ejb.EJBObject {}
/* Bad Bean suffix */
public interface BadSuffixBean extends javax.ejb.EJBObject {}

DoNotCallSystemExit

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

Example

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

StaticEJBFieldShouldBeFinal

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.

Example

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

DoNotUseThreads

The J2EE specification explicitly forbid use of threads.

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();
        }
}

No comments: