Class ClassDataAbstractionCouplingCheck
- All Implemented Interfaces:
Configurable,Contextualizable
Measures the number of instantiations of other classes within the given class or record. This type of coupling is not caused by inheritance or the object oriented paradigm. Generally speaking, any data type with other data types as members or local variable that is an instantiation (object) of another class has data abstraction coupling (DAC). The higher the DAC, the more complex the structure of the class.
This check processes files in the following way:
- Iterates over the list of tokens (defined below) and counts all mentioned classes.
-
If a class was imported with direct import (i.e.
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 theexcludedPackagesparameter, the class does not increase complexity. -
If a class name was added to the
excludedClassesparameter, the class does not increase complexity.
-
Property
max- Specify the maximum threshold allowed. Type isint. Default value is7. -
Property
excludedClasses- Specify user-configured class names to ignore. Type isjava.lang.String[]. Default value isArrayIndexOutOfBoundsException, 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. -
Property
excludeClassesRegexps- Specify user-configured regular expressions to ignore classes. Type isjava.util.regex.Pattern[]. Default value is^$. -
Property
excludedPackages- Specify user-configured packages to ignore. Type isjava.lang.String[]. Default value is"".
To configure the check:
<module name="ClassDataAbstractionCoupling"/>
Example:
The check passes without violations in the following:
class InputClassCoupling {
Set set = new HashSet(); // HashSet ignored due to default excludedClasses property
Map map = new HashMap(); // 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
}
The check results in a violation in the following:
class InputClassCoupling {
Set set = new HashSet(); // HashSet ignored due to default excludedClasses property
Map map = new HashMap(); // HashMap ignored due to default excludedClasses property
Date date = new Date(); // Counted, 1
Time time = new Time(); // Counted, 2
// instantiation of 5 other user defined classes
Place place = new Place(); // violation, total is 8
}
To configure the check with a threshold of 2:
<module name="ClassDataAbstractionCoupling"> <property name="max" value="2"/> </module>
Example:
The check passes without violations in the following:
class InputClassCoupling {
Set set = new HashSet(); // HashSet ignored due to default excludedClasses property
Map map = new HashMap(); // HashMap ignored due to default excludedClasses property
Date date = new Date(); // Counted, 1
Time time = new Time(); // Counted, 2
}
The check results in a violation in the following:
class InputClassCoupling {
Set set = new HashSet(); // HashSet ignored due to default excludedClasses property
Map map = new HashMap(); // 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
}
To configure the check with three excluded classes HashMap,
HashSet and Place:
<module name="ClassDataAbstractionCoupling"> <property name="excludedClasses" value="HashMap, HashSet, Place"/> </module>
Example:
The check passes without violations in the following:
class InputClassCoupling {
Set set = new HashSet(); // Ignored
Map map = new HashMap(); // Ignored
Date date = new Date(); // Counted, 1
Time time = new Time(); // Counted, 2
// instantiation of 5 other user defined classes
Place place = new Place(); // Ignored
}
The check results in a violation in the following:
class InputClassCoupling {
Set set = new HashSet(); // Ignored
Map map = new HashMap(); // Ignored
Date date = new Date(); // Counted, 1
Time time = new Time(); // Counted, 2
// instantiation of 5 other user defined classes
Space space = new Space(); // violation, total is 8
}
To configure the check to exclude classes with a regular expression
.*Reader$:
<module name="ClassDataAbstractionCoupling"> <property name="excludeClassesRegexps" value=".*Reader$"/> </module>
Example:
The check passes without violations in the following:
class InputClassCoupling {
Set set = new HashSet(); // HashSet ignored due to default excludedClasses property
Map map = new HashMap(); // HashMap ignored due to default excludedClasses property
Date date = new Date(); // Counted, 1
Time time = new Time(); // Counted, 2
// instantiation of 5 other user defined classes
BufferedReader br = new BufferedReader(); // Ignored
}
The check results in a violation in the following:
class InputClassCoupling {
Set set = new HashSet(); // HashSet ignored due to default excludedClasses property
Map map = new HashMap(); // HashMap ignored due to default excludedClasses property
Date date = new Date(); // Counted, 1
Time time = new Time(); // Counted, 2
// instantiation of 5 other user defined classes
File file = new File(); // violation, total is 8
}
To configure the check with an excluded package java.io:
<module name="ClassDataAbstractionCoupling"> <property name="excludedPackages" value="java.io"/> </module>
Example:
The check passes without violations in the following:
import java.io.BufferedReader;
class InputClassCoupling {
Set set = new HashSet(); // HashSet ignored due to default excludedClasses property
Map map = new HashMap(); // HashMap ignored due to default excludedClasses property
Date date = new Date(); // Counted, 1
Time time = new Time(); // Counted, 2
// instantiation of 5 other user defined classes
BufferedReader br = new BufferedReader(); // Ignored
}
The check results in a violation in the following:
import java.util.StringTokenizer;
class InputClassCoupling {
Set set = new HashSet(); // HashSet ignored due to default excludedClasses property
Map map = new HashMap(); // HashMap ignored due to default excludedClasses property
Date date = new Date(); // Counted, 1
Time time = new Time(); // Counted, 2
// instantiation of 5 other user defined classes
StringTokenizer st = new StringTokenizer(); // violation, total is 8
}
Override property excludedPackages to mark some packages as excluded.
Each member of excludedPackages should be a valid identifier:
-
java.util- valid, excludes all classes insidejava.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="ClassDataAbstractionCoupling"> <property name="excludedPackages" value="a.b"/> </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.
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
-
classDataAbstractionCoupling
- Since:
- 3.4
-
Nested Class Summary
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
AutomaticBean.OutputStreamOptions -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final intDefault allowed complexity.static final StringA key is pointing to the warning message text in "messages.properties" file. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionint[]The configurable token set.protected StringReturns message key we use for log violations.int[]The tokens that this check must be registered for.Methods inherited from class com.puppycrawl.tools.checkstyle.checks.metrics.AbstractClassCouplingCheck
beginTree, getDefaultTokens, leaveToken, setExcludeClassesRegexps, setExcludedClasses, setExcludedPackages, setMax, visitTokenMethods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
clearViolations, destroy, finishTree, getFileContents, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, log, log, log, setFileContents, setTabWidth, setTokensMethods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityMethods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
configure, contextualize, getConfiguration, setupChild
-
Field Details
-
MSG_KEY
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
DEFAULT_MAX
private static final int DEFAULT_MAXDefault allowed complexity.- See Also:
-
-
Constructor Details
-
ClassDataAbstractionCouplingCheck
public ClassDataAbstractionCouplingCheck()Creates bew instance of the check.
-
-
Method Details
-
getRequiredTokens
public int[] getRequiredTokens()Description copied from class:AbstractCheckThe tokens that this check must be registered for.- Specified by:
getRequiredTokensin classAbstractCheck- Returns:
- the token set this must be registered for.
- See Also:
-
getAcceptableTokens
public int[] getAcceptableTokens()Description copied from class:AbstractCheckThe configurable token set. Used to protect Checks against malicious users who specify an unacceptable token set in the configuration file. The default implementation returns the check's default tokens.- Specified by:
getAcceptableTokensin classAbstractCheck- Returns:
- the token set this check is designed for.
- See Also:
-
getLogMessageId
Description copied from class:AbstractClassCouplingCheckReturns message key we use for log violations.- Specified by:
getLogMessageIdin classAbstractClassCouplingCheck- Returns:
- message key we use for log violations.
-