View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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              TokenTypes.COMPACT_COMPILATION_UNIT,
87          };
88      }
89  
90      @Override
91      public int[] getAcceptableTokens() {
92          return getRequiredTokens();
93      }
94  
95      @Override
96      protected String getLogMessageId() {
97          return MSG_KEY;
98      }
99  
100     /**
101      * Setter to specify user-configured regular expressions to ignore classes.
102      *
103      * @param from array representing regular expressions of classes to ignore.
104      * @propertySince 7.7
105      */
106     @Override
107     public void setExcludeClassesRegexps(Pattern... from) {
108         super.setExcludeClassesRegexps(from);
109     }
110 
111     /**
112      * Setter to specify user-configured class names to ignore.
113      *
114      * @param excludedClasses classes to ignore.
115      * @propertySince 5.7
116      */
117     @Override
118     public void setExcludedClasses(String... excludedClasses) {
119         super.setExcludedClasses(excludedClasses);
120     }
121 
122     /**
123      * Setter to specify user-configured packages to ignore.
124      *
125      * @param excludedPackages packages to ignore.
126      * @throws IllegalArgumentException if there are invalid identifiers among the packages.
127      * @propertySince 7.7
128      */
129     @Override
130     public void setExcludedPackages(String... excludedPackages) {
131         super.setExcludedPackages(excludedPackages);
132     }
133 
134 }