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