View Javadoc
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 java.util.regex.Pattern;
23  
24  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
25  
26  /**
27   * <div>
28   * Checks the number of other types a given class/record/interface/enum/annotation
29   * relies on. Also, the square of this has been shown to indicate the amount
30   * of maintenance required in functional programs (on a file basis) at least.
31   * </div>
32   *
33   * <p>
34   * This check processes files in the following way:
35   * </p>
36   * <ol>
37   * <li>
38   * Iterates over all tokens that might contain type reference.
39   * </li>
40   * <li>
41   * If a class was imported with direct import (i.e. {@code import java.math.BigDecimal}),
42   * or the class was referenced with the package name (i.e. {@code java.math.BigDecimal value})
43   * and the package was added to the {@code excludedPackages} parameter,
44   * the class does not increase complexity.
45   * </li>
46   * <li>
47   * If a class name was added to the {@code excludedClasses} parameter,
48   * the class does not increase complexity.
49   * </li>
50   * </ol>
51   *
52   * @since 3.4
53   */
54  public final class ClassFanOutComplexityCheck extends AbstractClassCouplingCheck {
55  
56      /**
57       * A key is pointing to the warning message text in "messages.properties"
58       * file.
59       */
60      public static final String MSG_KEY = "classFanOutComplexity";
61  
62      /** Default value of max value. */
63      private static final int DEFAULT_MAX = 20;
64  
65      /** Creates new instance of this check. */
66      public ClassFanOutComplexityCheck() {
67          super(DEFAULT_MAX);
68      }
69  
70      @Override
71      public int[] getRequiredTokens() {
72          return new int[] {
73              TokenTypes.PACKAGE_DEF,
74              TokenTypes.IMPORT,
75              TokenTypes.CLASS_DEF,
76              TokenTypes.EXTENDS_CLAUSE,
77              TokenTypes.IMPLEMENTS_CLAUSE,
78              TokenTypes.ANNOTATION,
79              TokenTypes.INTERFACE_DEF,
80              TokenTypes.ENUM_DEF,
81              TokenTypes.TYPE,
82              TokenTypes.LITERAL_NEW,
83              TokenTypes.LITERAL_THROWS,
84              TokenTypes.ANNOTATION_DEF,
85              TokenTypes.RECORD_DEF,
86          };
87      }
88  
89      @Override
90      public int[] getAcceptableTokens() {
91          return getRequiredTokens();
92      }
93  
94      @Override
95      protected String getLogMessageId() {
96          return MSG_KEY;
97      }
98  
99      /**
100      * Setter to specify user-configured regular expressions to ignore classes.
101      *
102      * @param from array representing regular expressions of classes to ignore.
103      * @propertySince 7.7
104      * @noinspection RedundantMethodOverride
105      * @noinspectionreason Display module's unique property version
106      */
107     @Override
108     public void setExcludeClassesRegexps(Pattern... from) {
109         super.setExcludeClassesRegexps(from);
110     }
111 
112     /**
113      * Setter to specify user-configured class names to ignore.
114      *
115      * @param excludedClasses classes to ignore.
116      * @propertySince 5.7
117      * @noinspection RedundantMethodOverride
118      * @noinspectionreason Display module's unique property version
119      */
120     @Override
121     public void setExcludedClasses(String... excludedClasses) {
122         super.setExcludedClasses(excludedClasses);
123     }
124 
125     /**
126      * Setter to specify user-configured packages to ignore.
127      *
128      * @param excludedPackages packages to ignore.
129      * @throws IllegalArgumentException if there are invalid identifiers among the packages.
130      * @propertySince 7.7
131      * @noinspection RedundantMethodOverride
132      * @noinspectionreason Display module's unique property version
133      */
134     @Override
135     public void setExcludedPackages(String... excludedPackages) {
136         super.setExcludedPackages(excludedPackages);
137     }
138 
139 }