IllegalImport

Since Checkstyle 3.0

Description

Checks for imports from a set of illegal packages.

Notes

Note: By default, the check rejects all sun.* packages since programs that contain direct calls to the sun.* packages are "not guaranteed to work on all Java-compatible platforms". To reject other packages, set property illegalPkgs to a list of the illegal packages.

Properties

name description type default value since
illegalClasses Specify class names to reject, if regexp property is not set, checks if import equals class name. If regexp property is set, then list of class names will be interpreted as regular expressions. Note, all properties for match will be used. String[] {} 7.8
illegalPkgs Specify packages to reject, if regexp property is not set, checks if import is the part of package. If regexp property is set, then list of packages will be interpreted as regular expressions. Note, all properties for match will be used. String[] sun 3.0
regexp Control whether the illegalPkgs and illegalClasses should be interpreted as regular expressions. boolean false 7.8

Examples

To configure the check:

<module name="Checker">
  <module name="TreeWalker">
    <module name="IllegalImport"/>
  </module>
</module>
        

To configure the check so that it rejects packages java.io.* and java.sql.*:

<module name="Checker">
  <module name="TreeWalker">
    <module name="IllegalImport">
      <property name="illegalPkgs" value="java.io, java.sql"/>
    </module>
  </module>
</module>
        

The following example shows class with no illegal imports

import java.lang.ArithmeticException;
import java.util.List;
import java.util.Enumeration;
import java.util.Arrays;
import sun.applet.*;

public class InputIllegalImport { }
        

The following example shows class with two illegal imports

  • java.io.*, illegalPkgs property contains this package
  • java.sql.Connection is inside java.sql package
import java.io.*;           // violation
import java.lang.ArithmeticException;
import java.sql.Connection; // violation
import java.util.List;
import java.util.Enumeration;
import java.util.Arrays;
import sun.applet.*;

public class InputIllegalImport { }
        

To configure the check so that it rejects classes java.util.Date and java.sql.Connection:

<module name="Checker">
  <module name="TreeWalker">
    <module name="IllegalImport">
      <property name="illegalClasses"
        value="java.util.Date, java.sql.Connection"/>
    </module>
  </module>
</module>
        

The following example shows class with no illegal imports

import java.io.*;
import java.lang.ArithmeticException;
import java.util.List;
import java.util.Enumeration;
import java.util.Arrays;
import sun.applet.*;

public class InputIllegalImport { }
        

The following example shows class with two illegal imports

  • java.sql.Connection, illegalClasses property contains this class
  • java.util.Date, illegalClasses property contains this class
import java.io.*;
import java.lang.ArithmeticException;
import java.sql.Connection; // violation
import java.util.List;
import java.util.Enumeration;
import java.util.Arrays;
import java.util.Date;      // violation
import sun.applet.*;

public class InputIllegalImport { }
        

To configure the check so that it rejects packages not satisfying to regular expression java\.util:

<module name="Checker">
  <module name="TreeWalker">
    <module name="IllegalImport">
      <property name="regexp" value="true"/>
      <property name="illegalPkgs" value="java\.util"/>
    </module>
  </module>
</module>
        

The following example shows class with no illegal imports

import java.io.*;
import java.lang.ArithmeticException;
import java.sql.Connection;
import sun.applet.*;

public class InputIllegalImport { }
        

The following example shows class with four illegal imports

  • java.util.List
  • java.util.Enumeration
  • java.util.Arrays
  • java.util.Date

All four imports match "java\.util" regular expression

import java.io.*;
import java.lang.ArithmeticException;
import java.sql.Connection;
import java.util.List;          // violation
import java.util.Enumeration;   // violation
import java.util.Arrays;        // violation
import java.util.Date;          // violation
import sun.applet.*;

public class InputIllegalImport { }
        

To configure the check so that it rejects class names not satisfying to regular expression ^java\.util\.(List|Arrays) and ^java\.sql\.Connection:

<module name="Checker">
  <module name="TreeWalker">
    <module name="IllegalImport">
      <property name="regexp" value="true"/>
      <property name="illegalClasses"
        value="^java\.util\.(List|Arrays), ^java\.sql\.Connection"/>
    </module>
  </module>
</module>
        

The following example shows class with no illegal imports

import java.io.*;
import java.lang.ArithmeticException;
import java.util.Enumeration;
import java.util.Date;
import sun.applet.*;

public class InputIllegalImport { }
        

The following example shows class with three illegal imports

  • java.sql.Connection matches "^java\.sql\.Connection" regular expression
  • java.util.List matches "^java\.util\.(List|Arrays)" regular expression
  • java.util.Arrays matches "^java\.util\.(List|Arrays)" regular expression
import java.io.*;
import java.lang.ArithmeticException;
import java.sql.Connection;     // violation
import java.util.List;          // violation
import java.util.Enumeration;
import java.util.Arrays;        // violation
import java.util.Date;
import sun.applet.*;

public class InputIllegalImport { }
        

Example of Usage

Violation Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.imports

Parent Module

TreeWalker