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