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 com.puppycrawl.tools.checkstyle.api.TokenTypes;
23
24 /**
25 * <div>
26 * Measures the number of distinct classes that are instantiated
27 * within the given class or record. This type of coupling is not caused by inheritance or
28 * the object-oriented paradigm. Generally speaking, any data type with other
29 * data types as members or local variable that is an instantiation (object)
30 * of another class has data abstraction coupling (DAC). The higher the DAC,
31 * the more complex the structure of the class.
32 * </div>
33 *
34 * <p>
35 * This check processes files in the following way:
36 * </p>
37 * <ol>
38 * <li>
39 * Iterates over the list of tokens (defined below) and counts all mentioned classes.
40 * <ul>
41 * <li>
42 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#IMPORT">
43 * PACKAGE_DEF</a>
44 * </li>
45 * <li>
46 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#IMPORT">
47 * IMPORT</a>
48 * </li>
49 * <li>
50 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CLASS_DEF">
51 * CLASS_DEF</a>
52 * </li>
53 * <li>
54 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#INTERFACE_DEF">
55 * INTERFACE_DEF</a>
56 * </li>
57 * <li>
58 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ENUM_DEF">
59 * ENUM_DEF</a>
60 * </li>
61 * <li>
62 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_NEW">
63 * LITERAL_NEW</a>
64 * </li>
65 * <li>
66 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#RECORD_DEF">
67 * RECORD_DEF</a>
68 * </li>
69 * </ul>
70 * </li>
71 * <li>
72 * If a class was imported with direct import (i.e. {@code import java.math.BigDecimal}),
73 * or the class was referenced with the package name (i.e. {@code java.math.BigDecimal value})
74 * and the package was added to the {@code excludedPackages} parameter, the class
75 * does not increase complexity.
76 * </li>
77 * <li>
78 * If a class name was added to the {@code excludedClasses} parameter,
79 * the class does not increase complexity.
80 * </li>
81 * </ol>
82 * <ul>
83 * <li>
84 * Property {@code excludeClassesRegexps} - Specify user-configured regular
85 * expressions to ignore classes.
86 * Type is {@code java.util.regex.Pattern[]}.
87 * Default value is {@code ^$}.
88 * Since version 7.7
89 * </li>
90 * <li>
91 * Property {@code excludedClasses} - Specify user-configured class names to ignore.
92 * Type is {@code java.lang.String[]}.
93 * Default value is {@code ArrayIndexOutOfBoundsException, ArrayList, Boolean, Byte,
94 * Character, Class, Collection, Deprecated, Deque, Double, DoubleStream, EnumSet, Exception,
95 * Float, FunctionalInterface, HashMap, HashSet, IllegalArgumentException, IllegalStateException,
96 * IndexOutOfBoundsException, IntStream, Integer, LinkedHashMap, LinkedHashSet, LinkedList, List,
97 * Long, LongStream, Map, NullPointerException, Object, Optional, OptionalDouble, OptionalInt,
98 * OptionalLong, Override, Queue, RuntimeException, SafeVarargs, SecurityException, Set, Short,
99 * SortedMap, SortedSet, Stream, String, StringBuffer, StringBuilder, SuppressWarnings, Throwable,
100 * TreeMap, TreeSet, UnsupportedOperationException, Void, boolean, byte, char, double,
101 * float, int, long, short, var, void}.
102 * Since version 5.7
103 * </li>
104 * <li>
105 * Property {@code excludedPackages} - Specify user-configured packages to ignore.
106 * Type is {@code java.lang.String[]}.
107 * Default value is {@code ""}.
108 * Since version 7.7
109 * </li>
110 * <li>
111 * Property {@code max} - Specify the maximum threshold allowed.
112 * Type is {@code int}.
113 * Default value is {@code 7}.
114 * </li>
115 * </ul>
116 *
117 * <p>
118 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
119 * </p>
120 *
121 * <p>
122 * Violation Message Keys:
123 * </p>
124 * <ul>
125 * <li>
126 * {@code classDataAbstractionCoupling}
127 * </li>
128 * </ul>
129 *
130 * @since 3.4
131 *
132 */
133 public final class ClassDataAbstractionCouplingCheck
134 extends AbstractClassCouplingCheck {
135
136 /**
137 * A key is pointing to the warning message text in "messages.properties"
138 * file.
139 */
140 public static final String MSG_KEY = "classDataAbstractionCoupling";
141
142 /** Default allowed complexity. */
143 private static final int DEFAULT_MAX = 7;
144
145 /** Creates bew instance of the check. */
146 public ClassDataAbstractionCouplingCheck() {
147 super(DEFAULT_MAX);
148 }
149
150 @Override
151 public int[] getRequiredTokens() {
152 return new int[] {
153 TokenTypes.PACKAGE_DEF,
154 TokenTypes.IMPORT,
155 TokenTypes.CLASS_DEF,
156 TokenTypes.INTERFACE_DEF,
157 TokenTypes.ENUM_DEF,
158 TokenTypes.LITERAL_NEW,
159 TokenTypes.RECORD_DEF,
160 };
161 }
162
163 @Override
164 public int[] getAcceptableTokens() {
165 return getRequiredTokens();
166 }
167
168 @Override
169 protected String getLogMessageId() {
170 return MSG_KEY;
171 }
172
173 }