001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2023 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 * Checks the number of other types a given class/record/interface/enum/annotation
027 * relies on. Also, the square of this has been shown to indicate the amount
028 * of maintenance required in functional programs (on a file basis) at least.
029 * </p>
030 * <p>
031 * This check processes files in the following way:
032 * </p>
033 * <ol>
034 * <li>
035 * Iterates over all tokens that might contain type reference.
036 * </li>
037 * <li>
038 * If a class was imported with direct import (i.e. {@code import java.math.BigDecimal}),
039 * or the class was referenced with the package name (i.e. {@code java.math.BigDecimal value})
040 * and the package was added to the {@code excludedPackages} parameter,
041 * the class does not increase complexity.
042 * </li>
043 * <li>
044 * If a class name was added to the {@code excludedClasses} parameter,
045 * the class does not increase complexity.
046 * </li>
047 * </ol>
048 * <ul>
049 * <li>
050 * Property {@code excludeClassesRegexps} - Specify user-configured regular
051 * expressions to ignore classes.
052 * Type is {@code java.util.regex.Pattern[]}.
053 * Default value is {@code ^$}.
054 * </li>
055 * <li>
056 * Property {@code excludedClasses} - Specify user-configured class names to ignore.
057 * Type is {@code java.lang.String[]}.
058 * Default value is {@code ArrayIndexOutOfBoundsException, ArrayList, Boolean, Byte,
059 * Character, Class, Collection, Deprecated, Deque, Double, DoubleStream, EnumSet, Exception,
060 * Float, FunctionalInterface, HashMap, HashSet, IllegalArgumentException, IllegalStateException,
061 * IndexOutOfBoundsException, IntStream, Integer, LinkedHashMap, LinkedHashSet, LinkedList, List,
062 * Long, LongStream, Map, NullPointerException, Object, Optional, OptionalDouble, OptionalInt,
063 * OptionalLong, Override, Queue, RuntimeException, SafeVarargs, SecurityException, Set, Short,
064 * SortedMap, SortedSet, Stream, String, StringBuffer, StringBuilder, SuppressWarnings, Throwable,
065 * TreeMap, TreeSet, UnsupportedOperationException, Void, boolean, byte, char, double,
066 * float, int, long, short, var, void}.
067 * </li>
068 * <li>
069 * Property {@code excludedPackages} - Specify user-configured packages to ignore.
070 * All excluded packages should end with a period, so it also appends a dot to a package name.
071 * Type is {@code java.lang.String[]}.
072 * Default value is {@code ""}.
073 * </li>
074 * <li>
075 * Property {@code max} - Specify the maximum threshold allowed.
076 * Type is {@code int}.
077 * Default value is {@code 20}.
078 * </li>
079 * </ul>
080 * <p>
081 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
082 * </p>
083 * <p>
084 * Violation Message Keys:
085 * </p>
086 * <ul>
087 * <li>
088 * {@code classFanOutComplexity}
089 * </li>
090 * </ul>
091 *
092 * @since 3.4
093 */
094public final class ClassFanOutComplexityCheck extends AbstractClassCouplingCheck {
095
096    /**
097     * A key is pointing to the warning message text in "messages.properties"
098     * file.
099     */
100    public static final String MSG_KEY = "classFanOutComplexity";
101
102    /** Default value of max value. */
103    private static final int DEFAULT_MAX = 20;
104
105    /** Creates new instance of this check. */
106    public ClassFanOutComplexityCheck() {
107        super(DEFAULT_MAX);
108    }
109
110    @Override
111    public int[] getRequiredTokens() {
112        return new int[] {
113            TokenTypes.PACKAGE_DEF,
114            TokenTypes.IMPORT,
115            TokenTypes.CLASS_DEF,
116            TokenTypes.EXTENDS_CLAUSE,
117            TokenTypes.IMPLEMENTS_CLAUSE,
118            TokenTypes.ANNOTATION,
119            TokenTypes.INTERFACE_DEF,
120            TokenTypes.ENUM_DEF,
121            TokenTypes.TYPE,
122            TokenTypes.LITERAL_NEW,
123            TokenTypes.LITERAL_THROWS,
124            TokenTypes.ANNOTATION_DEF,
125            TokenTypes.RECORD_DEF,
126        };
127    }
128
129    @Override
130    public int[] getAcceptableTokens() {
131        return getRequiredTokens();
132    }
133
134    @Override
135    protected String getLogMessageId() {
136        return MSG_KEY;
137    }
138
139}