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 java.math.BigInteger;
23 import java.util.ArrayDeque;
24 import java.util.Deque;
25
26 import com.puppycrawl.tools.checkstyle.FileStatefulCheck;
27 import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
28 import com.puppycrawl.tools.checkstyle.api.DetailAST;
29 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
30
31 /**
32 * <div>
33 * Checks cyclomatic complexity against a specified limit. It is a measure of
34 * the minimum number of possible paths through the source and therefore the
35 * number of required tests, it is not about quality of code! It is only
36 * applied to methods, c-tors,
37 * <a href="https://docs.oracle.com/javase/tutorial/java/javaOO/initial.html">
38 * static initializers and instance initializers</a>.
39 * </div>
40 *
41 * <p>
42 * The complexity is equal to the number of decision points {@code + 1}.
43 * Decision points:
44 * </p>
45 * <ul>
46 * <li>
47 * {@code if}, {@code while}, {@code do}, {@code for},
48 * {@code ?:}, {@code catch}, {@code switch}, {@code case} statements.
49 * </li>
50 * <li>
51 * Operators {@code &&} and {@code ||} in the body of target.
52 * </li>
53 * <li>
54 * {@code when} expression in case labels, also known as guards.
55 * </li>
56 * </ul>
57 *
58 * <p>
59 * By pure theory level 1-4 is considered easy to test, 5-7 OK, 8-10 consider
60 * re-factoring to ease testing, and 11+ re-factor now as testing will be painful.
61 * </p>
62 *
63 * <p>
64 * When it comes to code quality measurement by this metric level 10 is very
65 * good level as a ultimate target (that is hard to archive). Do not be ashamed
66 * to have complexity level 15 or even higher, but keep it below 20 to catch
67 * really bad-designed code automatically.
68 * </p>
69 *
70 * <p>
71 * Please use Suppression to avoid violations on cases that could not be split
72 * in few methods without damaging readability of code or encapsulation.
73 * </p>
74 * <ul>
75 * <li>
76 * Property {@code max} - Specify the maximum threshold allowed.
77 * Type is {@code int}.
78 * Default value is {@code 10}.
79 * </li>
80 * <li>
81 * Property {@code switchBlockAsSingleDecisionPoint} - Control whether to treat
82 * the whole switch block as a single decision point.
83 * Type is {@code boolean}.
84 * Default value is {@code false}.
85 * </li>
86 * <li>
87 * Property {@code tokens} - tokens to check
88 * Type is {@code java.lang.String[]}.
89 * Validation type is {@code tokenSet}.
90 * Default value is:
91 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_WHILE">
92 * LITERAL_WHILE</a>,
93 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_DO">
94 * LITERAL_DO</a>,
95 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_FOR">
96 * LITERAL_FOR</a>,
97 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_IF">
98 * LITERAL_IF</a>,
99 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_SWITCH">
100 * LITERAL_SWITCH</a>,
101 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_CASE">
102 * LITERAL_CASE</a>,
103 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_CATCH">
104 * LITERAL_CATCH</a>,
105 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#QUESTION">
106 * QUESTION</a>,
107 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LAND">
108 * LAND</a>,
109 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LOR">
110 * LOR</a>,
111 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_WHEN">
112 * LITERAL_WHEN</a>.
113 * </li>
114 * </ul>
115 *
116 * <p>
117 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
118 * </p>
119 *
120 * <p>
121 * Violation Message Keys:
122 * </p>
123 * <ul>
124 * <li>
125 * {@code cyclomaticComplexity}
126 * </li>
127 * </ul>
128 *
129 * @since 3.2
130 */
131 @FileStatefulCheck
132 public class CyclomaticComplexityCheck
133 extends AbstractCheck {
134
135 /**
136 * A key is pointing to the warning message text in "messages.properties"
137 * file.
138 */
139 public static final String MSG_KEY = "cyclomaticComplexity";
140
141 /** The initial current value. */
142 private static final BigInteger INITIAL_VALUE = BigInteger.ONE;
143
144 /** Default allowed complexity. */
145 private static final int DEFAULT_COMPLEXITY_VALUE = 10;
146
147 /** Stack of values - all but the current value. */
148 private final Deque<BigInteger> valueStack = new ArrayDeque<>();
149
150 /** Control whether to treat the whole switch block as a single decision point. */
151 private boolean switchBlockAsSingleDecisionPoint;
152
153 /** The current value. */
154 private BigInteger currentValue = INITIAL_VALUE;
155
156 /** Specify the maximum threshold allowed. */
157 private int max = DEFAULT_COMPLEXITY_VALUE;
158
159 /**
160 * Setter to control whether to treat the whole switch block as a single decision point.
161 *
162 * @param switchBlockAsSingleDecisionPoint whether to treat the whole switch
163 * block as a single decision point.
164 * @since 6.11
165 */
166 public void setSwitchBlockAsSingleDecisionPoint(boolean switchBlockAsSingleDecisionPoint) {
167 this.switchBlockAsSingleDecisionPoint = switchBlockAsSingleDecisionPoint;
168 }
169
170 /**
171 * Setter to specify the maximum threshold allowed.
172 *
173 * @param max the maximum threshold
174 * @since 3.2
175 */
176 public final void setMax(int max) {
177 this.max = max;
178 }
179
180 @Override
181 public int[] getDefaultTokens() {
182 return new int[] {
183 TokenTypes.CTOR_DEF,
184 TokenTypes.METHOD_DEF,
185 TokenTypes.INSTANCE_INIT,
186 TokenTypes.STATIC_INIT,
187 TokenTypes.LITERAL_WHILE,
188 TokenTypes.LITERAL_DO,
189 TokenTypes.LITERAL_FOR,
190 TokenTypes.LITERAL_IF,
191 TokenTypes.LITERAL_SWITCH,
192 TokenTypes.LITERAL_CASE,
193 TokenTypes.LITERAL_CATCH,
194 TokenTypes.QUESTION,
195 TokenTypes.LAND,
196 TokenTypes.LOR,
197 TokenTypes.COMPACT_CTOR_DEF,
198 TokenTypes.LITERAL_WHEN,
199 };
200 }
201
202 @Override
203 public int[] getAcceptableTokens() {
204 return new int[] {
205 TokenTypes.CTOR_DEF,
206 TokenTypes.METHOD_DEF,
207 TokenTypes.INSTANCE_INIT,
208 TokenTypes.STATIC_INIT,
209 TokenTypes.LITERAL_WHILE,
210 TokenTypes.LITERAL_DO,
211 TokenTypes.LITERAL_FOR,
212 TokenTypes.LITERAL_IF,
213 TokenTypes.LITERAL_SWITCH,
214 TokenTypes.LITERAL_CASE,
215 TokenTypes.LITERAL_CATCH,
216 TokenTypes.QUESTION,
217 TokenTypes.LAND,
218 TokenTypes.LOR,
219 TokenTypes.COMPACT_CTOR_DEF,
220 TokenTypes.LITERAL_WHEN,
221 };
222 }
223
224 @Override
225 public final int[] getRequiredTokens() {
226 return new int[] {
227 TokenTypes.CTOR_DEF,
228 TokenTypes.METHOD_DEF,
229 TokenTypes.INSTANCE_INIT,
230 TokenTypes.STATIC_INIT,
231 TokenTypes.COMPACT_CTOR_DEF,
232 };
233 }
234
235 @Override
236 public void visitToken(DetailAST ast) {
237 switch (ast.getType()) {
238 case TokenTypes.CTOR_DEF:
239 case TokenTypes.METHOD_DEF:
240 case TokenTypes.INSTANCE_INIT:
241 case TokenTypes.STATIC_INIT:
242 case TokenTypes.COMPACT_CTOR_DEF:
243 visitMethodDef();
244 break;
245 default:
246 visitTokenHook(ast);
247 }
248 }
249
250 @Override
251 public void leaveToken(DetailAST ast) {
252 switch (ast.getType()) {
253 case TokenTypes.CTOR_DEF:
254 case TokenTypes.METHOD_DEF:
255 case TokenTypes.INSTANCE_INIT:
256 case TokenTypes.STATIC_INIT:
257 case TokenTypes.COMPACT_CTOR_DEF:
258 leaveMethodDef(ast);
259 break;
260 default:
261 break;
262 }
263 }
264
265 /**
266 * Hook called when visiting a token. Will not be called the method
267 * definition tokens.
268 *
269 * @param ast the token being visited
270 */
271 private void visitTokenHook(DetailAST ast) {
272 if (switchBlockAsSingleDecisionPoint) {
273 if (ast.getType() != TokenTypes.LITERAL_CASE) {
274 incrementCurrentValue(BigInteger.ONE);
275 }
276 }
277 else if (ast.getType() != TokenTypes.LITERAL_SWITCH) {
278 incrementCurrentValue(BigInteger.ONE);
279 }
280 }
281
282 /**
283 * Process the end of a method definition.
284 *
285 * @param ast the token representing the method definition
286 */
287 private void leaveMethodDef(DetailAST ast) {
288 final BigInteger bigIntegerMax = BigInteger.valueOf(max);
289 if (currentValue.compareTo(bigIntegerMax) > 0) {
290 log(ast, MSG_KEY, currentValue, bigIntegerMax);
291 }
292 popValue();
293 }
294
295 /**
296 * Increments the current value by a specified amount.
297 *
298 * @param amount the amount to increment by
299 */
300 private void incrementCurrentValue(BigInteger amount) {
301 currentValue = currentValue.add(amount);
302 }
303
304 /** Push the current value on the stack. */
305 private void pushValue() {
306 valueStack.push(currentValue);
307 currentValue = INITIAL_VALUE;
308 }
309
310 /**
311 * Pops a value off the stack and makes it the current value.
312 */
313 private void popValue() {
314 currentValue = valueStack.pop();
315 }
316
317 /** Process the start of the method definition. */
318 private void visitMethodDef() {
319 pushValue();
320 }
321
322 }