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; 021 022import java.util.Optional; 023import java.util.regex.Pattern; 024 025import com.puppycrawl.tools.checkstyle.FileStatefulCheck; 026import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 027import com.puppycrawl.tools.checkstyle.api.DetailAST; 028import com.puppycrawl.tools.checkstyle.api.FullIdent; 029import com.puppycrawl.tools.checkstyle.api.TokenTypes; 030 031/** 032 * <p> 033 * Detects uncommented {@code main} methods. 034 * </p> 035 * <p> 036 * Rationale: A {@code main} method is often used for debugging purposes. 037 * When debugging is finished, developers often forget to remove the method, 038 * which changes the API and increases the size of the resulting class or JAR file. 039 * With the exception of the real program entry points, all {@code main} methods 040 * should be removed or commented out of the sources. 041 * </p> 042 * <ul> 043 * <li> 044 * Property {@code excludedClasses} - Specify pattern for qualified names of 045 * classes which are allowed to have a {@code main} method. 046 * Default value is {@code "^$" (empty)}. 047 * </li> 048 * </ul> 049 * <p> 050 * To configure the check: 051 * </p> 052 * <pre> 053 * <module name="UncommentedMain"/> 054 * </pre> 055 * <p> 056 * To configure the check to allow the {@code main} method for all classes with "Main" name: 057 * </p> 058 * <pre> 059 * <module name="UncommentedMain"> 060 * <property name="excludedClasses" value="\.Main$"/> 061 * </module> 062 * </pre> 063 * 064 * @since 3.2 065 */ 066@FileStatefulCheck 067public class UncommentedMainCheck 068 extends AbstractCheck { 069 070 /** 071 * A key is pointing to the warning message text in "messages.properties" 072 * file. 073 */ 074 public static final String MSG_KEY = "uncommented.main"; 075 076 /** 077 * Specify pattern for qualified names of classes which are allowed to 078 * have a {@code main} method. 079 */ 080 private Pattern excludedClasses = Pattern.compile("^$"); 081 /** Current class name. */ 082 private String currentClass; 083 /** Current package. */ 084 private FullIdent packageName; 085 /** Class definition depth. */ 086 private int classDepth; 087 088 /** 089 * Setter to specify pattern for qualified names of classes which are allowed 090 * to have a {@code main} method. 091 * 092 * @param excludedClasses a pattern 093 */ 094 public void setExcludedClasses(Pattern excludedClasses) { 095 this.excludedClasses = excludedClasses; 096 } 097 098 @Override 099 public int[] getAcceptableTokens() { 100 return getRequiredTokens(); 101 } 102 103 @Override 104 public int[] getDefaultTokens() { 105 return getRequiredTokens(); 106 } 107 108 @Override 109 public int[] getRequiredTokens() { 110 return new int[] { 111 TokenTypes.METHOD_DEF, 112 TokenTypes.CLASS_DEF, 113 TokenTypes.PACKAGE_DEF, 114 }; 115 } 116 117 @Override 118 public void beginTree(DetailAST rootAST) { 119 packageName = FullIdent.createFullIdent(null); 120 currentClass = null; 121 classDepth = 0; 122 } 123 124 @Override 125 public void leaveToken(DetailAST ast) { 126 if (ast.getType() == TokenTypes.CLASS_DEF) { 127 classDepth--; 128 } 129 } 130 131 @Override 132 public void visitToken(DetailAST ast) { 133 switch (ast.getType()) { 134 case TokenTypes.PACKAGE_DEF: 135 visitPackageDef(ast); 136 break; 137 case TokenTypes.CLASS_DEF: 138 visitClassDef(ast); 139 break; 140 case TokenTypes.METHOD_DEF: 141 visitMethodDef(ast); 142 break; 143 default: 144 throw new IllegalStateException(ast.toString()); 145 } 146 } 147 148 /** 149 * Sets current package. 150 * @param packageDef node for package definition 151 */ 152 private void visitPackageDef(DetailAST packageDef) { 153 packageName = FullIdent.createFullIdent(packageDef.getLastChild() 154 .getPreviousSibling()); 155 } 156 157 /** 158 * If not inner class then change current class name. 159 * @param classDef node for class definition 160 */ 161 private void visitClassDef(DetailAST classDef) { 162 // we are not use inner classes because they can not 163 // have static methods 164 if (classDepth == 0) { 165 final DetailAST ident = classDef.findFirstToken(TokenTypes.IDENT); 166 currentClass = packageName.getText() + "." + ident.getText(); 167 classDepth++; 168 } 169 } 170 171 /** 172 * Checks method definition if this is 173 * {@code public static void main(String[])}. 174 * @param method method definition node 175 */ 176 private void visitMethodDef(DetailAST method) { 177 if (classDepth == 1 178 // method not in inner class or in interface definition 179 && checkClassName() 180 && checkName(method) 181 && checkModifiers(method) 182 && checkType(method) 183 && checkParams(method)) { 184 log(method.getLineNo(), MSG_KEY); 185 } 186 } 187 188 /** 189 * Checks that current class is not excluded. 190 * @return true if check passed, false otherwise 191 */ 192 private boolean checkClassName() { 193 return !excludedClasses.matcher(currentClass).find(); 194 } 195 196 /** 197 * Checks that method name is @quot;main@quot;. 198 * @param method the METHOD_DEF node 199 * @return true if check passed, false otherwise 200 */ 201 private static boolean checkName(DetailAST method) { 202 final DetailAST ident = method.findFirstToken(TokenTypes.IDENT); 203 return "main".equals(ident.getText()); 204 } 205 206 /** 207 * Checks that method has final and static modifiers. 208 * @param method the METHOD_DEF node 209 * @return true if check passed, false otherwise 210 */ 211 private static boolean checkModifiers(DetailAST method) { 212 final DetailAST modifiers = 213 method.findFirstToken(TokenTypes.MODIFIERS); 214 215 return modifiers.findFirstToken(TokenTypes.LITERAL_PUBLIC) != null 216 && modifiers.findFirstToken(TokenTypes.LITERAL_STATIC) != null; 217 } 218 219 /** 220 * Checks that return type is {@code void}. 221 * @param method the METHOD_DEF node 222 * @return true if check passed, false otherwise 223 */ 224 private static boolean checkType(DetailAST method) { 225 final DetailAST type = 226 method.findFirstToken(TokenTypes.TYPE).getFirstChild(); 227 return type.getType() == TokenTypes.LITERAL_VOID; 228 } 229 230 /** 231 * Checks that method has only {@code String[]} or only {@code String...} param. 232 * @param method the METHOD_DEF node 233 * @return true if check passed, false otherwise 234 */ 235 private static boolean checkParams(DetailAST method) { 236 boolean checkPassed = false; 237 final DetailAST params = method.findFirstToken(TokenTypes.PARAMETERS); 238 239 if (params.getChildCount() == 1) { 240 final DetailAST parameterType = params.getFirstChild().findFirstToken(TokenTypes.TYPE); 241 final Optional<DetailAST> arrayDecl = Optional.ofNullable( 242 parameterType.findFirstToken(TokenTypes.ARRAY_DECLARATOR)); 243 final Optional<DetailAST> varargs = Optional.ofNullable( 244 params.getFirstChild().findFirstToken(TokenTypes.ELLIPSIS)); 245 246 if (arrayDecl.isPresent()) { 247 checkPassed = isStringType(arrayDecl.get().getFirstChild()); 248 } 249 else if (varargs.isPresent()) { 250 checkPassed = isStringType(parameterType.getFirstChild()); 251 } 252 } 253 return checkPassed; 254 } 255 256 /** 257 * Whether the type is java.lang.String. 258 * @param typeAst the type to check. 259 * @return true, if the type is java.lang.String. 260 */ 261 private static boolean isStringType(DetailAST typeAst) { 262 final FullIdent type = FullIdent.createFullIdent(typeAst); 263 return "String".equals(type.getText()) 264 || "java.lang.String".equals(type.getText()); 265 } 266 267}