public class UncommentedMainCheck extends AbstractCheck
Detects uncommented main methods.
Rationale: A main method is often used for debugging purposes.
When debugging is finished, developers often forget to remove the method,
which changes the API and increases the size of the resulting class or JAR file.
With the exception of the real program entry points, all main methods
should be removed or commented out of the sources.
excludedClasses - Specify pattern for qualified names of
classes which are allowed to have a main method.
Type is java.util.regex.Pattern.
Default value is "^$".
To configure the check:
<module name="UncommentedMain"/>
Example:
public class Game {
public static void main(String... args){} // violation
}
public class Main {
public static void main(String[] args){} // violation
}
public class Launch {
//public static void main(String[] args){} // OK
}
public class Start {
public void main(){} // OK
}
public record MyRecord1 {
public void main(){} // violation
}
public record MyRecord2 {
//public void main(){} // OK
}
To configure the check to allow the main method for all classes with "Main" name:
<module name="UncommentedMain"> <property name="excludedClasses" value="\.Main$"/> </module>
Example:
public class Game {
public static void main(String... args){} // violation
}
public class Main {
public static void main(String[] args){} // OK
}
public class Launch {
//public static void main(String[] args){} // OK
}
public class Start {
public void main(){} // OK
}
public record MyRecord1 {
public void main(){} // OK
}
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
uncommented.main
AutomaticBean.OutputStreamOptions| Modifier and Type | Field and Description |
|---|---|
private int |
classDepth
Class definition depth.
|
private java.lang.String |
currentClass
Current class name.
|
private java.util.regex.Pattern |
excludedClasses
Specify pattern for qualified names of classes which are allowed to
have a
main method. |
static java.lang.String |
MSG_KEY
A key is pointing to the warning message text in "messages.properties"
file.
|
private FullIdent |
packageName
Current package.
|
private static java.util.Set<java.lang.String> |
STRING_PARAMETER_NAMES
Set of possible String array types.
|
| Constructor and Description |
|---|
UncommentedMainCheck() |
| Modifier and Type | Method and Description |
|---|---|
void |
beginTree(DetailAST rootAST)
Called before the starting to process a tree.
|
private boolean |
checkClassName()
Checks that current class is not excluded.
|
private static boolean |
checkModifiers(DetailAST method)
Checks that method has final and static modifiers.
|
private static boolean |
checkName(DetailAST method)
Checks that method name is @quot;main@quot;.
|
private static boolean |
checkParams(DetailAST method)
Checks that method has only
String[] or only String... param. |
private static boolean |
checkType(DetailAST method)
Checks that return type is
void. |
int[] |
getAcceptableTokens()
The configurable token set.
|
int[] |
getDefaultTokens()
Returns the default token a check is interested in.
|
int[] |
getRequiredTokens()
The tokens that this check must be registered for.
|
private static boolean |
isStringType(DetailAST typeAst)
Whether the type is java.lang.String.
|
void |
leaveToken(DetailAST ast)
Called after all the child nodes have been process.
|
void |
setExcludedClasses(java.util.regex.Pattern excludedClasses)
Setter to specify pattern for qualified names of classes which are allowed
to have a
main method. |
private void |
visitClassOrRecordDef(DetailAST classOrRecordDef)
If not inner class then change current class name.
|
private void |
visitMethodDef(DetailAST method)
Checks method definition if this is
public static void main(String[]). |
private void |
visitPackageDef(DetailAST packageDef)
Sets current package.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
clearViolations, destroy, finishTree, getFileContents, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, log, log, log, setFileContents, setTabWidth, setTokensfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityconfigure, contextualize, getConfiguration, setupChildpublic static final java.lang.String MSG_KEY
private static final java.util.Set<java.lang.String> STRING_PARAMETER_NAMES
private java.util.regex.Pattern excludedClasses
main method.private java.lang.String currentClass
private FullIdent packageName
private int classDepth
public UncommentedMainCheck()
public void setExcludedClasses(java.util.regex.Pattern excludedClasses)
main method.excludedClasses - a patternpublic int[] getAcceptableTokens()
AbstractCheckgetAcceptableTokens in class AbstractCheckTokenTypespublic int[] getDefaultTokens()
AbstractCheckgetDefaultTokens in class AbstractCheckTokenTypespublic int[] getRequiredTokens()
AbstractCheckgetRequiredTokens in class AbstractCheckTokenTypespublic void beginTree(DetailAST rootAST)
AbstractCheckbeginTree in class AbstractCheckrootAST - the root of the treepublic void leaveToken(DetailAST ast)
AbstractCheckleaveToken in class AbstractCheckast - the token leavingpublic void visitToken(DetailAST ast)
AbstractCheckvisitToken in class AbstractCheckast - the token to processprivate void visitPackageDef(DetailAST packageDef)
packageDef - node for package definitionprivate void visitClassOrRecordDef(DetailAST classOrRecordDef)
classOrRecordDef - node for class or record definitionprivate void visitMethodDef(DetailAST method)
public static void main(String[]).method - method definition nodeprivate boolean checkClassName()
private static boolean checkName(DetailAST method)
method - the METHOD_DEF nodeprivate static boolean checkModifiers(DetailAST method)
method - the METHOD_DEF nodeprivate static boolean checkType(DetailAST method)
void.method - the METHOD_DEF nodeprivate static boolean checkParams(DetailAST method)
String[] or only String... param.method - the METHOD_DEF nodeprivate static boolean isStringType(DetailAST typeAst)
typeAst - the type to check.Copyright © 2001-2022. All Rights Reserved.