001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2026 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018/////////////////////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.checks.metrics; 021 022import java.util.regex.Pattern; 023 024import com.puppycrawl.tools.checkstyle.api.TokenTypes; 025 026/** 027 * <div> 028 * Measures the number of distinct classes that are instantiated 029 * within the given class or record. This type of coupling is not caused by inheritance or 030 * the object-oriented paradigm. Generally speaking, any data type with other 031 * data types as members or local variable that is an instantiation (object) 032 * of another class has data abstraction coupling (DAC). The higher the DAC, 033 * the more complex the structure of the class. 034 * </div> 035 * 036 * <p> 037 * This check processes files in the following way: 038 * </p> 039 * <ol> 040 * <li> 041 * Iterates over the list of tokens (defined below) and counts all mentioned classes. 042 * <ul> 043 * <li> 044 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#IMPORT"> 045 * PACKAGE_DEF</a> 046 * </li> 047 * <li> 048 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#IMPORT"> 049 * IMPORT</a> 050 * </li> 051 * <li> 052 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CLASS_DEF"> 053 * CLASS_DEF</a> 054 * </li> 055 * <li> 056 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#INTERFACE_DEF"> 057 * INTERFACE_DEF</a> 058 * </li> 059 * <li> 060 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ENUM_DEF"> 061 * ENUM_DEF</a> 062 * </li> 063 * <li> 064 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_NEW"> 065 * LITERAL_NEW</a> 066 * </li> 067 * <li> 068 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#RECORD_DEF"> 069 * RECORD_DEF</a> 070 * </li> 071 * </ul> 072 * </li> 073 * <li> 074 * If a class was imported with direct import (i.e. {@code import java.math.BigDecimal}), 075 * or the class was referenced with the package name (i.e. {@code java.math.BigDecimal value}) 076 * and the package was added to the {@code excludedPackages} parameter, the class 077 * does not increase complexity. 078 * </li> 079 * <li> 080 * If a class name was added to the {@code excludedClasses} parameter, 081 * the class does not increase complexity. 082 * </li> 083 * </ol> 084 * 085 * @since 3.4 086 */ 087public final class ClassDataAbstractionCouplingCheck 088 extends AbstractClassCouplingCheck { 089 090 /** 091 * A key is pointing to the warning message text in "messages.properties" file. 092 */ 093 public static final String MSG_KEY = "classDataAbstractionCoupling"; 094 095 /** Default allowed complexity. */ 096 private static final int DEFAULT_MAX = 7; 097 098 /** Creates bew instance of the check. */ 099 public ClassDataAbstractionCouplingCheck() { 100 super(DEFAULT_MAX); 101 } 102 103 @Override 104 public int[] getRequiredTokens() { 105 return new int[] { 106 TokenTypes.PACKAGE_DEF, 107 TokenTypes.IMPORT, 108 TokenTypes.CLASS_DEF, 109 TokenTypes.INTERFACE_DEF, 110 TokenTypes.ENUM_DEF, 111 TokenTypes.LITERAL_NEW, 112 TokenTypes.RECORD_DEF, 113 }; 114 } 115 116 @Override 117 public int[] getAcceptableTokens() { 118 return getRequiredTokens(); 119 } 120 121 @Override 122 protected String getLogMessageId() { 123 return MSG_KEY; 124 } 125 126 /** 127 * Setter to specify user-configured regular expressions to ignore classes. 128 * 129 * @param from array representing regular expressions of classes to ignore. 130 * @propertySince 7.7 131 */ 132 @Override 133 public void setExcludeClassesRegexps(Pattern... from) { 134 super.setExcludeClassesRegexps(from); 135 } 136 137 /** 138 * Setter to specify user-configured class names to ignore. 139 * 140 * @param excludedClasses classes to ignore. 141 * @propertySince 5.7 142 */ 143 @Override 144 public void setExcludedClasses(String... excludedClasses) { 145 super.setExcludedClasses(excludedClasses); 146 } 147 148 /** 149 * Setter to specify user-configured packages to ignore. 150 * 151 * @param excludedPackages packages to ignore. 152 * @throws IllegalArgumentException if there are invalid identifiers among the packages. 153 * @propertySince 7.7 154 */ 155 @Override 156 public void setExcludedPackages(String... excludedPackages) { 157 super.setExcludedPackages(excludedPackages); 158 } 159 160}