Import Rules

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

DuplicateImports

Avoid duplicate import statements.

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'))]
                
              

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.

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.

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

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