1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2025 the original author or authors.
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ///////////////////////////////////////////////////////////////////////////////////////////////
19
20 package com.puppycrawl.tools.checkstyle.checks.metrics;
21
22 import java.util.regex.Pattern;
23
24 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
25
26 /**
27 * <div>
28 * Measures the number of distinct classes that are instantiated
29 * within the given class or record. This type of coupling is not caused by inheritance or
30 * the object-oriented paradigm. Generally speaking, any data type with other
31 * data types as members or local variable that is an instantiation (object)
32 * of another class has data abstraction coupling (DAC). The higher the DAC,
33 * the more complex the structure of the class.
34 * </div>
35 *
36 * <p>
37 * This check processes files in the following way:
38 * </p>
39 * <ol>
40 * <li>
41 * Iterates over the list of tokens (defined below) and counts all mentioned classes.
42 * <ul>
43 * <li>
44 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#IMPORT">
45 * PACKAGE_DEF</a>
46 * </li>
47 * <li>
48 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#IMPORT">
49 * IMPORT</a>
50 * </li>
51 * <li>
52 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CLASS_DEF">
53 * CLASS_DEF</a>
54 * </li>
55 * <li>
56 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#INTERFACE_DEF">
57 * INTERFACE_DEF</a>
58 * </li>
59 * <li>
60 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ENUM_DEF">
61 * ENUM_DEF</a>
62 * </li>
63 * <li>
64 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_NEW">
65 * LITERAL_NEW</a>
66 * </li>
67 * <li>
68 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#RECORD_DEF">
69 * RECORD_DEF</a>
70 * </li>
71 * </ul>
72 * </li>
73 * <li>
74 * If a class was imported with direct import (i.e. {@code import java.math.BigDecimal}),
75 * or the class was referenced with the package name (i.e. {@code java.math.BigDecimal value})
76 * and the package was added to the {@code excludedPackages} parameter, the class
77 * does not increase complexity.
78 * </li>
79 * <li>
80 * If a class name was added to the {@code excludedClasses} parameter,
81 * the class does not increase complexity.
82 * </li>
83 * </ol>
84 *
85 * @since 3.4
86 */
87 public final class ClassDataAbstractionCouplingCheck
88 extends AbstractClassCouplingCheck {
89
90 /**
91 * A key is pointing to the warning message text in "messages.properties"
92 * file.
93 */
94 public static final String MSG_KEY = "classDataAbstractionCoupling";
95
96 /** Default allowed complexity. */
97 private static final int DEFAULT_MAX = 7;
98
99 /** Creates bew instance of the check. */
100 public ClassDataAbstractionCouplingCheck() {
101 super(DEFAULT_MAX);
102 }
103
104 @Override
105 public int[] getRequiredTokens() {
106 return new int[] {
107 TokenTypes.PACKAGE_DEF,
108 TokenTypes.IMPORT,
109 TokenTypes.CLASS_DEF,
110 TokenTypes.INTERFACE_DEF,
111 TokenTypes.ENUM_DEF,
112 TokenTypes.LITERAL_NEW,
113 TokenTypes.RECORD_DEF,
114 };
115 }
116
117 @Override
118 public int[] getAcceptableTokens() {
119 return getRequiredTokens();
120 }
121
122 @Override
123 protected String getLogMessageId() {
124 return MSG_KEY;
125 }
126
127 /**
128 * Setter to specify user-configured regular expressions to ignore classes.
129 *
130 * @param from array representing regular expressions of classes to ignore.
131 * @propertySince 7.7
132 * @noinspection RedundantMethodOverride
133 * @noinspectionreason Display module's unique property version
134 */
135 @Override
136 public void setExcludeClassesRegexps(Pattern... from) {
137 super.setExcludeClassesRegexps(from);
138 }
139
140 /**
141 * Setter to specify user-configured class names to ignore.
142 *
143 * @param excludedClasses classes to ignore.
144 * @propertySince 5.7
145 * @noinspection RedundantMethodOverride
146 * @noinspectionreason Display module's unique property version
147 */
148 @Override
149 public void setExcludedClasses(String... excludedClasses) {
150 super.setExcludedClasses(excludedClasses);
151 }
152
153 /**
154 * Setter to specify user-configured packages to ignore.
155 *
156 * @param excludedPackages packages to ignore.
157 * @throws IllegalArgumentException if there are invalid identifiers among the packages.
158 * @propertySince 7.7
159 * @noinspection RedundantMethodOverride
160 * @noinspectionreason Display module's unique property version
161 */
162 @Override
163 public void setExcludedPackages(String... excludedPackages) {
164 super.setExcludedPackages(excludedPackages);
165 }
166
167 }