View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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   * </li>
56   * <li>
57   * Property {@code excludedClasses} - Specify user-configured class names to ignore.
58   * Type is {@code java.lang.String[]}.
59   * Default value is {@code ArrayIndexOutOfBoundsException, ArrayList, Boolean, Byte,
60   * Character, Class, Collection, Deprecated, Deque, Double, DoubleStream, EnumSet, Exception,
61   * Float, FunctionalInterface, HashMap, HashSet, IllegalArgumentException, IllegalStateException,
62   * IndexOutOfBoundsException, IntStream, Integer, LinkedHashMap, LinkedHashSet, LinkedList, List,
63   * Long, LongStream, Map, NullPointerException, Object, Optional, OptionalDouble, OptionalInt,
64   * OptionalLong, Override, Queue, RuntimeException, SafeVarargs, SecurityException, Set, Short,
65   * SortedMap, SortedSet, Stream, String, StringBuffer, StringBuilder, SuppressWarnings, Throwable,
66   * TreeMap, TreeSet, UnsupportedOperationException, Void, boolean, byte, char, double,
67   * float, int, long, short, var, void}.
68   * </li>
69   * <li>
70   * Property {@code excludedPackages} - Specify user-configured packages to ignore.
71   * Type is {@code java.lang.String[]}.
72   * Default value is {@code ""}.
73   * </li>
74   * <li>
75   * Property {@code max} - Specify the maximum threshold allowed.
76   * Type is {@code int}.
77   * Default value is {@code 20}.
78   * </li>
79   * </ul>
80   *
81   * <p>
82   * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
83   * </p>
84   *
85   * <p>
86   * Violation Message Keys:
87   * </p>
88   * <ul>
89   * <li>
90   * {@code classFanOutComplexity}
91   * </li>
92   * </ul>
93   *
94   * @since 3.4
95   */
96  public final class ClassFanOutComplexityCheck extends AbstractClassCouplingCheck {
97  
98      /**
99       * 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 }