001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2015 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; 023 024import com.puppycrawl.tools.checkstyle.api.DetailAST; 025import com.puppycrawl.tools.checkstyle.api.TokenTypes; 026 027/** 028 * Checks the npath complexity against a specified limit (default = 200). 029 * The npath metric computes the number of possible execution paths 030 * through a function. Similar to the cyclomatic complexity but also 031 * takes into account the nesting of conditional statements and 032 * multi-part boolean expressions. 033 * 034 * @author <a href="mailto:simon@redhillconsulting.com.au">Simon Harris</a> 035 * @author o_sukhodolsky 036 */ 037public final class NPathComplexityCheck extends AbstractComplexityCheck { 038 039 /** 040 * A key is pointing to the warning message text in "messages.properties" 041 * file. 042 */ 043 public static final String MSG_KEY = "npathComplexity"; 044 045 /** Default allowed complexity. */ 046 private static final int DEFAULT_MAX = 200; 047 048 /** Creates new instance of the check. */ 049 public NPathComplexityCheck() { 050 super(DEFAULT_MAX); 051 } 052 053 @Override 054 public int[] getDefaultTokens() { 055 return new int[] { 056 TokenTypes.CTOR_DEF, 057 TokenTypes.METHOD_DEF, 058 TokenTypes.STATIC_INIT, 059 TokenTypes.INSTANCE_INIT, 060 TokenTypes.LITERAL_WHILE, 061 TokenTypes.LITERAL_DO, 062 TokenTypes.LITERAL_FOR, 063 TokenTypes.LITERAL_IF, 064 TokenTypes.LITERAL_ELSE, 065 TokenTypes.LITERAL_SWITCH, 066 TokenTypes.LITERAL_CASE, 067 TokenTypes.LITERAL_TRY, 068 TokenTypes.LITERAL_CATCH, 069 TokenTypes.QUESTION, 070 }; 071 } 072 073 @Override 074 public int[] getAcceptableTokens() { 075 return new int[] { 076 TokenTypes.CTOR_DEF, 077 TokenTypes.METHOD_DEF, 078 TokenTypes.STATIC_INIT, 079 TokenTypes.INSTANCE_INIT, 080 TokenTypes.LITERAL_WHILE, 081 TokenTypes.LITERAL_DO, 082 TokenTypes.LITERAL_FOR, 083 TokenTypes.LITERAL_IF, 084 TokenTypes.LITERAL_ELSE, 085 TokenTypes.LITERAL_SWITCH, 086 TokenTypes.LITERAL_CASE, 087 TokenTypes.LITERAL_TRY, 088 TokenTypes.LITERAL_CATCH, 089 TokenTypes.QUESTION, 090 }; 091 } 092 093 @Override 094 public void visitToken(DetailAST ast) { 095 switch (ast.getType()) { 096 case TokenTypes.LITERAL_WHILE: 097 case TokenTypes.LITERAL_DO: 098 case TokenTypes.LITERAL_FOR: 099 case TokenTypes.LITERAL_IF: 100 case TokenTypes.QUESTION: 101 case TokenTypes.LITERAL_TRY: 102 case TokenTypes.LITERAL_SWITCH: 103 visitMultiplyingConditional(); 104 break; 105 case TokenTypes.LITERAL_ELSE: 106 case TokenTypes.LITERAL_CATCH: 107 case TokenTypes.LITERAL_CASE: 108 visitAddingConditional(); 109 break; 110 default: 111 super.visitToken(ast); 112 } 113 } 114 115 @Override 116 public void leaveToken(DetailAST ast) { 117 switch (ast.getType()) { 118 case TokenTypes.LITERAL_WHILE: 119 case TokenTypes.LITERAL_DO: 120 case TokenTypes.LITERAL_FOR: 121 case TokenTypes.LITERAL_IF: 122 case TokenTypes.QUESTION: 123 case TokenTypes.LITERAL_TRY: 124 case TokenTypes.LITERAL_SWITCH: 125 leaveMultiplyingConditional(); 126 break; 127 case TokenTypes.LITERAL_ELSE: 128 case TokenTypes.LITERAL_CATCH: 129 case TokenTypes.LITERAL_CASE: 130 leaveAddingConditional(); 131 break; 132 default: 133 super.leaveToken(ast); 134 } 135 } 136 137 @Override 138 protected String getMessageID() { 139 return MSG_KEY; 140 } 141 142 /** Visits else, catch or case. */ 143 private void visitAddingConditional() { 144 pushValue(); 145 } 146 147 /** Leaves else, catch or case. */ 148 private void leaveAddingConditional() { 149 setCurrentValue( 150 getCurrentValue().subtract(BigInteger.ONE).add(popValue())); 151 } 152 153 /** Visits while, do, for, if, try, ? (in ?::) or switch. */ 154 private void visitMultiplyingConditional() { 155 pushValue(); 156 } 157 158 /** Leaves while, do, for, if, try, ? (in ?::) or switch. */ 159 private void leaveMultiplyingConditional() { 160 setCurrentValue( 161 getCurrentValue().add(BigInteger.ONE).multiply(popValue())); 162 } 163}