Since Checkstyle 3.4
This check processes files in the following way:
import java.math.BigDecimal
), or the class was referenced with the
package name (i.e. java.math.BigDecimal value
) and the package was
added to the excludedPackages
parameter, the class does not increase
complexity.
excludedClasses
parameter,
the class does not increase complexity.
name | description | type | default value | since |
---|---|---|---|---|
excludeClassesRegexps | Specify user-configured regular expressions to ignore classes. | Pattern[] | ^$ |
7.7 |
excludedClasses | Specify user-configured class names to ignore. | String[] | ArrayIndexOutOfBoundsException, ArrayList, Boolean, Byte, Character, Class, Collection, Deprecated, Deque, Double, DoubleStream, EnumSet, Exception, Float, FunctionalInterface, HashMap, HashSet, IllegalArgumentException, IllegalStateException, IndexOutOfBoundsException, IntStream, Integer, LinkedHashMap, LinkedHashSet, LinkedList, List, Long, LongStream, Map, NullPointerException, Object, Optional, OptionalDouble, OptionalInt, OptionalLong, Override, Queue, RuntimeException, SafeVarargs, SecurityException, Set, Short, SortedMap, SortedSet, Stream, String, StringBuffer, StringBuilder, SuppressWarnings, Throwable, TreeMap, TreeSet, UnsupportedOperationException, Void, boolean, byte, char, double, float, int, long, short, var, void |
5.7 |
excludedPackages | Specify user-configured packages to ignore. | String[] | {} |
7.7 |
max | Specify the maximum threshold allowed. | int | 20 |
3.4 |
To configure the check:
<module name="Checker"> <module name="TreeWalker"> <module name="ClassFanOutComplexity"/> </module> </module>
Example:
The check passes without violations in the following:
class InputClassComplexity { Set set = new HashSet(); // Set, HashSet ignored due to default excludedClasses property Map map = new HashMap(); // Map, HashMap ignored due to default excludedClasses property Date date = new Date(); // Counted, 1 Time time = new Time(); // Counted, 2 Place place = new Place(); // Counted, 3 int value = 10; // int is ignored due to default excludedClasses property void method() { var result = "result"; // var is ignored due to default excludedClasses property } }
The check results in a violation in the following:
class InputClassComplexity { Set set = new HashSet(); // Set, HashSet ignored due to default excludedClasses property Map map = new HashMap(); // Map, HashMap ignored due to default excludedClasses property Date date = new Date(); // Counted, 1 Time time = new Time(); // Counted, 2 // mention of 18 other user defined classes Place place = new Place(); // violation, total is 21 }
To configure the check with a threshold of 2:
<module name="Checker"> <module name="TreeWalker"> <module name="ClassFanOutComplexity"> <property name="max" value="2"/> </module> </module> </module>
Example:
The check passes without violations in the following:
// A is ignored because the superclass doesn't depend on the permitted subclasses sealed class InputClassComplexity permits A { Set set = new HashSet(); // Set, HashSet ignored due to default excludedClasses property Map map = new HashMap(); // Map, HashMap ignored due to default excludedClasses property Date date = new Date(); // Counted, 1 Time time = new Time(); // Counted, 2 } final class A extends InputClassComplexity { } // InputClassComplexity is counted, 1
The check results in a violation in the following:
// A is ignored because the superclass doesn't depend on the permitted subclasses sealed class InputClassComplexity permits A { Set set = new HashSet(); // Set, HashSet ignored due to default excludedClasses property Map map = new HashMap(); // Map, HashMap ignored due to default excludedClasses property Date date = new Date(); // Counted, 1 Time time = new Time(); // Counted, 2 Place place = new Place(); // violation, total is 3 } final class A extends InputClassComplexity { } // InputClassComplexity is counted, 1
To configure the check with three excluded classes HashMap
,
HashSet
and Place
:
<module name="Checker"> <module name="TreeWalker"> <module name="ClassFanOutComplexity"> <property name="excludedClasses" value="HashMap, HashSet, Place"/> </module> </module> </module>
Example:
The check passes without violations in the following:
class InputClassComplexity { Set set = new HashSet(); // Set counted 1, HashSet ignored Map map = new HashMap(); // Map counted 2, HashMap ignored Date date = new Date(); // Counted, 3 Time time = new Time(); // Counted, 4 // mention of 16 other user defined classes Place place = new Place(); // Ignored }
The check results in a violation in the following:
class InputClassComplexity { Set set = new HashSet(); // Set counted 1, HashSet ignored Map map = new HashMap(); // Map counted 2, HashMap ignored Date date = new Date(); // Counted, 3 Time time = new Time(); // Counted, 4 // mention of 16 other user defined classes Space space = new Space(); // violation, total is 21 }
To configure the check to exclude classes with a regular expression
.*Reader$
:
<module name="Checker"> <module name="TreeWalker"> <module name="ClassFanOutComplexity"> <property name="excludeClassesRegexps" value=".*Reader$"/> </module> </module> </module>
Example:
The check passes without violations in the following:
class InputClassComplexity { Set set = new HashSet(); // Set, HashSet ignored due to default excludedClasses property Map map = new HashMap(); // Map, HashMap ignored due to default excludedClasses property Date date = new Date(); // Counted, 1 Time time = new Time(); // Counted, 2 // mention of 18 other user defined classes BufferedReader br; // Ignored }
The check results in a violation in the following:
class InputClassComplexity { Set set = new HashSet(); // Set, HashSet ignored due to default excludedClasses property Map map = new HashMap(); // Map, HashMap ignored due to default excludedClasses property Date date = new Date(); // Counted, 1 Time time = new Time(); // Counted, 2 // mention of 18 other user defined classes File file; // violation, total is 21 }
To configure the check with an excluded package java.io
:
<module name="Checker"> <module name="TreeWalker"> <module name="ClassFanOutComplexity"> <property name="excludedPackages" value="java.io"/> </module> </module> </module>
Example:
The check passes without violations in the following:
import java.io.BufferedReader; class InputClassComplexity { Set set = new HashSet(); // Set, HashSet ignored due to default excludedClasses property Map map = new HashMap(); // Map, HashMap ignored due to default excludedClasses property Date date = new Date(); // Counted, 1 Time time = new Time(); // Counted, 2 // mention of 18 other user defined classes BufferedReader br; // Ignored }
The check results in a violation in the following:
import java.util.StringTokenizer; class InputClassComplexity { Set set = new HashSet(); // Set, HashSet ignored due to default excludedClasses property Map map = new HashMap(); // Map, HashMap ignored due to default excludedClasses property Date date = new Date(); // Counted, 1 Time time = new Time(); // Counted, 2 // mention of 18 other user defined classes StringTokenizer st; // violation, total is 21 }
Override property excludedPackages
to mark some packages as excluded.
Each member of excludedPackages
should be a valid identifier:
java.util
- valid, excludes all classes inside java.util
,
but not from the subpackages.
java.util.
- invalid, should not end with a dot.
java.util.*
- invalid, should not end with a star.
Note, that checkstyle will ignore all classes from the java.lang
package and its subpackages, even if the java.lang
was not listed in
the excludedPackages
parameter.
Also note, that excludedPackages
will not exclude classes, imported via
wildcard (e.g. import java.math.*
). Instead of wildcard import you should
use direct import (e.g. import java.math.BigDecimal
).
Also note, that checkstyle will not exclude classes within the same file
even if it was listed in the excludedPackages
parameter. For example,
assuming the config is
<module name="Checker"> <module name="TreeWalker"> <module name="ClassFanOutComplexity"> <property name="excludedPackages" value="a.b"/> </module> </module> </module>
And the file a.b.Foo.java
is:
package a.b; import a.b.Bar; import a.b.c.Baz; class Foo { Bar bar; // Will be ignored, located inside ignored a.b package Baz baz; // Will not be ignored, located inside a.b.c package Data data; // Will not be ignored, same file class Data { Foo foo; // Will not be ignored, same file } }
The bar
member will not be counted,
since the a.b
added to the excludedPackages
.
The baz
member will be counted,
since the a.b.c
was not added to the excludedPackages
.
The data
and foo
members will be counted, as they are
inside same file.
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.metrics