Class ClassDataAbstractionCouplingCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.api.AutomaticBean
-
- com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
-
- com.puppycrawl.tools.checkstyle.api.AbstractCheck
-
- com.puppycrawl.tools.checkstyle.checks.metrics.AbstractClassCouplingCheck
-
- com.puppycrawl.tools.checkstyle.checks.metrics.ClassDataAbstractionCouplingCheck
-
- All Implemented Interfaces:
Configurable
,Contextualizable
public final class ClassDataAbstractionCouplingCheck extends AbstractClassCouplingCheck
This metric measures the number of instantiations of other classes within the given class. 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 theexcludedPackages
parameter, the class does not increase complexity. -
If a class name was added to the
excludedClasses
parameter, the class does not increase complexity.
-
Property
max
- Specify the maximum threshold allowed. Default value is7
. -
Property
excludedClasses
- Specify user-configured class names to ignore. Default value isArrayIndexOutOfBoundsException, ArrayList, Boolean, Byte, Character, Class, Deprecated, Deque, Double, Exception, Float, FunctionalInterface, HashMap, HashSet, IllegalArgumentException, IllegalStateException, IndexOutOfBoundsException, Integer, LinkedList, List, Long, Map, NullPointerException, Object, Override, Queue, RuntimeException, SafeVarargs, SecurityException, Set, Short, SortedMap, SortedSet, String, StringBuffer, StringBuilder, SuppressWarnings, Throwable, TreeMap, TreeSet, UnsupportedOperationException, Void, boolean, byte, char, double, float, int, long, short, void
. -
Property
excludeClassesRegexps
- Specify user-configured regular expressions to ignore classes. Default value is^$
. -
Property
excludedPackages
- Specify user-configured packages to ignore. Default value is{}
.
To configure the check:
<module name="ClassDataAbstractionCoupling"/>
To configure the check with a threshold of 5:
<module name="ClassDataAbstractionCoupling"> <property name="max" value="5"/> </module>
To configure the check with two excluded classes
HashMap
andHashSet
:<module name="ClassDataAbstractionCoupling"> <property name="excludedClasses" value="HashMap, HashSet"/> </module>
To configure the check with two regular expressions
^Array.*
and.*Exception$
:<module name="ClassDataAbstractionCoupling"> <property name="excludeClassesRegexps" value="^Array.*, .*Exception$"/> </module>
The following example demonstrates usage of excludedClasses and excludeClassesRegexps properties
Expected result is one class
Date
<module name="ClassDataAbstractionCoupling"> <property name="excludedClasses" value="ArrayList"/> <property name="excludeClassesRegexps" value="^Hash.*"/> </module>
public class InputClassCoupling { public Set _set = new HashSet(); public Map _map = new HashMap(); public List<String> _list = new ArrayList<>(); public Date _date = new Date(); }
To configure the check with two excluded classes
HashMap
andHashSet
:<module name="ClassDataAbstractionCoupling"> <property name="excludedClasses" value="HashMap, HashSet"/> </module>
To configure the check with two regular expressions
^Array.*
and.*Exception$
:<module name="ClassDataAbstractionCoupling"> <property name="excludeClassesRegexps" value="^Array.*, .*Exception$"/> </module>
The following example demonstrates usage of excludedClasses and excludeClassesRegexps properties
Expected result is one class
Date
<module name="ClassDataAbstractionCoupling"> <property name="excludedClasses" value="ArrayList"/> <property name="excludeClassesRegexps" value="^Hash.*"/> </module>
public class InputClassCoupling { public Set _set = new HashSet(); public Map _map = new HashMap(); public List<String> _list = new ArrayList<>(); public Date _date = new Date(); }
Override property
excludedPackages
to mark some packages as excluded. Each member ofexcludedPackages
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 thejava.lang
was not listed in theexcludedPackages
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; public class Foo { public Bar bar; // Will be ignored, located inside ignored a.b package public Baz baz; // Will not be ignored, located inside a.b.c package public Data data; // Will not be ignored, same file class Data { public Foo foo; // Will not be ignored, same file } }
The
bar
member will not be counted, since thea.b
added to theexcludedPackages
. Thebaz
member will be counted, since thea.b.c
was not added to theexcludedPackages
. Thedata
andfoo
members will be counted, as they are inside same file.Example of usage:
<module name="ClassDataAbstractionCoupling"> <property name="excludedPackages" value="java.util, java.math"/> </module>
- Since:
- 3.4
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
AutomaticBean.OutputStreamOptions
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
MSG_KEY
A key is pointing to the warning message text in "messages.properties" file.
-
Constructor Summary
Constructors Constructor Description ClassDataAbstractionCouplingCheck()
Creates bew instance of the check.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int[]
getAcceptableTokens()
The configurable token set.protected java.lang.String
getLogMessageId()
Returns message key we use for log violations.int[]
getRequiredTokens()
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, visitToken
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
clearMessages, destroy, finishTree, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, init, isCommentNodesRequired, log, log, log, setFileContents, setTabWidth, setTokens
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
configure, contextualize, getConfiguration, setupChild
-
-
-
-
Field Detail
-
MSG_KEY
public static final java.lang.String MSG_KEY
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
-
Method Detail
-
getRequiredTokens
public int[] getRequiredTokens()
Description copied from class:AbstractCheck
The tokens that this check must be registered for.- Specified by:
getRequiredTokens
in classAbstractCheck
- Returns:
- the token set this must be registered for.
- See Also:
TokenTypes
-
getAcceptableTokens
public int[] getAcceptableTokens()
Description copied from class:AbstractCheck
The 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:
getAcceptableTokens
in classAbstractCheck
- Returns:
- the token set this check is designed for.
- See Also:
TokenTypes
-
getLogMessageId
protected java.lang.String getLogMessageId()
Description copied from class:AbstractClassCouplingCheck
Returns message key we use for log violations.- Specified by:
getLogMessageId
in classAbstractClassCouplingCheck
- Returns:
- message key we use for log violations.
-
-