1 package com.puppycrawl.tools.checkstyle.checks.metrics; 20 21 import com.puppycrawl.tools.checkstyle.api.DetailAST; 22 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 23 24 36 public final class NPathComplexityCheck extends AbstractComplexityCheck 37 { 38 39 private static final int DEFAULT_MAX = 200; 40 41 42 public NPathComplexityCheck() 43 { 44 super(DEFAULT_MAX); 45 } 46 47 48 public int[] getDefaultTokens() 49 { 50 return new int[] { 51 TokenTypes.CTOR_DEF, 52 TokenTypes.METHOD_DEF, 53 TokenTypes.STATIC_INIT, 54 TokenTypes.INSTANCE_INIT, 55 TokenTypes.LITERAL_WHILE, 56 TokenTypes.LITERAL_DO, 57 TokenTypes.LITERAL_FOR, 58 TokenTypes.LITERAL_IF, 59 TokenTypes.LITERAL_ELSE, 60 TokenTypes.LITERAL_SWITCH, 61 TokenTypes.LITERAL_CASE, 62 TokenTypes.LITERAL_TRY, 63 TokenTypes.LITERAL_CATCH, 64 TokenTypes.QUESTION, 65 }; 66 } 67 68 69 public void visitToken(DetailAST aAST) 70 { 71 switch (aAST.getType()) { 72 case TokenTypes.LITERAL_WHILE: 73 case TokenTypes.LITERAL_DO: 74 case TokenTypes.LITERAL_FOR: 75 case TokenTypes.LITERAL_IF: 76 case TokenTypes.QUESTION: 77 case TokenTypes.LITERAL_TRY: 78 case TokenTypes.LITERAL_SWITCH: 79 visitMultiplyingConditional(); 80 break; 81 case TokenTypes.LITERAL_ELSE: 82 case TokenTypes.LITERAL_CATCH: 83 case TokenTypes.LITERAL_CASE: 84 visitAddingConditional(); 85 break; 86 default: 87 super.visitToken(aAST); 88 } 89 } 90 91 92 public void leaveToken(DetailAST aAST) 93 { 94 switch (aAST.getType()) { 95 case TokenTypes.LITERAL_WHILE: 96 case TokenTypes.LITERAL_DO: 97 case TokenTypes.LITERAL_FOR: 98 case TokenTypes.LITERAL_IF: 99 case TokenTypes.QUESTION: 100 case TokenTypes.LITERAL_TRY: 101 case TokenTypes.LITERAL_SWITCH: 102 leaveMultiplyingConditional(); 103 break; 104 case TokenTypes.LITERAL_ELSE: 105 case TokenTypes.LITERAL_CATCH: 106 case TokenTypes.LITERAL_CASE: 107 leaveAddingConditional(); 108 break; 109 default: 110 super.leaveToken(aAST); 111 } 112 } 113 114 115 protected String getMessageID() 116 { 117 return "npathComplexity"; 118 } 119 120 121 private void visitAddingConditional() 122 { 123 pushValue(); 124 } 125 126 127 private void leaveAddingConditional() 128 { 129 setCurrentValue((getCurrentValue() - 1) + popValue()); 130 } 131 132 133 private void visitMultiplyingConditional() 134 { 135 pushValue(); 136 } 137 138 139 private void leaveMultiplyingConditional() 140 { 141 setCurrentValue((getCurrentValue() + 1) * popValue()); 142 } 143 } 144 | Popular Tags |