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 also checks for the equivalent call Runtime.getRuntime().exit().

This rule is defined by the following XPath expression:

//Name[
    starts-with(@Image,'System.exit')
    or
    (starts-with(@Image,'Runtime.getRuntime') and ../../PrimarySuffix[ends-with(@Image,'exit')])
]

    		

Example:

                
    		
public class Foo {
    public void bar() {
        // never call this when running in an application server !
        System.exit(0);
    }

    public void foo() {
        Runtime.getRuntime().exit(0);   // never stop the JVM manually, the container will do this.
    }
}
     
            

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