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 | 7 |
3.4 |
To configure the check:
<module name="Checker"> <module name="TreeWalker"> <module name="ClassDataAbstractionCoupling"/> </module> </module>
Example:
The check passes without violations in the following:
public class Example1 { private Set<Object> set = new HashSet<>(); // Ignored by default private Map<Object,Object> map = new HashMap<>(); // Ignored by default private Object object = new Object(); // Ignored by default private Instant instant = Instant.now(); // Counted 1 private LocalTime localTime = LocalTime.now(); // Counted 2 }
The check results in a violation in the following:
// violation below, "Class Data Abstraction Coupling is 8 (max allowed is 7)." public class Example2 { Set set = new HashSet(); // Ignored by default Map map = new HashMap(); // Ignored by default AtomicInteger atomicInteger = new AtomicInteger(); // Counted 1 BigInteger bigInteger = new BigInteger("0"); Example1 example1 = new Example1(); Example3 example3 = new Example3(); Example4 example4 = new Example4(); Example5 example5 = new Example5(); Example6 example6 = new Example6(); Example7 example7 = new Example7(); // Counted 8 }
To configure the check with a threshold of 2:
<module name="Checker"> <module name="TreeWalker"> <module name="ClassDataAbstractionCoupling"> <property name="max" value="2"/> </module> </module> </module>
Example:
The check passes without violations in the following:
public class Example3 { Set set = new HashSet(); // Ignored by default Map map = new HashMap(); // Ignored by default Instant instant = Instant.now(); // Counted 1 LocalTime localTime = LocalTime.now(); // Counted 2 }
The check results in a violation in the following:
// violation below, "Class Data Abstraction Coupling is 3 (max allowed is 2)." public class Example4 { Set set = new HashSet(); // Ignored by default Map map = new HashMap(); // Ignored by default AtomicInteger atomicInteger = new AtomicInteger(); // Counted 1 BigInteger bigInteger = new BigInteger("0"); BigDecimal bigDecimal = new BigDecimal("0"); // Counted 3 }
To configure the check with three excluded classes HashMap
,
HashSet
and Example1
:
<module name="Checker"> <module name="TreeWalker"> <module name="ClassDataAbstractionCoupling"> <property name="excludedClasses" value="HashMap, HashSet, Example1"/> </module> </module> </module>
Example:
The check passes without violations in the following:
public class Example5 { Set set = new HashSet(); // Ignored 1 Map map = new HashMap(); // Ignored 2 Example1 example1 = new Example1(); // Ignore 3 AtomicInteger atomicInteger = new AtomicInteger(); // Counted 1 BigInteger bigInteger = new BigInteger("0"); Example2 example2 = new Example2(); Example3 example3 = new Example3(); Example4 example4 = new Example4(); Example6 example6 = new Example6(); Example7 example7 = new Example7(); // Counted 7 }
The check results in a violation in the following:
// violation below, "Class Data Abstraction Coupling is 8 (max allowed is 7)." public class Example6 { Set set = new HashSet(); // Ignored by default Map map = new HashMap(); // Ignored by default AtomicInteger atomicInteger = new AtomicInteger(); // Counted 1 BigInteger bigInteger = new BigInteger("0"); Example2 example2 = new Example2(); Example3 example3 = new Example3(); Example4 example4 = new Example4(); Example5 example5 = new Example5(); Example7 example7 = new Example7(); Example8 example8 = new Example8(); // Counted 8 }
To configure the check to exclude classes with a regular expression
.*Reader$
:
<module name="Checker"> <module name="TreeWalker"> <module name="ClassDataAbstractionCoupling"> <property name="excludeClassesRegexps" value=".*Reader$"/> </module> </module> </module>
Example:
The check passes without violations in the following:
public class Example7 { Set set = new HashSet(); // Ignored by default Map map = new HashMap(); // Ignored by default AtomicInteger atomicInteger = new AtomicInteger(); // Counted 1 BigInteger bigInteger = new BigInteger("0"); Example1 example1 = new Example1(); Example2 example2 = new Example2(); Example3 example3 = new Example3(); Example4 example4 = new Example4(); Example5 example5 = new Example5(); // Counted 7 // Ignored using module excludeClassesRegexps property BufferedReader bufferedReader = new BufferedReader(new PipedReader()); }
The check results in a violation in the following:
// violation below, "Class Data Abstraction Coupling is 8 (max allowed is 7)." public class Example8 { Set set = new HashSet(); // Ignored by default Map map = new HashMap(); // Ignored by default AtomicInteger atomicInteger = new AtomicInteger(); // Counted 1 BigInteger bigInteger = new BigInteger("0"); BigDecimal bigDecimal = new BigDecimal("0"); MathContext mathContext = new MathContext(0); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(new byte[1]); CharArrayWriter charArrayWriter = new CharArrayWriter(); StringWriter stringWriter = new StringWriter(); File file = new File("path"); // Counted 8 }
To configure the check with an excluded package java.io
:
<module name="Checker"> <module name="TreeWalker"> <module name="ClassDataAbstractionCoupling"> <property name="excludedPackages" value="java.io"/> </module> </module> </module>
Example:
The check passes without violations in the following:
import java.io.BufferedReader; public class Example9 { Set set = new HashSet(); // Ignored by default Map map = new HashMap(); // Ignored by default AtomicInteger atomicInteger = new AtomicInteger(); // Counted 1 BigInteger bigInteger = new BigInteger("0"); BigDecimal bigDecimal = new BigDecimal("0"); MathContext mathContext = new MathContext(0); // Counted 4 // Ignored using module excludedPackages property ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(new byte[1]); CharArrayWriter charArrayWriter = new CharArrayWriter(); PipedReader pipedReader = new PipedReader(); BufferedReader bufferedReader = new BufferedReader(pipedReader); }
The check results in a violation in the following:
import com.puppycrawl.tools.checkstyle.checks.metrics.classdataabstractioncoupling.Example2; import com.puppycrawl.tools.checkstyle.checks.metrics.classdataabstractioncoupling.Example3; import com.puppycrawl.tools.checkstyle.checks.metrics.classdataabstractioncoupling.ignore.deeper.Example4; import com.puppycrawl.tools.checkstyle.checks.metrics.classdataabstractioncoupling.ignore.deeper.Example6; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.CharArrayWriter; import java.io.PipedReader; import java.math.BigDecimal; import java.math.BigInteger; import java.math.MathContext; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.concurrent.atomic.AtomicInteger; // violation below, "Class Data Abstraction Coupling is 8 (max allowed is 7)." class Example10 { Set set = new HashSet(); // Ignored by default Map map = new HashMap(); // Ignored by default AtomicInteger atomicInteger = new AtomicInteger(); // Counted 1 BigInteger bigInteger = new BigInteger("0"); BigDecimal bigDecimal = new BigDecimal("0"); MathContext mathContext = new MathContext(0); Example2 example3 = new Example2(); Example3 example4 = new Example3(); Example4 example5 = new Example4(); Example6 example6 = new Example6(); // Counted 8 // Ignored using module excludedPackages property ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(new byte[1]); PipedReader pipedReader = new PipedReader(); BufferedReader bufferedReader = new BufferedReader(pipedReader); CharArrayWriter charArrayWriter = new CharArrayWriter(); }
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="ClassDataAbstractionCoupling"> <property name="excludedPackages" value="com.puppycrawl.tools.checkstyle.checks.metrics.classdataabstractioncoupling.ignore"/> </module> </module> </module>
And the file a.b.Foo.java
is:
import com.puppycrawl.tools.checkstyle.checks.metrics.classdataabstractioncoupling.ignore.Example7; import com.puppycrawl.tools.checkstyle.checks.metrics.classdataabstractioncoupling.ignore.Example8; import com.puppycrawl.tools.checkstyle.checks.metrics.classdataabstractioncoupling.ignore.Example9; import com.puppycrawl.tools.checkstyle.checks.metrics.classdataabstractioncoupling.ignore.deeper.Example4; import com.puppycrawl.tools.checkstyle.checks.metrics.classdataabstractioncoupling.ignore.deeper.Example5; import com.puppycrawl.tools.checkstyle.checks.metrics.classdataabstractioncoupling.ignore.deeper.Example6; class Example11 { // Ignored, located inside classdataabstractioncoupling.ignore package Example7 example7 = new Example7(); Example8 example8 = new Example8(); Example9 example9 = new Example9(); // Counted, located outside of classdataabstractioncoupling.ignore package Example1 example1 = new Example1(); Example2 example2 = new Example2(); Example3 example3 = new Example3(); Example4 example4 = new Example4(); Example5 example5 = new Example5(); Example6 example6 = new Example6(); Data data = new Data(); // Counted 7 class Data { public Data() { Example11 example11 = new Example11(); // 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