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