001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2020 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.FileStatefulCheck; 027import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 028import com.puppycrawl.tools.checkstyle.api.DetailAST; 029import com.puppycrawl.tools.checkstyle.api.TokenTypes; 030 031/** 032 * <p> 033 * Checks the NPATH complexity against a specified limit. 034 * </p> 035 * <p> 036 * The NPATH metric computes the number of possible execution paths through a 037 * function(method). It takes into account the nesting of conditional statements 038 * and multi-part boolean expressions (A && B, C || D, E ? F :G and 039 * their combinations). 040 * </p> 041 * <p> 042 * The NPATH metric was designed base on Cyclomatic complexity to avoid problem 043 * of Cyclomatic complexity metric like nesting level within a function(method). 044 * </p> 045 * <p> 046 * Metric was described at <a href="http://dl.acm.org/citation.cfm?id=42379"> 047 * "NPATH: a measure of execution pathcomplexity and its applications"</a>. 048 * If you need detailed description of algorithm, please read that article, 049 * it is well written and have number of examples and details. 050 * </p> 051 * <p> 052 * Here is some quotes: 053 * </p> 054 * <blockquote> 055 * An NPATH threshold value of 200 has been established for a function. 056 * The value 200 is based on studies done at AT&T Bell Laboratories [1988 year]. 057 * </blockquote> 058 * <blockquote> 059 * Some of the most effective methods of reducing the NPATH value include: 060 * <ul> 061 * <li> 062 * distributing functionality; 063 * </li> 064 * <li> 065 * implementing multiple if statements as a switch statement; 066 * </li> 067 * <li> 068 * creating a separate function for logical expressions with a high count of 069 * variables and (&&) and or (||) operators. 070 * </li> 071 * </ul> 072 * </blockquote> 073 * <blockquote> 074 * Although strategies to reduce the NPATH complexity of functions are important, 075 * care must be taken not to distort the logical clarity of the software by 076 * applying a strategy to reduce the complexity of functions. That is, there is 077 * a point of diminishing return beyond which a further attempt at reduction of 078 * complexity distorts the logical clarity of the system structure. 079 * </blockquote> 080 * <table> 081 * <caption>Examples</caption> 082 * <thead><tr><th>Structure</th><th>Complexity expression</th></tr></thead> 083 * <tr><td>if ([expr]) { [if-range] }</td><td>NP(if-range) + 1 + NP(expr)</td></tr> 084 * <tr><td>if ([expr]) { [if-range] } else { [else-range] }</td> 085 * <td>NP(if-range)+ NP(else-range) + NP(expr)</td></tr> 086 * <tr><td>while ([expr]) { [while-range] }</td><td>NP(while-range) + NP(expr) + 1</td></tr> 087 * <tr><td>do { [do-range] } while ([expr])</td><td>NP(do-range) + NP(expr) + 1</td></tr> 088 * <tr><td>for([expr1]; [expr2]; [expr3]) { [for-range] }</td> 089 * <td>NP(for-range) + NP(expr1)+ NP(expr2) + NP(expr3) + 1</td></tr> 090 * <tr><td>switch ([expr]) { case : [case-range] default: [default-range] }</td> 091 * <td>S(i=1:i=n)NP(case-range[i]) + NP(default-range) + NP(expr)</td></tr> 092 * <tr><td>[expr1] ? [expr2] : [expr3]</td><td>NP(expr1) + NP(expr2) + NP(expr3) + 2</td></tr> 093 * <tr><td>goto label</td><td>1</td></tr><tr><td>break</td><td>1</td></tr> 094 * <tr><td>Expressions</td> 095 * <td>Number of && and || operators in expression. No operators - 0</td></tr> 096 * <tr><td>continue</td><td>1</td></tr><tr><td>return</td><td>1</td></tr> 097 * <tr><td>Statement (even sequential statements)</td><td>1</td></tr> 098 * <tr><td>Empty block {}</td><td>1</td></tr><tr><td>Function call</td><td>1</td> 099 * </tr><tr><td>Function(Method) declaration or Block</td><td>P(i=1:i=N)NP(Statement[i])</td></tr> 100 * </table> 101 * <p> 102 * <b>Rationale:</b> Nejmeh says that his group had an informal NPATH limit of 103 * 200 on individual routines; functions(methods) that exceeded this value were 104 * candidates for further decomposition - or at least a closer look. 105 * <b>Please do not be fanatic with limit 200</b> - choose number that suites 106 * your project style. Limit 200 is empirical number base on some sources of at 107 * AT&T Bell Laboratories of 1988 year. 108 * </p> 109 * <ul> 110 * <li> 111 * Property {@code max} - Specify the maximum threshold allowed. 112 * Default value is {@code 200}. 113 * </li> 114 * </ul> 115 * <p> 116 * To configure the check: 117 * </p> 118 * <pre> 119 * <module name="NPathComplexity"/> 120 * </pre> 121 * <p> 122 * Example: 123 * </p> 124 * <pre> 125 * public abstract class Test { 126 * 127 * final int a = 0; 128 * int b = 0; 129 * 130 * public void foo() { // OK, NPath complexity is less than default threshold 131 * // function consists of one if-else block with an NPath Complexity of 3 132 * if (a > 10) { 133 * if (a > b) { // nested if-else decision tree adds 2 to the complexity count 134 * buzz(); 135 * } else { 136 * fizz(); 137 * } 138 * } else { // last possible outcome of the main if-else block, adds 1 to complexity 139 * buzz(); 140 * } 141 * } 142 * 143 * public void boo() { // violation, NPath complexity is 217 (max allowed is 200) 144 * // looping through 3 switch statements produces 6^3 + 1 (217) possible outcomes 145 * for(int i = 0; i < b; i++) { // for statement adds 1 to final complexity 146 * switch(i) { // each independent switch statement multiplies complexity by 6 147 * case a: 148 * // ternary with && adds 3 to switch's complexity 149 * print(f(i) && g(i) ? fizz() : buzz()); 150 * default: 151 * // ternary with || adds 3 to switch's complexity 152 * print(f(i) || g(i) ? fizz() : buzz()); 153 * } 154 * switch(i - 1) { // multiplies complexity by 6 155 * case a: 156 * print(f(i) && g(i) ? fizz() : buzz()); 157 * default: 158 * print(f(i) || g(i) ? fizz() : buzz()); 159 * } 160 * switch(i + 1) { // multiplies complexity by 6 161 * case a: 162 * print(f(i) && g(i) ? fizz() : buzz()); 163 * default: 164 * print(f(i) || g(i) ? fizz() : buzz()); 165 * } 166 * } 167 * } 168 * 169 * public abstract boolean f(int x); 170 * public abstract boolean g(int x); 171 * public abstract String fizz(); 172 * public abstract String buzz(); 173 * public abstract void print(String str); 174 * } 175 * </pre> 176 * <p> 177 * To configure the check with a threshold of 100: 178 * </p> 179 * <pre> 180 * <module name="NPathComplexity"> 181 * <property name="max" value="100"/> 182 * </module> 183 * </pre> 184 * <p> 185 * Example: 186 * </p> 187 * <pre> 188 * public abstract class Test1 { 189 * public void foo() { // violation, NPath complexity is 128 (max allowed is 100) 190 * int a,b,t,m,n; 191 * a=b=t=m=n = 0; 192 * 193 * // Complexity is achieved by choosing from 2 options 7 times (2^7 = 128 possible outcomes) 194 * if (a > b) { // non-nested if-else decision tree multiplies complexity by 2 195 * bar(); 196 * } else { 197 * baz(); 198 * } 199 * 200 * print(t > 1 ? bar() : baz()); // 5 ternary statements multiply complexity by 2^5 201 * print(t > 2 ? bar() : baz()); 202 * print(t > 3 ? bar() : baz()); 203 * print(t > 4 ? bar() : baz()); 204 * print(t > 5 ? bar() : baz()); 205 * 206 * if (m > n) { // multiplies complexity by 2 207 * baz(); 208 * } else { 209 * bar(); 210 * } 211 * } 212 * 213 * public abstract String bar(); 214 * public abstract String baz(); 215 * public abstract void print(String str); 216 * } 217 * </pre> 218 * 219 * @since 3.4 220 */ 221// -@cs[AbbreviationAsWordInName] Can't change check name 222@FileStatefulCheck 223public final class NPathComplexityCheck extends AbstractCheck { 224 225 /** 226 * A key is pointing to the warning message text in "messages.properties" 227 * file. 228 */ 229 public static final String MSG_KEY = "npathComplexity"; 230 231 /** Default allowed complexity. */ 232 private static final int DEFAULT_MAX = 200; 233 234 /** The initial current value. */ 235 private static final BigInteger INITIAL_VALUE = BigInteger.ZERO; 236 237 /** 238 * Stack of NP values for ranges. 239 */ 240 private final Deque<BigInteger> rangeValues = new ArrayDeque<>(); 241 242 /** Stack of NP values for expressions. */ 243 private final Deque<Integer> expressionValues = new ArrayDeque<>(); 244 245 /** Stack of belongs to range values for question operator. */ 246 private final Deque<Boolean> afterValues = new ArrayDeque<>(); 247 248 /** 249 * Range of the last processed expression. Used for checking that ternary operation 250 * which is a part of expression won't be processed for second time. 251 */ 252 private final TokenEnd processingTokenEnd = new TokenEnd(); 253 254 /** NP value for current range. */ 255 private BigInteger currentRangeValue = INITIAL_VALUE; 256 257 /** Specify the maximum threshold allowed. */ 258 private int max = DEFAULT_MAX; 259 260 /** True, when branch is visited, but not leaved. */ 261 private boolean branchVisited; 262 263 /** 264 * Setter to specify the maximum threshold allowed. 265 * 266 * @param max the maximum threshold 267 */ 268 public void setMax(int max) { 269 this.max = max; 270 } 271 272 @Override 273 public int[] getDefaultTokens() { 274 return getRequiredTokens(); 275 } 276 277 @Override 278 public int[] getAcceptableTokens() { 279 return getRequiredTokens(); 280 } 281 282 @Override 283 public int[] getRequiredTokens() { 284 return new int[] { 285 TokenTypes.CTOR_DEF, 286 TokenTypes.METHOD_DEF, 287 TokenTypes.STATIC_INIT, 288 TokenTypes.INSTANCE_INIT, 289 TokenTypes.LITERAL_WHILE, 290 TokenTypes.LITERAL_DO, 291 TokenTypes.LITERAL_FOR, 292 TokenTypes.LITERAL_IF, 293 TokenTypes.LITERAL_ELSE, 294 TokenTypes.LITERAL_SWITCH, 295 TokenTypes.CASE_GROUP, 296 TokenTypes.LITERAL_TRY, 297 TokenTypes.LITERAL_CATCH, 298 TokenTypes.QUESTION, 299 TokenTypes.LITERAL_RETURN, 300 TokenTypes.LITERAL_DEFAULT, 301 }; 302 } 303 304 @Override 305 public void beginTree(DetailAST rootAST) { 306 rangeValues.clear(); 307 expressionValues.clear(); 308 afterValues.clear(); 309 processingTokenEnd.reset(); 310 currentRangeValue = INITIAL_VALUE; 311 branchVisited = false; 312 } 313 314 @Override 315 public void visitToken(DetailAST ast) { 316 switch (ast.getType()) { 317 case TokenTypes.LITERAL_IF: 318 case TokenTypes.LITERAL_SWITCH: 319 case TokenTypes.LITERAL_WHILE: 320 case TokenTypes.LITERAL_DO: 321 case TokenTypes.LITERAL_FOR: 322 visitConditional(ast, 1); 323 break; 324 case TokenTypes.QUESTION: 325 visitUnitaryOperator(ast, 2); 326 break; 327 case TokenTypes.LITERAL_RETURN: 328 visitUnitaryOperator(ast, 0); 329 break; 330 case TokenTypes.CASE_GROUP: 331 final int caseNumber = countCaseTokens(ast); 332 branchVisited = true; 333 pushValue(caseNumber); 334 break; 335 case TokenTypes.LITERAL_ELSE: 336 branchVisited = true; 337 if (currentRangeValue.equals(BigInteger.ZERO)) { 338 currentRangeValue = BigInteger.ONE; 339 } 340 pushValue(0); 341 break; 342 case TokenTypes.LITERAL_TRY: 343 case TokenTypes.LITERAL_CATCH: 344 case TokenTypes.LITERAL_DEFAULT: 345 pushValue(1); 346 break; 347 case TokenTypes.CTOR_DEF: 348 case TokenTypes.METHOD_DEF: 349 case TokenTypes.INSTANCE_INIT: 350 case TokenTypes.STATIC_INIT: 351 pushValue(0); 352 break; 353 default: 354 break; 355 } 356 } 357 358 @Override 359 public void leaveToken(DetailAST ast) { 360 switch (ast.getType()) { 361 case TokenTypes.LITERAL_WHILE: 362 case TokenTypes.LITERAL_DO: 363 case TokenTypes.LITERAL_FOR: 364 case TokenTypes.LITERAL_IF: 365 case TokenTypes.LITERAL_SWITCH: 366 leaveConditional(); 367 break; 368 case TokenTypes.LITERAL_TRY: 369 leaveMultiplyingConditional(); 370 break; 371 case TokenTypes.LITERAL_RETURN: 372 case TokenTypes.QUESTION: 373 leaveUnitaryOperator(); 374 break; 375 case TokenTypes.LITERAL_CATCH: 376 leaveAddingConditional(); 377 break; 378 case TokenTypes.LITERAL_DEFAULT: 379 leaveBranch(); 380 break; 381 case TokenTypes.LITERAL_ELSE: 382 case TokenTypes.CASE_GROUP: 383 leaveBranch(); 384 branchVisited = false; 385 break; 386 case TokenTypes.CTOR_DEF: 387 case TokenTypes.METHOD_DEF: 388 case TokenTypes.INSTANCE_INIT: 389 case TokenTypes.STATIC_INIT: 390 leaveMethodDef(ast); 391 break; 392 default: 393 break; 394 } 395 } 396 397 /** 398 * Visits if, while, do-while, for and switch tokens - all of them have expression in 399 * parentheses which is used for calculation. 400 * 401 * @param ast visited token. 402 * @param basicBranchingFactor default number of branches added. 403 */ 404 private void visitConditional(DetailAST ast, int basicBranchingFactor) { 405 int expressionValue = basicBranchingFactor; 406 DetailAST bracketed; 407 for (bracketed = ast.findFirstToken(TokenTypes.LPAREN).getNextSibling(); 408 bracketed.getType() != TokenTypes.RPAREN; 409 bracketed = bracketed.getNextSibling()) { 410 expressionValue += countConditionalOperators(bracketed); 411 } 412 processingTokenEnd.setToken(bracketed); 413 pushValue(expressionValue); 414 } 415 416 /** 417 * Visits ternary operator (?:) and return tokens. They differ from those processed by 418 * visitConditional method in that their expression isn't bracketed. 419 * 420 * @param ast visited token. 421 * @param basicBranchingFactor number of branches inherently added by this token. 422 */ 423 private void visitUnitaryOperator(DetailAST ast, int basicBranchingFactor) { 424 final boolean isAfter = processingTokenEnd.isAfter(ast); 425 afterValues.push(isAfter); 426 if (!isAfter) { 427 processingTokenEnd.setToken(getLastToken(ast)); 428 final int expressionValue = basicBranchingFactor + countConditionalOperators(ast); 429 pushValue(expressionValue); 430 } 431 } 432 433 /** 434 * Leaves ternary operator (?:) and return tokens. 435 */ 436 private void leaveUnitaryOperator() { 437 if (Boolean.FALSE.equals(afterValues.pop())) { 438 final Values valuePair = popValue(); 439 BigInteger basicRangeValue = valuePair.getRangeValue(); 440 BigInteger expressionValue = valuePair.getExpressionValue(); 441 if (expressionValue.equals(BigInteger.ZERO)) { 442 expressionValue = BigInteger.ONE; 443 } 444 if (basicRangeValue.equals(BigInteger.ZERO)) { 445 basicRangeValue = BigInteger.ONE; 446 } 447 currentRangeValue = currentRangeValue.add(expressionValue).multiply(basicRangeValue); 448 } 449 } 450 451 /** Leaves while, do, for, if, ternary (?::), return or switch. */ 452 private void leaveConditional() { 453 final Values valuePair = popValue(); 454 final BigInteger expressionValue = valuePair.getExpressionValue(); 455 BigInteger basicRangeValue = valuePair.getRangeValue(); 456 if (currentRangeValue.equals(BigInteger.ZERO)) { 457 currentRangeValue = BigInteger.ONE; 458 } 459 if (basicRangeValue.equals(BigInteger.ZERO)) { 460 basicRangeValue = BigInteger.ONE; 461 } 462 currentRangeValue = currentRangeValue.add(expressionValue).multiply(basicRangeValue); 463 } 464 465 /** Leaves else, default or case group tokens. */ 466 private void leaveBranch() { 467 final Values valuePair = popValue(); 468 final BigInteger basicRangeValue = valuePair.getRangeValue(); 469 final BigInteger expressionValue = valuePair.getExpressionValue(); 470 if (branchVisited && currentRangeValue.equals(BigInteger.ZERO)) { 471 currentRangeValue = BigInteger.ONE; 472 } 473 currentRangeValue = currentRangeValue.subtract(BigInteger.ONE) 474 .add(basicRangeValue) 475 .add(expressionValue); 476 } 477 478 /** 479 * Process the end of a method definition. 480 * 481 * @param ast the token type representing the method definition 482 */ 483 private void leaveMethodDef(DetailAST ast) { 484 final BigInteger bigIntegerMax = BigInteger.valueOf(max); 485 if (currentRangeValue.compareTo(bigIntegerMax) > 0) { 486 log(ast, MSG_KEY, currentRangeValue, bigIntegerMax); 487 } 488 popValue(); 489 currentRangeValue = INITIAL_VALUE; 490 } 491 492 /** Leaves catch. */ 493 private void leaveAddingConditional() { 494 currentRangeValue = currentRangeValue.add(popValue().getRangeValue().add(BigInteger.ONE)); 495 } 496 497 /** 498 * Pushes the current range value on the range value stack. Pushes this token expression value 499 * on the expression value stack. 500 * 501 * @param expressionValue value of expression calculated for current token. 502 */ 503 private void pushValue(Integer expressionValue) { 504 rangeValues.push(currentRangeValue); 505 expressionValues.push(expressionValue); 506 currentRangeValue = INITIAL_VALUE; 507 } 508 509 /** 510 * Pops values from both stack of expression values and stack of range values. 511 * 512 * @return pair of head values from both of the stacks. 513 */ 514 private Values popValue() { 515 final int expressionValue = expressionValues.pop(); 516 return new Values(rangeValues.pop(), BigInteger.valueOf(expressionValue)); 517 } 518 519 /** Leaves try. */ 520 private void leaveMultiplyingConditional() { 521 currentRangeValue = currentRangeValue.add(BigInteger.ONE) 522 .multiply(popValue().getRangeValue().add(BigInteger.ONE)); 523 } 524 525 /** 526 * Calculates number of conditional operators, including inline ternary operator, for a token. 527 * 528 * @param ast inspected token. 529 * @return number of conditional operators. 530 * @see <a href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.23"> 531 * Java Language Specification, §15.23</a> 532 * @see <a href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.24"> 533 * Java Language Specification, §15.24</a> 534 * @see <a href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.25"> 535 * Java Language Specification, §15.25</a> 536 */ 537 private static int countConditionalOperators(DetailAST ast) { 538 int number = 0; 539 for (DetailAST child = ast.getFirstChild(); child != null; 540 child = child.getNextSibling()) { 541 final int type = child.getType(); 542 if (type == TokenTypes.LOR || type == TokenTypes.LAND) { 543 number++; 544 } 545 else if (type == TokenTypes.QUESTION) { 546 number += 2; 547 } 548 number += countConditionalOperators(child); 549 } 550 return number; 551 } 552 553 /** 554 * Finds a leaf, which is the most distant from the root. 555 * 556 * @param ast the root of tree. 557 * @return the leaf. 558 */ 559 private static DetailAST getLastToken(DetailAST ast) { 560 final DetailAST lastChild = ast.getLastChild(); 561 final DetailAST result; 562 if (lastChild.getFirstChild() == null) { 563 result = lastChild; 564 } 565 else { 566 result = getLastToken(lastChild); 567 } 568 return result; 569 } 570 571 /** 572 * Counts number of case tokens subject to a case group token. 573 * 574 * @param ast case group token. 575 * @return number of case tokens. 576 */ 577 private static int countCaseTokens(DetailAST ast) { 578 int counter = 0; 579 for (DetailAST iterator = ast.getFirstChild(); iterator != null; 580 iterator = iterator.getNextSibling()) { 581 if (iterator.getType() == TokenTypes.LITERAL_CASE) { 582 counter++; 583 } 584 } 585 return counter; 586 } 587 588 /** 589 * Coordinates of token end. Used to prevent inline ternary 590 * operator from being processed twice. 591 */ 592 private static class TokenEnd { 593 594 /** End line of token. */ 595 private int endLineNo; 596 597 /** End column of token. */ 598 private int endColumnNo; 599 600 /** 601 * Sets end coordinates from given token. 602 * 603 * @param endToken token. 604 */ 605 public void setToken(DetailAST endToken) { 606 if (!isAfter(endToken)) { 607 endLineNo = endToken.getLineNo(); 608 endColumnNo = endToken.getColumnNo(); 609 } 610 } 611 612 /** Sets end token coordinates to the start of the file. */ 613 public void reset() { 614 endLineNo = 0; 615 endColumnNo = 0; 616 } 617 618 /** 619 * Checks if saved coordinates located after given token. 620 * 621 * @param ast given token. 622 * @return true, if saved coordinates located after given token. 623 */ 624 public boolean isAfter(DetailAST ast) { 625 final int lineNo = ast.getLineNo(); 626 final int columnNo = ast.getColumnNo(); 627 boolean isAfter = true; 628 if (lineNo > endLineNo 629 || lineNo == endLineNo 630 && columnNo > endColumnNo) { 631 isAfter = false; 632 } 633 return isAfter; 634 } 635 636 } 637 638 /** 639 * Class that store range value and expression value. 640 */ 641 private static class Values { 642 643 /** NP value for range. */ 644 private final BigInteger rangeValue; 645 646 /** NP value for expression. */ 647 private final BigInteger expressionValue; 648 649 /** 650 * Constructor that assigns all of class fields. 651 * 652 * @param valueOfRange NP value for range 653 * @param valueOfExpression NP value for expression 654 */ 655 /* package */ Values(BigInteger valueOfRange, BigInteger valueOfExpression) { 656 rangeValue = valueOfRange; 657 expressionValue = valueOfExpression; 658 } 659 660 /** 661 * Returns NP value for range. 662 * 663 * @return NP value for range 664 */ 665 public BigInteger getRangeValue() { 666 return rangeValue; 667 } 668 669 /** 670 * Returns NP value for expression. 671 * 672 * @return NP value for expression 673 */ 674 public BigInteger getExpressionValue() { 675 return expressionValue; 676 } 677 678 } 679 680}