001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2025 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 * Since version 7.7 056 * </li> 057 * <li> 058 * Property {@code excludedClasses} - Specify user-configured class names to ignore. 059 * Type is {@code java.lang.String[]}. 060 * Default value is {@code ArrayIndexOutOfBoundsException, ArrayList, Boolean, Byte, 061 * Character, Class, Collection, Deprecated, Deque, Double, DoubleStream, EnumSet, Exception, 062 * Float, FunctionalInterface, HashMap, HashSet, IllegalArgumentException, IllegalStateException, 063 * IndexOutOfBoundsException, IntStream, Integer, LinkedHashMap, LinkedHashSet, LinkedList, List, 064 * Long, LongStream, Map, NullPointerException, Object, Optional, OptionalDouble, OptionalInt, 065 * OptionalLong, Override, Queue, RuntimeException, SafeVarargs, SecurityException, Set, Short, 066 * SortedMap, SortedSet, Stream, String, StringBuffer, StringBuilder, SuppressWarnings, Throwable, 067 * TreeMap, TreeSet, UnsupportedOperationException, Void, boolean, byte, char, double, 068 * float, int, long, short, var, void}. 069 * Since version 5.7 070 * </li> 071 * <li> 072 * Property {@code excludedPackages} - Specify user-configured packages to ignore. 073 * Type is {@code java.lang.String[]}. 074 * Default value is {@code ""}. 075 * Since version 7.7 076 * </li> 077 * <li> 078 * Property {@code max} - Specify the maximum threshold allowed. 079 * Type is {@code int}. 080 * Default value is {@code 20}. 081 * </li> 082 * </ul> 083 * 084 * <p> 085 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 086 * </p> 087 * 088 * <p> 089 * Violation Message Keys: 090 * </p> 091 * <ul> 092 * <li> 093 * {@code classFanOutComplexity} 094 * </li> 095 * </ul> 096 * 097 * @since 3.4 098 */ 099public 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}