1 package com.puppycrawl.tools.checkstyle.checks.metrics; 20 21 import com.puppycrawl.tools.checkstyle.api.Check; 22 import com.puppycrawl.tools.checkstyle.api.DetailAST; 23 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 24 import java.util.Stack ; 25 26 27 33 public abstract class AbstractComplexityCheck 34 extends Check 35 { 36 37 private static final int INITIAL_VALUE = 1; 38 39 40 private final Stack mValueStack = new Stack (); 41 42 43 private int mCurrentValue; 44 45 46 private int mMax; 47 48 52 public AbstractComplexityCheck(int aMax) 53 { 54 mMax = aMax; 55 } 56 57 60 protected abstract String getMessageID(); 61 62 68 protected void visitTokenHook(DetailAST aAST) 69 { 70 } 71 72 78 protected void leaveTokenHook(DetailAST aAST) 79 { 80 } 81 82 83 public final int[] getRequiredTokens() 84 { 85 return new int[] { 86 TokenTypes.CTOR_DEF, 87 TokenTypes.METHOD_DEF, 88 TokenTypes.INSTANCE_INIT, 89 TokenTypes.STATIC_INIT, 90 }; 91 } 92 93 94 public final int getMax() 95 { 96 return mMax; 97 } 98 99 104 public final void setMax(int aMax) 105 { 106 mMax = aMax; 107 } 108 109 110 public void visitToken(DetailAST aAST) 111 { 112 switch (aAST.getType()) { 113 case TokenTypes.CTOR_DEF: 114 case TokenTypes.METHOD_DEF: 115 case TokenTypes.INSTANCE_INIT: 116 case TokenTypes.STATIC_INIT: 117 visitMethodDef(); 118 break; 119 default: 120 visitTokenHook(aAST); 121 } 122 } 123 124 125 public void leaveToken(DetailAST aAST) 126 { 127 switch (aAST.getType()) { 128 case TokenTypes.CTOR_DEF: 129 case TokenTypes.METHOD_DEF: 130 case TokenTypes.INSTANCE_INIT: 131 case TokenTypes.STATIC_INIT: 132 leaveMethodDef(aAST); 133 break; 134 default: 135 leaveTokenHook(aAST); 136 } 137 } 138 139 142 protected final int getCurrentValue() 143 { 144 return mCurrentValue; 145 } 146 147 151 protected final void setCurrentValue(int aValue) 152 { 153 mCurrentValue = aValue; 154 } 155 156 161 protected final void incrementCurrentValue(int aBy) 162 { 163 setCurrentValue(getCurrentValue() + aBy); 164 } 165 166 167 protected final void pushValue() 168 { 169 mValueStack.push(new Integer (mCurrentValue)); 170 mCurrentValue = INITIAL_VALUE; 171 } 172 173 176 protected final int popValue() 177 { 178 mCurrentValue = ((Integer ) mValueStack.pop()).intValue(); 179 180 return mCurrentValue; 181 } 182 183 184 private void visitMethodDef() 185 { 186 pushValue(); 187 } 188 189 194 private void leaveMethodDef(DetailAST aAST) 195 { 196 if (mCurrentValue > mMax) { 197 log(aAST, getMessageID(), 198 new Integer (mCurrentValue), 199 new Integer (mMax)); 200 } 201 popValue(); 202 } 203 } 204 | Popular Tags |