Import Statement Rules

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

DuplicateImports

Avoid duplicate import statements.

This rule is defined by the following Java class: net.sourceforge.pmd.rules.DuplicateImportsRule

Here's an example of code that would trigger this rule:

			

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

// --- in another source code file...

// this is bad
import java.io.*;
import java.io.File;

public class Foo {}

    
		

DontImportJavaLang

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

This rule is defined by the following XPath expression:

                  
//ImportDeclaration
 [starts-with(Name/@Image, 'java.lang')]
 [not(starts-with(Name/@Image, 'java.lang.ref'))]
 [not(starts-with(Name/@Image, 'java.lang.reflect'))]
 [not(starts-with(Name/@Image, 'java.lang.annotation'))]
 [not(starts-with(Name/@Image, 'java.lang.instrument'))]
 [not(starts-with(Name/@Image, 'java.lang.management'))]
                
              

Here's an example of code that would trigger this rule:

			

// 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.

This rule is defined by the following Java class: net.sourceforge.pmd.rules.UnusedImportsRule

Here's an example of code that would trigger this rule:

			

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

    
		

ImportFromSamePackage

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

This rule is defined by the following Java class: net.sourceforge.pmd.rules.ImportFromSamePackageRule

Here's an example of code that would trigger this rule:

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