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 * 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 * Type is {@code java.lang.String[]}.
071 * Default value is {@code ""}.
072 * </li>
073 * <li>
074 * Property {@code max} - Specify the maximum threshold allowed.
075 * Type is {@code int}.
076 * Default value is {@code 20}.
077 * </li>
078 * </ul>
079 * <p>
080 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
081 * </p>
082 * <p>
083 * Violation Message Keys:
084 * </p>
085 * <ul>
086 * <li>
087 * {@code classFanOutComplexity}
088 * </li>
089 * </ul>
090 *
091 * @since 3.4
092 */
093public final class ClassFanOutComplexityCheck extends AbstractClassCouplingCheck {
094
095    /**
096     * A key is pointing to the warning message text in "messages.properties"
097     * file.
098     */
099    public static final String MSG_KEY = "classFanOutComplexity";
100
101    /** Default value of max value. */
102    private static final int DEFAULT_MAX = 20;
103
104    /** Creates new instance of this check. */
105    public ClassFanOutComplexityCheck() {
106        super(DEFAULT_MAX);
107    }
108
109    @Override
110    public int[] getRequiredTokens() {
111        return new int[] {
112            TokenTypes.PACKAGE_DEF,
113            TokenTypes.IMPORT,
114            TokenTypes.CLASS_DEF,
115            TokenTypes.EXTENDS_CLAUSE,
116            TokenTypes.IMPLEMENTS_CLAUSE,
117            TokenTypes.ANNOTATION,
118            TokenTypes.INTERFACE_DEF,
119            TokenTypes.ENUM_DEF,
120            TokenTypes.TYPE,
121            TokenTypes.LITERAL_NEW,
122            TokenTypes.LITERAL_THROWS,
123            TokenTypes.ANNOTATION_DEF,
124            TokenTypes.RECORD_DEF,
125        };
126    }
127
128    @Override
129    public int[] getAcceptableTokens() {
130        return getRequiredTokens();
131    }
132
133    @Override
134    protected String getLogMessageId() {
135        return MSG_KEY;
136    }
137
138}