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 * <p>
026 * Measures the number of instantiations of other classes
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 * </p>
033 * <p>
034 * This check processes files in the following way:
035 * </p>
036 * <ol>
037 * <li>
038 * Iterates over the list of tokens (defined below) and counts all mentioned classes.
039 * <ul>
040 * <li>
041 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#IMPORT">
042 * PACKAGE_DEF</a>
043 * </li>
044 * <li>
045 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#IMPORT">
046 * IMPORT</a>
047 * </li>
048 * <li>
049 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CLASS_DEF">
050 * CLASS_DEF</a>
051 * </li>
052 * <li>
053 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#INTERFACE_DEF">
054 * INTERFACE_DEF</a>
055 * </li>
056 * <li>
057 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ENUM_DEF">
058 * ENUM_DEF</a>
059 * </li>
060 * <li>
061 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_NEW">
062 * LITERAL_NEW</a>
063 * </li>
064 * <li>
065 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#RECORD_DEF">
066 * RECORD_DEF</a>
067 * </li>
068 * </ul>
069 * </li>
070 * <li>
071 * If a class was imported with direct import (i.e. {@code import java.math.BigDecimal}),
072 * or the class was referenced with the package name (i.e. {@code java.math.BigDecimal value})
073 * and the package was added to the {@code excludedPackages} parameter, the class
074 * does not increase complexity.
075 * </li>
076 * <li>
077 * If a class name was added to the {@code excludedClasses} parameter,
078 * the class does not increase complexity.
079 * </li>
080 * </ol>
081 * <ul>
082 * <li>
083 * Property {@code excludeClassesRegexps} - Specify user-configured regular
084 * expressions to ignore classes.
085 * Type is {@code java.util.regex.Pattern[]}.
086 * Default value is {@code ^$}.
087 * </li>
088 * <li>
089 * Property {@code excludedClasses} - Specify user-configured class names to ignore.
090 * Type is {@code java.lang.String[]}.
091 * Default value is {@code ArrayIndexOutOfBoundsException, ArrayList, Boolean, Byte,
092 * Character, Class, Collection, Deprecated, Deque, Double, DoubleStream, EnumSet, Exception,
093 * Float, FunctionalInterface, HashMap, HashSet, IllegalArgumentException, IllegalStateException,
094 * IndexOutOfBoundsException, IntStream, Integer, LinkedHashMap, LinkedHashSet, LinkedList, List,
095 * Long, LongStream, Map, NullPointerException, Object, Optional, OptionalDouble, OptionalInt,
096 * OptionalLong, Override, Queue, RuntimeException, SafeVarargs, SecurityException, Set, Short,
097 * SortedMap, SortedSet, Stream, String, StringBuffer, StringBuilder, SuppressWarnings, Throwable,
098 * TreeMap, TreeSet, UnsupportedOperationException, Void, boolean, byte, char, double,
099 * 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 */
127public 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}