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.sizes;
21  
22  import com.puppycrawl.tools.checkstyle.FileStatefulCheck;
23  import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
24  import com.puppycrawl.tools.checkstyle.api.DetailAST;
25  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
26  
27  /**
28   * <p>
29   * Checks for the number of types declared at the <i>outer</i> (or <i>root</i>) level in a file.
30   * </p>
31   * <p>
32   * Rationale: It is considered good practice to only define one outer type per file.
33   * </p>
34   * <ul>
35   * <li>
36   * Property {@code max} - Specify the maximum number of outer types allowed.
37   * Type is {@code int}.
38   * Default value is {@code 1}.
39   * </li>
40   * </ul>
41   * <p>
42   * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
43   * </p>
44   * <p>
45   * Violation Message Keys:
46   * </p>
47   * <ul>
48   * <li>
49   * {@code maxOuterTypes}
50   * </li>
51   * </ul>
52   *
53   * @since 5.0
54   */
55  @FileStatefulCheck
56  public class OuterTypeNumberCheck extends AbstractCheck {
57  
58      /**
59       * A key is pointing to the warning message text in "messages.properties"
60       * file.
61       */
62      public static final String MSG_KEY = "maxOuterTypes";
63  
64      /** Specify the maximum number of outer types allowed. */
65      private int max = 1;
66      /** Tracks the current depth in types. */
67      private int currentDepth;
68      /** Tracks the number of outer types found. */
69      private int outerNum;
70  
71      @Override
72      public int[] getDefaultTokens() {
73          return getRequiredTokens();
74      }
75  
76      @Override
77      public int[] getAcceptableTokens() {
78          return getRequiredTokens();
79      }
80  
81      @Override
82      public int[] getRequiredTokens() {
83          return new int[] {
84              TokenTypes.CLASS_DEF,
85              TokenTypes.INTERFACE_DEF,
86              TokenTypes.ENUM_DEF,
87              TokenTypes.ANNOTATION_DEF,
88              TokenTypes.RECORD_DEF,
89          };
90      }
91  
92      @Override
93      public void beginTree(DetailAST ast) {
94          currentDepth = 0;
95          outerNum = 0;
96      }
97  
98      @Override
99      public void finishTree(DetailAST ast) {
100         if (max < outerNum) {
101             log(ast, MSG_KEY, outerNum, max);
102         }
103     }
104 
105     @Override
106     public void visitToken(DetailAST ast) {
107         if (currentDepth == 0) {
108             outerNum++;
109         }
110         currentDepth++;
111     }
112 
113     @Override
114     public void leaveToken(DetailAST ast) {
115         currentDepth--;
116     }
117 
118     /**
119      * Setter to specify the maximum number of outer types allowed.
120      *
121      * @param max the new number.
122      * @since 5.0
123      */
124     public void setMax(int max) {
125         this.max = max;
126     }
127 
128 }