ClassDataAbstractionCoupling

Since Checkstyle 3.4

Description

Measures the number of distinct classes that are instantiated 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:

  1. Iterates over the list of tokens (defined below) and counts all mentioned classes.
  2. 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 the excludedPackages parameter, the class does not increase complexity.
  3. If a class name was added to the excludedClasses parameter, the class does not increase complexity.

Properties

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

Examples

To configure the check:


<module name="Checker">
  <module name="TreeWalker">
    <module name="ClassDataAbstractionCoupling"/>
  </module>
</module>

Example:

The check passes without violations in the following:


// violation below, "Class Data Abstraction Coupling is 9 (max allowed is 7)."
public class Example1 {
  private Set<Object> set = new HashSet<>();         // ok, ignored
  private Map<Object, Object> map = new HashMap<>(); // ok, ignored
  private Object object = new Object();              // ok, ignored

  private AtomicInteger atomicInteger = new AtomicInteger();
  private BigInteger bigInteger = new BigInteger("0");
  private BigDecimal bigDecimal = new BigDecimal("0");
  private MathContext mathContext = new MathContext(0);

  private Example1 example1 = new Example1(); // ok, ignored
  private UseCase1 useCase1 = new UseCase1();

  private ByteArrayInputStream byteArrayInputStream =
          new ByteArrayInputStream(new byte[1]);
  private CharArrayWriter charArrayWriter = new CharArrayWriter();

  private PipedReader pipedReader = new PipedReader();
  private BufferedReader bufferedReader = new BufferedReader(pipedReader);
}

To configure the check with a threshold of 2:


<module name="Checker">
  <module name="TreeWalker">
    <module name="ClassDataAbstractionCoupling">
      <property name="max" value="9"/>
    </module>
  </module>
</module>

Example:

The check passes without violations in the following:


// violation below, "Class Data Abstraction Coupling is 10 (max allowed is 9)."
public class Example3 {
  private Set<Object> set = new HashSet<>();         // ok, ignored
  private Map<Object, Object> map = new HashMap<>(); // ok, ignored
  private Object object = new Object();              // ok, ignored

  private AtomicInteger atomicInteger = new AtomicInteger();
  private BigInteger bigInteger = new BigInteger("0");
  private BigDecimal bigDecimal = new BigDecimal("0");
  private MathContext mathContext = new MathContext(0);

  private Example1 example1 = new Example1(); // ok, ignored
  private UseCase1 useCase1 = new UseCase1();

  private ByteArrayInputStream byteArrayInputStream =
          new ByteArrayInputStream(new byte[1]);
  private CharArrayWriter charArrayWriter = new CharArrayWriter();

  private PipedReader pipedReader = new PipedReader();
  private BufferedReader bufferedReader = new BufferedReader(pipedReader);
}

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:


// violation below, "Class Data Abstraction Coupling is 11 (max allowed is 7)."
public class Example5 {
  private Set<Object> set = new HashSet<>();         // ok, ignored
  private Map<Object, Object> map = new HashMap<>(); // ok, ignored
  private Object object = new Object();              // ok, ignored

  private AtomicInteger atomicInteger = new AtomicInteger();
  private BigInteger bigInteger = new BigInteger("0");
  private BigDecimal bigDecimal = new BigDecimal("0");
  private MathContext mathContext = new MathContext(0);

  private Example1 example1 = new Example1();        // ok, ignored
  private UseCase1 useCase1 = new UseCase1();

  private ByteArrayInputStream byteArrayInputStream =
          new ByteArrayInputStream(new byte[1]);
  private CharArrayWriter charArrayWriter = new CharArrayWriter();

  private PipedReader pipedReader = new PipedReader();
  private BufferedReader bufferedReader = new BufferedReader(pipedReader);
}

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:


// violation below, "Class Data Abstraction Coupling is 8 (max allowed is 7)."
public class Example7 {
  private Set<Object> set = new HashSet<>();         // ok, ignored
  private Map<Object, Object> map = new HashMap<>(); // ok, ignored
  private Object object = new Object();              // ok, ignored

  private AtomicInteger atomicInteger = new AtomicInteger();
  private BigInteger bigInteger = new BigInteger("0");
  private BigDecimal bigDecimal = new BigDecimal("0");
  private MathContext mathContext = new MathContext(0);

  private Example1 example1 = new Example1();        // ok, ignored
  private UseCase1 useCase1 = new UseCase1();

  private ByteArrayInputStream byteArrayInputStream =
      new ByteArrayInputStream(new byte[1]);
  private CharArrayWriter charArrayWriter = new CharArrayWriter();

  private PipedReader pipedReader = new PipedReader(); // ok, ignored
  private BufferedReader bufferedReader =
      new BufferedReader(pipedReader); // ok, ignored
}

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:


public class Example9 {
  private Set<Object> set = new HashSet<>();         // ok, ignored
  private Map<Object, Object> map = new HashMap<>(); // ok, ignored
  private Object object = new Object();              // ok, ignored

  private AtomicInteger atomicInteger = new AtomicInteger();
  private BigInteger bigInteger = new BigInteger("0");
  private BigDecimal bigDecimal = new BigDecimal("0");
  private MathContext mathContext = new MathContext(0);

  private Example1 example1 = new Example1(); // ok, ignored
  private UseCase1 useCase1 = new UseCase1();

  private ByteArrayInputStream byteArrayInputStream =
      new ByteArrayInputStream(new byte[1]); // ok, ignored
  private CharArrayWriter charArrayWriter = new CharArrayWriter(); // ok, ignored

  private PipedReader pipedReader = new PipedReader(); // ok, ignored
  private BufferedReader bufferedReader =
      new BufferedReader(pipedReader); // ok, ignored
}

Use Cases

To configure the check:


<module name="Checker">
  <module name="TreeWalker">
    <module name="ClassDataAbstractionCoupling"/>
  </module>
</module>

The check results in a violation in the following:


// violation below, "Class Data Abstraction Coupling is 8 (max allowed is 7)."
public class UseCase1 {
  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();
  UseCase2 useCase2 = new UseCase2();
  Example5 example5 = new Example5();
  UseCase3 useCase3 = new UseCase3();
  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>

The check results in a violation in the following:


// violation below, "Class Data Abstraction Coupling is 3 (max allowed is 2)."
public class UseCase2 {
  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>

The check results in a violation in the following:


// violation below, "Class Data Abstraction Coupling is 8 (max allowed is 7)."
public class UseCase3 {
  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");
  UseCase1 useCase1 = new UseCase1();
  Example3 example3 = new Example3();
  UseCase2 useCase2 = new UseCase2();
  Example5 example5 = new Example5();
  Example7 example7 = new Example7();
  UseCase4 useCase4 = new UseCase4(); // 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>

The check results in a violation in the following:


// violation below, "Class Data Abstraction Coupling is 8 (max allowed is 7)."
public class UseCase4 {
  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>

The check results in a violation in the following:


import com.puppycrawl.tools.checkstyle.checks.metrics.classdataabstractioncoupling.UseCase1;
import com.puppycrawl.tools.checkstyle.checks.metrics.classdataabstractioncoupling.Example3;
import com.puppycrawl.tools.checkstyle.checks.metrics.classdataabstractioncoupling.ignore.deeper.UseCase2;
import com.puppycrawl.tools.checkstyle.checks.metrics.classdataabstractioncoupling.ignore.deeper.UseCase3;

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 UseCase5 {
  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);
  UseCase1 example3 = new UseCase1();
  Example3 example4 = new Example3();
  UseCase2 example5 = new UseCase2();
  UseCase3 useCase3 = new UseCase3(); // 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).


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.UseCase4;
import com.puppycrawl.tools.checkstyle.checks.metrics.classdataabstractioncoupling.ignore.Example9;
import com.puppycrawl.tools.checkstyle.checks.metrics.classdataabstractioncoupling.ignore.deeper.UseCase2;
import com.puppycrawl.tools.checkstyle.checks.metrics.classdataabstractioncoupling.ignore.deeper.Example5;
import com.puppycrawl.tools.checkstyle.checks.metrics.classdataabstractioncoupling.ignore.deeper.UseCase3;

class UseCase6 {
  // Ignored, located inside classdataabstractioncoupling.ignore package
  Example7 example7 = new Example7();
  UseCase4 useCase4 = new UseCase4();
  Example9 example9 = new Example9();

  // Counted, located outside of classdataabstractioncoupling.ignore package
  Example1 example1 = new Example1();
  UseCase1 useCase1 = new UseCase1();
  Example3 example3 = new Example3();
  UseCase2 useCase2 = new UseCase2();
  Example5 example5 = new Example5();
  UseCase3 useCase3 = new UseCase3();
  Data data = new Data(); // Counted 7

  class Data {
    public Data() {
      UseCase6 useCase6 = new UseCase6(); // 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.

Example of Usage

Violation Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Fully Qualified Name

com.puppycrawl.tools.checkstyle.checks.metrics.ClassDataAbstractionCouplingCheck

Use this fully qualified class name in configuration when an exact class reference is required.

Parent Module

TreeWalker