Archive for the ‘Static Code Checks’ Category

In addition to all the Rule validation done by PMD, PMD also checks for copy paste occurrences in the code. This is by far one of the most important checks that PMD does. More the occurrences of Copy-Paste more the likelihood of errors in the code. More the occurrences of Copy Paste the more the possibility of creating errors. One would change in one place and forget in another. Also it leads to code bloat and inefficient memory usage.

Example

This is the twenty second installment of explanation of PMD rules covering Cloning of objects.

Clone Implementation Rules

The Clone Implementation ruleset contains a collection of rules that find questionable usages of the clone() method.Clone is a method used to create a copy of objects in Java. The resulting object must be an exact replica of the object from which it was cloned. It is important to note that in the process of cloning if we use a statement likethis.attribute = classcloned.attributewill only achieve a shallow copy as only the reference of the object attribute will be copied into the cloned class. Any changes to attribute in the clone will impact the original object. Instead we should usethis.attribute = classcloned.attribute,clone()This will ensure that the cloned object gets a cloned copy of the attribute object too.

ProperCloneImplementation

Object clone() should be implemented with super.clone().

Example

class Foo{

public Object clone(){

return new Foo(); // This is bad

}

}

CloneThrowsCloneNotSupportedException

The method clone() should throw a CloneNotSupportedException.

Example

public class MyClass implements Cloneable{

public Object clone() { // will cause an error

MyClass clone = (MyClass)super.clone();

return clone;

}

}

CloneMethodMustImplementCloneable

The method clone() should only be implemented if the class implements the Cloneable interface with the exception of a final method that only throws CloneNotSupportedException.

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

CloneMethodMustImplementCloneable

The method clone() should only be implemented if the class implements the Cloneable interface with the exception of a final method that only throws CloneNotSupportedException. This version uses PMD’s type resolution facilities, and can detect if the class implements or extends a Cloneable class

Example

public class MyClass {

public Object clone() throws CloneNotSupportedException {

return foo;

}

}

UnusedImports

Example

// this is bad

import java.io.*;

public class Foo {}

SignatureDeclareThrowsException

It is unclear which exceptions that can be thrown from the methods. It might be difficult to document and understand the vague interfaces. Use either a class derived from RuntimeException or a checked exception. Junit classes are excluded.