Since Checkstyle 3.2
Rationale: Helps reduce coupling on concrete classes.
For additional restriction of type usage see also: IllegalInstantiation, IllegalImport
name | description | type | default value | since |
---|---|---|---|---|
ignoredMethodNames | Specify methods that should not be checked. | String[] | getEnvironment, getInitialContext |
3.2 |
illegalAbstractClassNameFormat | Specify RegExp for illegal abstract class names. | Pattern | "^(.*[.])?Abstract.*$" |
3.2 |
illegalClassNames | Specify classes that should not be used as types in variable declarations, return values or parameters. | String[] | HashMap, HashSet, LinkedHashMap, LinkedHashSet, TreeMap, TreeSet, java.util.HashMap, java.util.HashSet, java.util.LinkedHashMap, java.util.LinkedHashSet, java.util.TreeMap, java.util.TreeSet |
3.2 |
legalAbstractClassNames | Define abstract classes that may be used as types. | String[] | {} |
4.2 |
memberModifiers | Control whether to check only methods and fields with any of the specified modifiers. This property does not affect method calls nor method references nor record components. | subset of tokens TokenTypes | {} |
6.3 |
validateAbstractClassNames | Control whether to validate abstract class names. | boolean | false |
6.10 |
tokens | tokens to check | subset of tokens ANNOTATION_FIELD_DEF , CLASS_DEF , INTERFACE_DEF , METHOD_CALL , METHOD_DEF , METHOD_REF , PARAMETER_DEF , VARIABLE_DEF , PATTERN_VARIABLE_DEF , RECORD_DEF , RECORD_COMPONENT_DEF , RECORD_PATTERN_DEF . | ANNOTATION_FIELD_DEF , CLASS_DEF , INTERFACE_DEF , METHOD_CALL , METHOD_DEF , METHOD_REF , PARAMETER_DEF , VARIABLE_DEF , PATTERN_VARIABLE_DEF , RECORD_DEF , RECORD_COMPONENT_DEF , RECORD_PATTERN_DEF . | 3.2 |
It is possible to set illegal class names via short or
canonical name.
Specifying illegal type invokes analyzing imports and Check puts violations at
corresponding declarations
(of variables, methods or parameters). This helps to avoid ambiguous cases, e.g.:
java.awt.List
was set as illegal class name, then, code like:
import java.util.List; ... List list; //No violation here
will be ok.
In most cases it's justified to put following classes to illegalClassNames:
as methods that are differ from interface methods are rarely used, so in most cases user will benefit from checking for them.
To configure the default check:
<module name="Checker"> <module name="TreeWalker"> <module name="IllegalType"/> </module> </module>
Example:
// violation below 'Usage of type 'TreeSet' is not allowed' public class Example1 extends TreeSet { // violation below 'Usage of type 'java.util.HashSet' is not allowed' public <T extends java.util.HashSet> void method() { // violation below 'Usage of type 'LinkedHashMap' is not allowed' LinkedHashMap<Integer, String> lhmap = new LinkedHashMap<Integer, String>(); // violation below 'Usage of type 'TreeMap' is not allowed' TreeMap<Integer, String> treemap = new TreeMap<Integer, String>(); java.lang.IllegalArgumentException illegalex; // violation below 'Usage of type 'java.util.TreeSet' is not allowed' java.util.TreeSet treeset; } // violation below 'Usage of type 'java.util.HashSet' is not allowed' public <T extends java.util.HashSet> void typeParam(T t) {} // violation below 'Usage of type 'HashMap' is not allowed' public HashMap<String, String> function1() { return null; } // violation below 'Usage of type 'HashMap' is not allowed' private HashMap<String, String> function2() { return null; } // violation below 'Usage of type 'HashMap' is not allowed' protected HashMap<Integer, String> function3() { return null; } public <T extends Boolean, U extends Serializable> void typeParam(T a) {} public <T extends java.util.Optional> void method(T t) { Optional<T> i; } abstract class A { public abstract Set<Boolean> shortName(Set<? super Boolean> a); } class B extends Gitter {} class C extends Github {} public Optional<String> field2; protected String field3; Optional<String> field4; private void method(List<Foo> list, Boolean value) {} void foo() { Optional<String> i; } final Consumer<Foo> consumer = Foo<Boolean>::foo; }
To configure the Check so that particular tokens are checked:
<module name="Checker"> <module name="TreeWalker"> <module name="IllegalType"> <property name="tokens" value="METHOD_DEF"/> </module> </module> </module>
Example:
public class Example2 extends TreeSet { // violation below 'Usage of type 'java.util.HashSet' is not allowed' public <T extends java.util.HashSet> void method() { LinkedHashMap<Integer, String> lhmap = new LinkedHashMap<Integer, String>(); TreeMap<Integer, String> treemap = new TreeMap<Integer, String>(); java.lang.IllegalArgumentException illegalex; java.util.TreeSet treeset; } // violation below 'Usage of type 'java.util.HashSet' is not allowed' public <T extends java.util.HashSet> void typeParam(T t) {} // violation below 'Usage of type 'HashMap' is not allowed' public HashMap<String, String> function1() { return null; } // violation below 'Usage of type 'HashMap' is not allowed' private HashMap<String, String> function2() { return null; } // violation below 'Usage of type 'HashMap' is not allowed' protected HashMap<Integer, String> function3() { return null; } public <T extends Boolean, U extends Serializable> void typeParam(T a) {} public <T extends java.util.Optional> void method(T t) { Optional<T> i; } abstract class A { public abstract Set<Boolean> shortName(Set<? super Boolean> a); } class B extends Gitter {} class C extends Github {} public Optional<String> field2; protected String field3; Optional<String> field4; private void method(List<Foo> list, Boolean value) {} void foo() { Optional<String> i; } final Consumer<Foo> consumer = Foo<Boolean>::foo; }
To configure the Check so that it ignores function() methods:
<module name="Checker"> <module name="TreeWalker"> <module name="IllegalType"> <property name="ignoredMethodNames" value="function1, function2"/> </module> </module> </module>
Example:
// violation below 'Usage of type 'TreeSet' is not allowed' public class Example3 extends TreeSet { // violation below 'Usage of type 'java.util.HashSet' is not allowed' public <T extends java.util.HashSet> void method() { // violation below 'Usage of type 'LinkedHashMap' is not allowed' LinkedHashMap<Integer, String> lhmap = new LinkedHashMap<Integer, String>(); // violation below 'Usage of type 'TreeMap' is not allowed' TreeMap<Integer, String> treemap = new TreeMap<Integer, String>(); java.lang.IllegalArgumentException illegalex; // violation below 'Usage of type 'java.util.TreeSet' is not allowed' java.util.TreeSet treeset; } // violation below 'Usage of type 'java.util.HashSet' is not allowed' public <T extends java.util.HashSet> void typeParam(T t) {} public HashMap<String, String> function1() { return null; } private HashMap<String, String> function2() { return null; } // violation below 'Usage of type 'HashMap' is not allowed' protected HashMap<Integer, String> function3() { return null; } public <T extends Boolean, U extends Serializable> void typeParam(T a) {} public <T extends java.util.Optional> void method(T t) { Optional<T> i; } abstract class A { public abstract Set<Boolean> shortName(Set<? super Boolean> a); } class B extends Gitter {} class C extends Github {} public Optional<String> field2; protected String field3; Optional<String> field4; private void method(List<Foo> list, Boolean value) {} void foo() { Optional<String> i; } final Consumer<Foo> consumer = Foo<Boolean>::foo; }
To configure the Check so that it validates abstract class names:
<module name="Checker"> <module name="TreeWalker"> <module name="IllegalType"> <property name="validateAbstractClassNames" value="true"/> <property name="illegalAbstractClassNameFormat" value="Gitt"/> </module> </module> </module>
Example:
// violation below 'Usage of type 'TreeSet' is not allowed' public class Example4 extends TreeSet { // violation below 'Usage of type 'java.util.HashSet' is not allowed' public <T extends java.util.HashSet> void method() { // violation below 'Usage of type 'LinkedHashMap' is not allowed' LinkedHashMap<Integer, String> lhmap = new LinkedHashMap<Integer, String>(); // violation below 'Usage of type 'TreeMap' is not allowed' TreeMap<Integer, String> treemap = new TreeMap<Integer, String>(); java.lang.IllegalArgumentException illegalex; // violation below 'Usage of type 'java.util.TreeSet' is not allowed' java.util.TreeSet treeset; } // violation below 'Usage of type 'java.util.HashSet' is not allowed' public <T extends java.util.HashSet> void typeParam(T t) {} // violation below 'Usage of type 'HashMap' is not allowed' public HashMap<String, String> function1() { return null; } // violation below 'Usage of type 'HashMap' is not allowed' private HashMap<String, String> function2() { return null; } // violation below 'Usage of type 'HashMap' is not allowed' protected HashMap<Integer, String> function3() { return null; } public <T extends Boolean, U extends Serializable> void typeParam(T a) {} public <T extends java.util.Optional> void method(T t) { Optional<T> i; } abstract class A { public abstract Set<Boolean> shortName(Set<? super Boolean> a); } // violation below 'Usage of type 'Gitter' is not allowed' class B extends Gitter {} class C extends Github {} public Optional<String> field2; protected String field3; Optional<String> field4; private void method(List<Foo> list, Boolean value) {} void foo() { Optional<String> i; } final Consumer<Foo> consumer = Foo<Boolean>::foo; }
To configure the Check so that it verifies only public, protected or static methods and fields:
<module name="Checker"> <module name="TreeWalker"> <module name="IllegalType"> <property name="memberModifiers" value="LITERAL_PUBLIC, LITERAL_PROTECTED, LITERAL_STATIC"/> </module> </module> </module>
Example:
// violation below 'Usage of type 'TreeSet' is not allowed' public class Example5 extends TreeSet { // violation below 'Usage of type 'java.util.HashSet' is not allowed' public <T extends java.util.HashSet> void method() { LinkedHashMap<Integer, String> lhmap = new LinkedHashMap<Integer, String>(); TreeMap<Integer, String> treemap = new TreeMap<Integer, String>(); java.lang.IllegalArgumentException illegalex; java.util.TreeSet treeset; } // violation below 'Usage of type 'java.util.HashSet' is not allowed' public <T extends java.util.HashSet> void typeParam(T t) {} // violation below 'Usage of type 'HashMap' is not allowed' public HashMap<String, String> function1() { return null; } private HashMap<String, String> function2() { return null; } // violation below 'Usage of type 'HashMap' is not allowed' protected HashMap<Integer, String> function3() { return null; } public <T extends Boolean, U extends Serializable> void typeParam(T a) {} public <T extends java.util.Optional> void method(T t) { Optional<T> i; } abstract class A { public abstract Set<Boolean> shortName(Set<? super Boolean> a); } class B extends Gitter {} class C extends Github {} public Optional<String> field2; protected String field3; Optional<String> field4; private void method(List<Foo> list, Boolean value) {} void foo() { Optional<String> i; } final Consumer<Foo> consumer = Foo<Boolean>::foo; }
To configure the check so that it verifies usage of types Boolean and Foo:
<module name="Checker"> <module name="TreeWalker"> <module name="IllegalType"> <property name="illegalClassNames" value="Boolean, Foo"/> </module> </module> </module>
Example:
public class Example6 extends TreeSet { public <T extends java.util.HashSet> void method() { LinkedHashMap<Integer, String> lhmap = new LinkedHashMap<Integer, String>(); TreeMap<Integer, String> treemap = new TreeMap<Integer, String>(); java.lang.IllegalArgumentException illegalex; java.util.TreeSet treeset; } public <T extends java.util.HashSet> void typeParam(T t) {} public HashMap<String, String> function1() { return null; } private HashMap<String, String> function2() { return null; } protected HashMap<Integer, String> function3() { return null; } // violation below 'Usage of type 'Boolean' is not allowed' public <T extends Boolean, U extends Serializable> void typeParam(T a) {} public <T extends java.util.Optional> void method(T t) { Optional<T> i; } abstract class A { // violation below 'Usage of type 'Boolean' is not allowed' public abstract Set<Boolean> shortName(Set<? super Boolean> a); // violation above 'Usage of type 'Boolean' is not allowed' } class B extends Gitter {} class C extends Github {} public Optional<String> field2; protected String field3; Optional<String> field4; // violation below 'Usage of type 'Foo' is not allowed' private void method(List<Foo> list, Boolean value) {} // violation above 'Usage of type 'Boolean' is not allowed' void foo() { Optional<String> i; } // violation below 'Usage of type 'Foo' is not allowed' final Consumer<Foo> consumer = Foo<Boolean>::foo; // violation above 'Usage of type 'Boolean' is not allowed' }
To configure the check to target fields types only:
<module name="Checker"> <module name="TreeWalker"> <module name="IllegalType"> <property name="illegalClassNames" value="java.util.Optional"/> <property name="tokens" value="VARIABLE_DEF"/> <property name="id" value="IllegalTypeOptionalAsField"/> </module> <module name="SuppressionXpathSingleFilter"> <property name="query" value="//METHOD_DEF//*"/> <property name="id" value="IllegalTypeOptionalAsField"/> </module> </module> </module>
Example:
public class Example7 extends TreeSet { public <T extends java.util.HashSet> void method() { LinkedHashMap<Integer, String> lhmap = new LinkedHashMap<Integer, String>(); TreeMap<Integer, String> treemap = new TreeMap<Integer, String>(); java.lang.IllegalArgumentException illegalex; java.util.TreeSet treeset; } public <T extends java.util.HashSet> void typeParam(T t) {} public HashMap<String, String> function1() { return null; } private HashMap<String, String> function2() { return null; } protected HashMap<Integer, String> function3() { return null; } public <T extends Boolean, U extends Serializable> void typeParam(T a) {} public <T extends java.util.Optional> void method(T t) { Optional<T> i; } abstract class A { public abstract Set<Boolean> shortName(Set<? super Boolean> a); } class B extends Gitter {} class C extends Github {} // violation below 'Usage of type 'Optional' is not allowed' public Optional<String> field2; protected String field3; Optional<String> field4; // violation, 'Usage of type 'Optional' is not allowed' private void method(List<Foo> list, Boolean value) {} void foo() { Optional<String> i; } final Consumer<Foo> consumer = Foo<Boolean>::foo; }
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks.coding