001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2024 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 com.puppycrawl.tools.checkstyle.api.TokenTypes;
023
024/**
025 * <div>
026 * Measures the number of distinct classes that are instantiated
027 * within the given class or record. This type of coupling is not caused by inheritance or
028 * the object-oriented paradigm. Generally speaking, any data type with other
029 * data types as members or local variable that is an instantiation (object)
030 * of another class has data abstraction coupling (DAC). The higher the DAC,
031 * the more complex the structure of the class.
032 * </div>
033 *
034 * <p>
035 * This check processes files in the following way:
036 * </p>
037 * <ol>
038 * <li>
039 * Iterates over the list of tokens (defined below) and counts all mentioned classes.
040 * <ul>
041 * <li>
042 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#IMPORT">
043 * PACKAGE_DEF</a>
044 * </li>
045 * <li>
046 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#IMPORT">
047 * IMPORT</a>
048 * </li>
049 * <li>
050 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CLASS_DEF">
051 * CLASS_DEF</a>
052 * </li>
053 * <li>
054 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#INTERFACE_DEF">
055 * INTERFACE_DEF</a>
056 * </li>
057 * <li>
058 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ENUM_DEF">
059 * ENUM_DEF</a>
060 * </li>
061 * <li>
062 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_NEW">
063 * LITERAL_NEW</a>
064 * </li>
065 * <li>
066 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#RECORD_DEF">
067 * RECORD_DEF</a>
068 * </li>
069 * </ul>
070 * </li>
071 * <li>
072 * If a class was imported with direct import (i.e. {@code import java.math.BigDecimal}),
073 * or the class was referenced with the package name (i.e. {@code java.math.BigDecimal value})
074 * and the package was added to the {@code excludedPackages} parameter, the class
075 * does not increase complexity.
076 * </li>
077 * <li>
078 * If a class name was added to the {@code excludedClasses} parameter,
079 * the class does not increase complexity.
080 * </li>
081 * </ol>
082 * <ul>
083 * <li>
084 * Property {@code excludeClassesRegexps} - Specify user-configured regular
085 * expressions to ignore classes.
086 * Type is {@code java.util.regex.Pattern[]}.
087 * Default value is {@code ^$}.
088 * </li>
089 * <li>
090 * Property {@code excludedClasses} - Specify user-configured class names to ignore.
091 * Type is {@code java.lang.String[]}.
092 * Default value is {@code ArrayIndexOutOfBoundsException, ArrayList, Boolean, Byte,
093 * Character, Class, Collection, Deprecated, Deque, Double, DoubleStream, EnumSet, Exception,
094 * Float, FunctionalInterface, HashMap, HashSet, IllegalArgumentException, IllegalStateException,
095 * IndexOutOfBoundsException, IntStream, Integer, LinkedHashMap, LinkedHashSet, LinkedList, List,
096 * Long, LongStream, Map, NullPointerException, Object, Optional, OptionalDouble, OptionalInt,
097 * OptionalLong, Override, Queue, RuntimeException, SafeVarargs, SecurityException, Set, Short,
098 * SortedMap, SortedSet, Stream, String, StringBuffer, StringBuilder, SuppressWarnings, Throwable,
099 * 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 */
130public 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}