001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2017 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018//////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.checks.metrics; 021 022import java.math.BigInteger; 023import java.util.ArrayDeque; 024import java.util.Deque; 025 026import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 027import com.puppycrawl.tools.checkstyle.api.DetailAST; 028import com.puppycrawl.tools.checkstyle.api.TokenTypes; 029 030/** 031 * Checks cyclomatic complexity against a specified limit. The complexity is 032 * measured by the number of "if", "while", "do", "for", "?:", "catch", 033 * "switch", "case", "&&" and "||" statements (plus one) in the body of 034 * the member. It is a measure of the minimum number of possible paths through 035 * the source and therefore the number of required tests. Generally 1-4 is 036 * considered good, 5-7 ok, 8-10 consider re-factoring, and 11+ re-factor now! 037 * 038 * <p>Check has following properties: 039 * 040 * <p><b>switchBlockAsSingleDecisionPoint</b> - controls whether to treat the whole switch 041 * block as a single decision point. Default value is <b>false</b> 042 * 043 * 044 * @author <a href="mailto:simon@redhillconsulting.com.au">Simon Harris</a> 045 * @author Oliver Burn 046 * @author <a href="mailto:andreyselkin@gmail.com">Andrei Selkin</a> 047 */ 048public class CyclomaticComplexityCheck 049 extends AbstractCheck { 050 051 /** 052 * A key is pointing to the warning message text in "messages.properties" 053 * file. 054 */ 055 public static final String MSG_KEY = "cyclomaticComplexity"; 056 057 /** The initial current value. */ 058 private static final BigInteger INITIAL_VALUE = BigInteger.ONE; 059 060 /** Default allowed complexity. */ 061 private static final int DEFAULT_COMPLEXITY_VALUE = 10; 062 063 /** Stack of values - all but the current value. */ 064 private final Deque<BigInteger> valueStack = new ArrayDeque<>(); 065 066 /** Whether to treat the whole switch block as a single decision point.*/ 067 private boolean switchBlockAsSingleDecisionPoint; 068 069 /** The current value. */ 070 private BigInteger currentValue = INITIAL_VALUE; 071 072 /** Threshold to report error for. */ 073 private int max = DEFAULT_COMPLEXITY_VALUE; 074 075 /** 076 * Sets whether to treat the whole switch block as a single decision point. 077 * @param switchBlockAsSingleDecisionPoint whether to treat the whole switch 078 * block as a single decision point. 079 */ 080 public void setSwitchBlockAsSingleDecisionPoint(boolean switchBlockAsSingleDecisionPoint) { 081 this.switchBlockAsSingleDecisionPoint = switchBlockAsSingleDecisionPoint; 082 } 083 084 /** 085 * Set the maximum threshold allowed. 086 * 087 * @param max the maximum threshold 088 */ 089 public final void setMax(int max) { 090 this.max = max; 091 } 092 093 @Override 094 public int[] getDefaultTokens() { 095 return new int[] { 096 TokenTypes.CTOR_DEF, 097 TokenTypes.METHOD_DEF, 098 TokenTypes.INSTANCE_INIT, 099 TokenTypes.STATIC_INIT, 100 TokenTypes.LITERAL_WHILE, 101 TokenTypes.LITERAL_DO, 102 TokenTypes.LITERAL_FOR, 103 TokenTypes.LITERAL_IF, 104 TokenTypes.LITERAL_SWITCH, 105 TokenTypes.LITERAL_CASE, 106 TokenTypes.LITERAL_CATCH, 107 TokenTypes.QUESTION, 108 TokenTypes.LAND, 109 TokenTypes.LOR, 110 }; 111 } 112 113 @Override 114 public int[] getAcceptableTokens() { 115 return new int[] { 116 TokenTypes.CTOR_DEF, 117 TokenTypes.METHOD_DEF, 118 TokenTypes.INSTANCE_INIT, 119 TokenTypes.STATIC_INIT, 120 TokenTypes.LITERAL_WHILE, 121 TokenTypes.LITERAL_DO, 122 TokenTypes.LITERAL_FOR, 123 TokenTypes.LITERAL_IF, 124 TokenTypes.LITERAL_SWITCH, 125 TokenTypes.LITERAL_CASE, 126 TokenTypes.LITERAL_CATCH, 127 TokenTypes.QUESTION, 128 TokenTypes.LAND, 129 TokenTypes.LOR, 130 }; 131 } 132 133 @Override 134 public final int[] getRequiredTokens() { 135 return new int[] { 136 TokenTypes.CTOR_DEF, 137 TokenTypes.METHOD_DEF, 138 TokenTypes.INSTANCE_INIT, 139 TokenTypes.STATIC_INIT, 140 }; 141 } 142 143 @Override 144 public void visitToken(DetailAST ast) { 145 switch (ast.getType()) { 146 case TokenTypes.CTOR_DEF: 147 case TokenTypes.METHOD_DEF: 148 case TokenTypes.INSTANCE_INIT: 149 case TokenTypes.STATIC_INIT: 150 visitMethodDef(); 151 break; 152 default: 153 visitTokenHook(ast); 154 } 155 } 156 157 @Override 158 public void leaveToken(DetailAST ast) { 159 switch (ast.getType()) { 160 case TokenTypes.CTOR_DEF: 161 case TokenTypes.METHOD_DEF: 162 case TokenTypes.INSTANCE_INIT: 163 case TokenTypes.STATIC_INIT: 164 leaveMethodDef(ast); 165 break; 166 default: 167 break; 168 } 169 } 170 171 /** 172 * Hook called when visiting a token. Will not be called the method 173 * definition tokens. 174 * 175 * @param ast the token being visited 176 */ 177 protected final void visitTokenHook(DetailAST ast) { 178 if (switchBlockAsSingleDecisionPoint) { 179 if (ast.getType() != TokenTypes.LITERAL_CASE) { 180 incrementCurrentValue(BigInteger.ONE); 181 } 182 } 183 else if (ast.getType() != TokenTypes.LITERAL_SWITCH) { 184 incrementCurrentValue(BigInteger.ONE); 185 } 186 } 187 188 /** 189 * Process the end of a method definition. 190 * 191 * @param ast the token representing the method definition 192 */ 193 private void leaveMethodDef(DetailAST ast) { 194 final BigInteger bigIntegerMax = BigInteger.valueOf(max); 195 if (currentValue.compareTo(bigIntegerMax) > 0) { 196 log(ast, MSG_KEY, currentValue, bigIntegerMax); 197 } 198 popValue(); 199 } 200 201 /** 202 * Increments the current value by a specified amount. 203 * 204 * @param amount the amount to increment by 205 */ 206 protected final void incrementCurrentValue(BigInteger amount) { 207 currentValue = currentValue.add(amount); 208 } 209 210 /** Push the current value on the stack. */ 211 protected final void pushValue() { 212 valueStack.push(currentValue); 213 currentValue = INITIAL_VALUE; 214 } 215 216 /** 217 * Pops a value off the stack and makes it the current value. 218 * @return pop a value off the stack and make it the current value 219 */ 220 protected final BigInteger popValue() { 221 currentValue = valueStack.pop(); 222 return currentValue; 223 } 224 225 /** Process the start of the method definition. */ 226 private void visitMethodDef() { 227 pushValue(); 228 } 229}