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   * <p>
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   * </p>
30   * <p>
31   * This check processes files in the following way:
32   * </p>
33   * <ol>
34   * <li>
35   * Iterates over all tokens that might contain type reference.
36   * </li>
37   * <li>
38   * If a class was imported with direct import (i.e. {@code import java.math.BigDecimal}),
39   * or the class was referenced with the package name (i.e. {@code java.math.BigDecimal value})
40   * and the package was added to the {@code excludedPackages} parameter,
41   * the class does not increase complexity.
42   * </li>
43   * <li>
44   * If a class name was added to the {@code excludedClasses} parameter,
45   * the class does not increase complexity.
46   * </li>
47   * </ol>
48   * <ul>
49   * <li>
50   * Property {@code excludeClassesRegexps} - Specify user-configured regular
51   * expressions to ignore classes.
52   * Type is {@code java.util.regex.Pattern[]}.
53   * Default value is {@code ^$}.
54   * </li>
55   * <li>
56   * Property {@code excludedClasses} - Specify user-configured class names to ignore.
57   * Type is {@code java.lang.String[]}.
58   * Default value is {@code ArrayIndexOutOfBoundsException, ArrayList, Boolean, Byte,
59   * Character, Class, Collection, Deprecated, Deque, Double, DoubleStream, EnumSet, Exception,
60   * Float, FunctionalInterface, HashMap, HashSet, IllegalArgumentException, IllegalStateException,
61   * IndexOutOfBoundsException, IntStream, Integer, LinkedHashMap, LinkedHashSet, LinkedList, List,
62   * Long, LongStream, Map, NullPointerException, Object, Optional, OptionalDouble, OptionalInt,
63   * OptionalLong, Override, Queue, RuntimeException, SafeVarargs, SecurityException, Set, Short,
64   * SortedMap, SortedSet, Stream, String, StringBuffer, StringBuilder, SuppressWarnings, Throwable,
65   * TreeMap, TreeSet, UnsupportedOperationException, Void, boolean, byte, char, double,
66   * float, int, long, short, var, void}.
67   * </li>
68   * <li>
69   * Property {@code excludedPackages} - Specify user-configured packages to ignore.
70   * Type is {@code java.lang.String[]}.
71   * Default value is {@code ""}.
72   * </li>
73   * <li>
74   * Property {@code max} - Specify the maximum threshold allowed.
75   * Type is {@code int}.
76   * Default value is {@code 20}.
77   * </li>
78   * </ul>
79   * <p>
80   * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
81   * </p>
82   * <p>
83   * Violation Message Keys:
84   * </p>
85   * <ul>
86   * <li>
87   * {@code classFanOutComplexity}
88   * </li>
89   * </ul>
90   *
91   * @since 3.4
92   */
93  public final class ClassFanOutComplexityCheck extends AbstractClassCouplingCheck {
94  
95      /**
96       * A key is pointing to the warning message text in "messages.properties"
97       * file.
98       */
99      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 }