1 package com.puppycrawl.tools.checkstyle.checks; 20 21 import com.puppycrawl.tools.checkstyle.api.Check; 22 import com.puppycrawl.tools.checkstyle.api.DetailAST; 23 import com.puppycrawl.tools.checkstyle.api.FullIdent; 24 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 25 import com.puppycrawl.tools.checkstyle.api.Utils; 26 27 import java.util.regex.Pattern ; 28 import java.util.regex.PatternSyntaxException ; 29 30 import org.apache.commons.beanutils.ConversionException; 31 32 44 public class UncommentedMainCheck 45 extends Check 46 { 47 48 private String mExcludedClasses = "^$"; 49 50 private Pattern mExcludedClassesPattern = 51 Utils.createPattern(mExcludedClasses); 52 53 private String mCurrentClass; 54 55 private FullIdent mPackage; 56 57 private int mClassDepth; 58 59 64 public void setExcludedClasses(String aExcludedClasses) 65 throws ConversionException 66 { 67 try { 68 mExcludedClasses = aExcludedClasses; 69 mExcludedClassesPattern = Utils.getPattern(mExcludedClasses); 70 } 71 catch (final PatternSyntaxException e) { 72 throw new ConversionException("unable to parse " 73 + mExcludedClasses, 74 e); 75 } 76 } 77 78 79 public int[] getDefaultTokens() 80 { 81 return new int[] { 82 TokenTypes.METHOD_DEF, 83 TokenTypes.CLASS_DEF, 84 TokenTypes.PACKAGE_DEF, 85 }; 86 } 87 88 89 public int[] getRequiredTokens() 90 { 91 return getDefaultTokens(); 92 } 93 94 95 public void beginTree(DetailAST aRootAST) 96 { 97 mPackage = FullIdent.createFullIdent(null); 98 mCurrentClass = null; 99 mClassDepth = 0; 100 } 101 102 103 public void leaveToken(DetailAST aAst) 104 { 105 if (aAst.getType() == TokenTypes.CLASS_DEF) { 106 if (mClassDepth == 1) { 107 mCurrentClass = null; 108 } 109 mClassDepth--; 110 } 111 } 112 113 114 public void visitToken(DetailAST aAst) 115 { 116 switch (aAst.getType()) { 117 case TokenTypes.PACKAGE_DEF: 118 visitPackageDef(aAst); 119 break; 120 case TokenTypes.CLASS_DEF: 121 visitClassDef(aAst); 122 break; 123 case TokenTypes.METHOD_DEF: 124 visitMethodDef(aAst); 125 break; 126 default: 127 throw new IllegalStateException (aAst.toString()); 128 } 129 } 130 131 135 private void visitPackageDef(DetailAST aPackage) 136 { 137 mPackage = FullIdent.createFullIdent(aPackage.getLastChild() 138 .getPreviousSibling()); 139 } 140 141 145 private void visitClassDef(DetailAST aClass) 146 { 147 if (mClassDepth == 0) { 150 final DetailAST ident = aClass.findFirstToken(TokenTypes.IDENT); 151 mCurrentClass = mPackage.getText() + "." + ident.getText(); 152 mClassDepth++; 153 } 154 } 155 156 161 private void visitMethodDef(DetailAST aMethod) 162 { 163 if (mClassDepth != 1) { 164 return; 166 } 167 168 if (checkClassName() 169 && checkName(aMethod) 170 && checkModifiers(aMethod) 171 && checkType(aMethod) 172 && checkParams(aMethod)) 173 { 174 log(aMethod.getLineNo(), "uncommented.main"); 175 } 176 } 177 178 182 private boolean checkClassName() 183 { 184 return !mExcludedClassesPattern.matcher(mCurrentClass).find(); 185 } 186 187 192 private boolean checkName(DetailAST aMethod) 193 { 194 final DetailAST ident = aMethod.findFirstToken(TokenTypes.IDENT); 195 return "main".equals(ident.getText()); 196 } 197 198 203 private boolean checkModifiers(DetailAST aMethod) 204 { 205 final DetailAST modifiers = 206 aMethod.findFirstToken(TokenTypes.MODIFIERS); 207 208 return modifiers.branchContains(TokenTypes.LITERAL_PUBLIC) 209 && modifiers.branchContains(TokenTypes.LITERAL_STATIC); 210 } 211 212 217 private boolean checkType(DetailAST aMethod) 218 { 219 final DetailAST type = 220 (DetailAST) aMethod.findFirstToken(TokenTypes.TYPE).getFirstChild(); 221 return type.getType() == TokenTypes.LITERAL_VOID; 222 } 223 224 229 private boolean checkParams(DetailAST aMethod) 230 { 231 final DetailAST params = aMethod.findFirstToken(TokenTypes.PARAMETERS); 232 if (params.getChildCount() != 1) { 233 return false; 234 } 235 final DetailAST paramType = ((DetailAST) params.getFirstChild()) 236 .findFirstToken(TokenTypes.TYPE); 237 final DetailAST arrayDecl = 238 paramType.findFirstToken(TokenTypes.ARRAY_DECLARATOR); 239 if (arrayDecl == null) { 240 return false; 241 } 242 243 final DetailAST arrayType = (DetailAST) arrayDecl.getFirstChild(); 244 245 if ((arrayType.getType() == TokenTypes.IDENT) 246 || (arrayType.getType() == TokenTypes.DOT)) 247 { 248 final FullIdent type = FullIdent.createFullIdent(arrayType); 249 return ("String".equals(type.getText()) 250 || "java.lang.String".equals(type.getText())); 251 } 252 253 return false; 254 } 255 } 256 | Popular Tags |