1 package com.puppycrawl.tools.checkstyle.checks.sizes; 20 21 import java.util.Stack ; 22 23 import com.puppycrawl.tools.checkstyle.api.Check; 24 import com.puppycrawl.tools.checkstyle.api.DetailAST; 25 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 26 27 32 public final class ExecutableStatementCountCheck 33 extends Check 34 { 35 36 private static final int DEFAULT_MAX = 30; 37 38 39 private int mMax; 40 41 42 private final Stack mContextStack = new Stack (); 43 44 45 private Context mContext; 46 47 48 public ExecutableStatementCountCheck() 49 { 50 setMax(DEFAULT_MAX); 51 } 52 53 54 public int[] getDefaultTokens() 55 { 56 return new int[] { 57 TokenTypes.CTOR_DEF, 58 TokenTypes.METHOD_DEF, 59 TokenTypes.INSTANCE_INIT, 60 TokenTypes.STATIC_INIT, 61 TokenTypes.SLIST, 62 }; 63 } 64 65 66 public int[] getRequiredTokens() 67 { 68 return new int[] {TokenTypes.SLIST}; 69 } 70 71 75 public int getMax() 76 { 77 return mMax; 78 } 79 80 84 public void setMax(int aMax) 85 { 86 mMax = aMax; 87 } 88 89 90 public void beginTree(DetailAST aRootAST) 91 { 92 mContext = null; 93 mContextStack.clear(); 94 } 95 96 97 public void visitToken(DetailAST aAST) 98 { 99 switch (aAST.getType()) { 100 case TokenTypes.CTOR_DEF: 101 case TokenTypes.METHOD_DEF: 102 case TokenTypes.INSTANCE_INIT: 103 case TokenTypes.STATIC_INIT: 104 visitMemberDef(aAST); 105 break; 106 case TokenTypes.SLIST: 107 visitSlist(aAST); 108 break; 109 default: 110 throw new IllegalStateException (aAST.toString()); 111 } 112 } 113 114 115 public void leaveToken(DetailAST aAST) 116 { 117 switch (aAST.getType()) { 118 case TokenTypes.CTOR_DEF: 119 case TokenTypes.METHOD_DEF: 120 case TokenTypes.INSTANCE_INIT: 121 case TokenTypes.STATIC_INIT: 122 leaveMemberDef(aAST); 123 break; 124 case TokenTypes.SLIST: 125 break; 127 default: 128 throw new IllegalStateException (aAST.toString()); 129 } 130 } 131 132 136 private void visitMemberDef(DetailAST aAST) 137 { 138 mContextStack.push(mContext); 139 mContext = new Context(aAST); 140 } 141 142 147 private void leaveMemberDef(DetailAST aAST) 148 { 149 final int count = mContext.getCount(); 150 if (count > getMax()) { 151 log( 152 aAST.getLineNo(), 153 aAST.getColumnNo(), 154 "executableStatementCount", 155 new Integer (count), 156 new Integer (getMax())); 157 } 158 mContext = (Context) mContextStack.pop(); 159 } 160 161 166 private void visitSlist(DetailAST aAST) 167 { 168 if (mContext != null) { 169 final DetailAST contextAST = mContext.getAST(); 171 DetailAST parent = aAST.getParent(); 172 while (parent != null) { 173 final int type = parent.getType(); 174 if ((type == TokenTypes.CTOR_DEF) 175 || (type == TokenTypes.METHOD_DEF) 176 || (type == TokenTypes.INSTANCE_INIT) 177 || (type == TokenTypes.STATIC_INIT)) 178 { 179 if (parent == contextAST) { 180 mContext.addCount(aAST.getChildCount() / 2); 181 } 182 break; 183 } 184 parent = parent.getParent(); 185 } 186 } 187 } 188 189 193 private class Context 194 { 195 196 private DetailAST mAST; 197 198 199 private int mCount; 200 201 205 public Context(DetailAST aAST) 206 { 207 mAST = aAST; 208 mCount = 0; 209 } 210 211 215 public void addCount(int aCount) 216 { 217 mCount += aCount; 218 } 219 220 224 public DetailAST getAST() 225 { 226 return mAST; 227 } 228 229 233 public int getCount() 234 { 235 return mCount; 236 } 237 } 238 } 239 | Popular Tags |