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 * </li>
89 * <li>
90 * Property {@code excludedClasses} - Specify user-configured class names to ignore.
91 * Type is {@code java.lang.String[]}.
92 * Default value is {@code ArrayIndexOutOfBoundsException, ArrayList, Boolean, Byte,
93 * Character, Class, Collection, Deprecated, Deque, Double, DoubleStream, EnumSet, Exception,
94 * Float, FunctionalInterface, HashMap, HashSet, IllegalArgumentException, IllegalStateException,
95 * IndexOutOfBoundsException, IntStream, Integer, LinkedHashMap, LinkedHashSet, LinkedList, List,
96 * Long, LongStream, Map, NullPointerException, Object, Optional, OptionalDouble, OptionalInt,
97 * OptionalLong, Override, Queue, RuntimeException, SafeVarargs, SecurityException, Set, Short,
98 * SortedMap, SortedSet, Stream, String, StringBuffer, StringBuilder, SuppressWarnings, Throwable,
99 * TreeMap, TreeSet, UnsupportedOperationException, Void, boolean, byte, char, double,
100 * float, int, long, short, var, void}.
101 * </li>
102 * <li>
103 * Property {@code excludedPackages} - Specify user-configured packages to ignore.
104 * Type is {@code java.lang.String[]}.
105 * Default value is {@code ""}.
106 * </li>
107 * <li>
108 * Property {@code max} - Specify the maximum threshold allowed.
109 * Type is {@code int}.
110 * Default value is {@code 7}.
111 * </li>
112 * </ul>
113 *
114 * <p>
115 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
116 * </p>
117 *
118 * <p>
119 * Violation Message Keys:
120 * </p>
121 * <ul>
122 * <li>
123 * {@code classDataAbstractionCoupling}
124 * </li>
125 * </ul>
126 *
127 * @since 3.4
128 *
129 */
130 public final class ClassDataAbstractionCouplingCheck
131 extends AbstractClassCouplingCheck {
132
133 /**
134 * A key is pointing to the warning message text in "messages.properties"
135 * file.
136 */
137 public static final String MSG_KEY = "classDataAbstractionCoupling";
138
139 /** Default allowed complexity. */
140 private static final int DEFAULT_MAX = 7;
141
142 /** Creates bew instance of the check. */
143 public ClassDataAbstractionCouplingCheck() {
144 super(DEFAULT_MAX);
145 }
146
147 @Override
148 public int[] getRequiredTokens() {
149 return new int[] {
150 TokenTypes.PACKAGE_DEF,
151 TokenTypes.IMPORT,
152 TokenTypes.CLASS_DEF,
153 TokenTypes.INTERFACE_DEF,
154 TokenTypes.ENUM_DEF,
155 TokenTypes.LITERAL_NEW,
156 TokenTypes.RECORD_DEF,
157 };
158 }
159
160 @Override
161 public int[] getAcceptableTokens() {
162 return getRequiredTokens();
163 }
164
165 @Override
166 protected String getLogMessageId() {
167 return MSG_KEY;
168 }
169
170 }