com.puppycrawl.tools.checkstyle.checks.metrics.CyclomaticComplexityCheck Maven / Gradle / Ivy
Show all versions of checkstyle Show documentation
///////////////////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
// Copyright (C) 2001-2024 the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
///////////////////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.checks.metrics;
import java.math.BigInteger;
import java.util.ArrayDeque;
import java.util.Deque;
import com.puppycrawl.tools.checkstyle.FileStatefulCheck;
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
/**
*
* Checks cyclomatic complexity against a specified limit. It is a measure of
* the minimum number of possible paths through the source and therefore the
* number of required tests, it is not about quality of code! It is only
* applied to methods, c-tors,
*
* static initializers and instance initializers.
*
*
* The complexity is equal to the number of decision points {@code + 1}.
* Decision points:
*
*
* -
* {@code if}, {@code while}, {@code do}, {@code for},
* {@code ?:}, {@code catch}, {@code switch}, {@code case} statements.
*
* -
* Operators {@code &&} and {@code ||} in the body of target.
*
* -
* {@code when} expression in case labels, also known as guards.
*
*
*
* By pure theory level 1-4 is considered easy to test, 5-7 OK, 8-10 consider
* re-factoring to ease testing, and 11+ re-factor now as testing will be painful.
*
*
* When it comes to code quality measurement by this metric level 10 is very
* good level as a ultimate target (that is hard to archive). Do not be ashamed
* to have complexity level 15 or even higher, but keep it below 20 to catch
* really bad-designed code automatically.
*
*
* Please use Suppression to avoid violations on cases that could not be split
* in few methods without damaging readability of code or encapsulation.
*
*
* -
* Property {@code max} - Specify the maximum threshold allowed.
* Type is {@code int}.
* Default value is {@code 10}.
*
* -
* Property {@code switchBlockAsSingleDecisionPoint} - Control whether to treat
* the whole switch block as a single decision point.
* Type is {@code boolean}.
* Default value is {@code false}.
*
* -
* Property {@code tokens} - tokens to check
* Type is {@code java.lang.String[]}.
* Validation type is {@code tokenSet}.
* Default value is:
*
* LITERAL_WHILE,
*
* LITERAL_DO,
*
* LITERAL_FOR,
*
* LITERAL_IF,
*
* LITERAL_SWITCH,
*
* LITERAL_CASE,
*
* LITERAL_CATCH,
*
* QUESTION,
*
* LAND,
*
* LOR,
*
* LITERAL_WHEN.
*
*
*
* Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
*
*
* Violation Message Keys:
*
*
* -
* {@code cyclomaticComplexity}
*
*
*
* @since 3.2
*/
@FileStatefulCheck
public class CyclomaticComplexityCheck
extends AbstractCheck {
/**
* A key is pointing to the warning message text in "messages.properties"
* file.
*/
public static final String MSG_KEY = "cyclomaticComplexity";
/** The initial current value. */
private static final BigInteger INITIAL_VALUE = BigInteger.ONE;
/** Default allowed complexity. */
private static final int DEFAULT_COMPLEXITY_VALUE = 10;
/** Stack of values - all but the current value. */
private final Deque valueStack = new ArrayDeque<>();
/** Control whether to treat the whole switch block as a single decision point. */
private boolean switchBlockAsSingleDecisionPoint;
/** The current value. */
private BigInteger currentValue = INITIAL_VALUE;
/** Specify the maximum threshold allowed. */
private int max = DEFAULT_COMPLEXITY_VALUE;
/**
* Setter to control whether to treat the whole switch block as a single decision point.
*
* @param switchBlockAsSingleDecisionPoint whether to treat the whole switch
* block as a single decision point.
* @since 6.11
*/
public void setSwitchBlockAsSingleDecisionPoint(boolean switchBlockAsSingleDecisionPoint) {
this.switchBlockAsSingleDecisionPoint = switchBlockAsSingleDecisionPoint;
}
/**
* Setter to specify the maximum threshold allowed.
*
* @param max the maximum threshold
* @since 3.2
*/
public final void setMax(int max) {
this.max = max;
}
@Override
public int[] getDefaultTokens() {
return new int[] {
TokenTypes.CTOR_DEF,
TokenTypes.METHOD_DEF,
TokenTypes.INSTANCE_INIT,
TokenTypes.STATIC_INIT,
TokenTypes.LITERAL_WHILE,
TokenTypes.LITERAL_DO,
TokenTypes.LITERAL_FOR,
TokenTypes.LITERAL_IF,
TokenTypes.LITERAL_SWITCH,
TokenTypes.LITERAL_CASE,
TokenTypes.LITERAL_CATCH,
TokenTypes.QUESTION,
TokenTypes.LAND,
TokenTypes.LOR,
TokenTypes.COMPACT_CTOR_DEF,
TokenTypes.LITERAL_WHEN,
};
}
@Override
public int[] getAcceptableTokens() {
return new int[] {
TokenTypes.CTOR_DEF,
TokenTypes.METHOD_DEF,
TokenTypes.INSTANCE_INIT,
TokenTypes.STATIC_INIT,
TokenTypes.LITERAL_WHILE,
TokenTypes.LITERAL_DO,
TokenTypes.LITERAL_FOR,
TokenTypes.LITERAL_IF,
TokenTypes.LITERAL_SWITCH,
TokenTypes.LITERAL_CASE,
TokenTypes.LITERAL_CATCH,
TokenTypes.QUESTION,
TokenTypes.LAND,
TokenTypes.LOR,
TokenTypes.COMPACT_CTOR_DEF,
TokenTypes.LITERAL_WHEN,
};
}
@Override
public final int[] getRequiredTokens() {
return new int[] {
TokenTypes.CTOR_DEF,
TokenTypes.METHOD_DEF,
TokenTypes.INSTANCE_INIT,
TokenTypes.STATIC_INIT,
TokenTypes.COMPACT_CTOR_DEF,
};
}
@Override
public void visitToken(DetailAST ast) {
switch (ast.getType()) {
case TokenTypes.CTOR_DEF:
case TokenTypes.METHOD_DEF:
case TokenTypes.INSTANCE_INIT:
case TokenTypes.STATIC_INIT:
case TokenTypes.COMPACT_CTOR_DEF:
visitMethodDef();
break;
default:
visitTokenHook(ast);
}
}
@Override
public void leaveToken(DetailAST ast) {
switch (ast.getType()) {
case TokenTypes.CTOR_DEF:
case TokenTypes.METHOD_DEF:
case TokenTypes.INSTANCE_INIT:
case TokenTypes.STATIC_INIT:
case TokenTypes.COMPACT_CTOR_DEF:
leaveMethodDef(ast);
break;
default:
break;
}
}
/**
* Hook called when visiting a token. Will not be called the method
* definition tokens.
*
* @param ast the token being visited
*/
private void visitTokenHook(DetailAST ast) {
if (switchBlockAsSingleDecisionPoint) {
if (ast.getType() != TokenTypes.LITERAL_CASE) {
incrementCurrentValue(BigInteger.ONE);
}
}
else if (ast.getType() != TokenTypes.LITERAL_SWITCH) {
incrementCurrentValue(BigInteger.ONE);
}
}
/**
* Process the end of a method definition.
*
* @param ast the token representing the method definition
*/
private void leaveMethodDef(DetailAST ast) {
final BigInteger bigIntegerMax = BigInteger.valueOf(max);
if (currentValue.compareTo(bigIntegerMax) > 0) {
log(ast, MSG_KEY, currentValue, bigIntegerMax);
}
popValue();
}
/**
* Increments the current value by a specified amount.
*
* @param amount the amount to increment by
*/
private void incrementCurrentValue(BigInteger amount) {
currentValue = currentValue.add(amount);
}
/** Push the current value on the stack. */
private void pushValue() {
valueStack.push(currentValue);
currentValue = INITIAL_VALUE;
}
/**
* Pops a value off the stack and makes it the current value.
*/
private void popValue() {
currentValue = valueStack.pop();
}
/** Process the start of the method definition. */
private void visitMethodDef() {
pushValue();
}
}