Wednesday, September 28, 2011

PMD - Import Rules

The eighth installments of explanation of PMD rules covering Import rules

Import Statement Rules

These rules deal with different problems that can occur with a class' import statements.

DuplicateImports

Avoid duplicate import statements.

Example

import java.lang.String;
import java.lang.*;
public class Foo {}

DontImportJavaLang

Avoid importing anything from the package 'java.lang'. These classes are automatically imported (JLS 7.5.3).

Example

// this is bad
import java.lang.String;
public class Foo {}
 
// --- in another source code file...
 
// this is bad
import java.lang.*;
 
public class Foo {}

UnusedImports

Avoid unused import statements.

Example

// this is bad
import java.io.File;
public class Foo {}

ImportFromSamePackage

No need to import a type that lives in the same package.

Example

package foo;
import foo.Buz; // no need for this
import foo.*; // or this
public class Bar{}

TooManyStaticImports

If you overuse the static import feature, it can make your program unreadable and unmaintainable, polluting its namespace with all the static members you import. Readers of your code (including you, a few months after you wrote it) will not know which class a static member comes from (Sun 1.5 Language Guide).

Example

import static Lennon;
import static Ringo;
import static George;
import static Paul;
import static Yoko; // Too much !

No comments: