1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2025 the original author or authors.
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ///////////////////////////////////////////////////////////////////////////////////////////////
19
20 package com.puppycrawl.tools.checkstyle.checks.metrics;
21
22 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
23
24 /**
25 * <div>
26 * Checks the number of other types a given class/record/interface/enum/annotation
27 * relies on. Also, the square of this has been shown to indicate the amount
28 * of maintenance required in functional programs (on a file basis) at least.
29 * </div>
30 *
31 * <p>
32 * This check processes files in the following way:
33 * </p>
34 * <ol>
35 * <li>
36 * Iterates over all tokens that might contain type reference.
37 * </li>
38 * <li>
39 * If a class was imported with direct import (i.e. {@code import java.math.BigDecimal}),
40 * or the class was referenced with the package name (i.e. {@code java.math.BigDecimal value})
41 * and the package was added to the {@code excludedPackages} parameter,
42 * the class does not increase complexity.
43 * </li>
44 * <li>
45 * If a class name was added to the {@code excludedClasses} parameter,
46 * the class does not increase complexity.
47 * </li>
48 * </ol>
49 * <ul>
50 * <li>
51 * Property {@code excludeClassesRegexps} - Specify user-configured regular
52 * expressions to ignore classes.
53 * Type is {@code java.util.regex.Pattern[]}.
54 * Default value is {@code ^$}.
55 * Since version 7.7
56 * </li>
57 * <li>
58 * Property {@code excludedClasses} - Specify user-configured class names to ignore.
59 * Type is {@code java.lang.String[]}.
60 * Default value is {@code ArrayIndexOutOfBoundsException, ArrayList, Boolean, Byte,
61 * Character, Class, Collection, Deprecated, Deque, Double, DoubleStream, EnumSet, Exception,
62 * Float, FunctionalInterface, HashMap, HashSet, IllegalArgumentException, IllegalStateException,
63 * IndexOutOfBoundsException, IntStream, Integer, LinkedHashMap, LinkedHashSet, LinkedList, List,
64 * Long, LongStream, Map, NullPointerException, Object, Optional, OptionalDouble, OptionalInt,
65 * OptionalLong, Override, Queue, RuntimeException, SafeVarargs, SecurityException, Set, Short,
66 * SortedMap, SortedSet, Stream, String, StringBuffer, StringBuilder, SuppressWarnings, Throwable,
67 * TreeMap, TreeSet, UnsupportedOperationException, Void, boolean, byte, char, double,
68 * float, int, long, short, var, void}.
69 * Since version 5.7
70 * </li>
71 * <li>
72 * Property {@code excludedPackages} - Specify user-configured packages to ignore.
73 * Type is {@code java.lang.String[]}.
74 * Default value is {@code ""}.
75 * Since version 7.7
76 * </li>
77 * <li>
78 * Property {@code max} - Specify the maximum threshold allowed.
79 * Type is {@code int}.
80 * Default value is {@code 20}.
81 * </li>
82 * </ul>
83 *
84 * <p>
85 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
86 * </p>
87 *
88 * <p>
89 * Violation Message Keys:
90 * </p>
91 * <ul>
92 * <li>
93 * {@code classFanOutComplexity}
94 * </li>
95 * </ul>
96 *
97 * @since 3.4
98 */
99 public final class ClassFanOutComplexityCheck extends AbstractClassCouplingCheck {
100
101 /**
102 * A key is pointing to the warning message text in "messages.properties"
103 * file.
104 */
105 public static final String MSG_KEY = "classFanOutComplexity";
106
107 /** Default value of max value. */
108 private static final int DEFAULT_MAX = 20;
109
110 /** Creates new instance of this check. */
111 public ClassFanOutComplexityCheck() {
112 super(DEFAULT_MAX);
113 }
114
115 @Override
116 public int[] getRequiredTokens() {
117 return new int[] {
118 TokenTypes.PACKAGE_DEF,
119 TokenTypes.IMPORT,
120 TokenTypes.CLASS_DEF,
121 TokenTypes.EXTENDS_CLAUSE,
122 TokenTypes.IMPLEMENTS_CLAUSE,
123 TokenTypes.ANNOTATION,
124 TokenTypes.INTERFACE_DEF,
125 TokenTypes.ENUM_DEF,
126 TokenTypes.TYPE,
127 TokenTypes.LITERAL_NEW,
128 TokenTypes.LITERAL_THROWS,
129 TokenTypes.ANNOTATION_DEF,
130 TokenTypes.RECORD_DEF,
131 };
132 }
133
134 @Override
135 public int[] getAcceptableTokens() {
136 return getRequiredTokens();
137 }
138
139 @Override
140 protected String getLogMessageId() {
141 return MSG_KEY;
142 }
143
144 }