public class MethodLengthCheck extends AbstractCheck
Checks for long methods and constructors.
Rationale: If a method becomes very long it is hard to understand. Therefore long methods should usually be refactored into several individual methods that focus on a specific task.
max - Specify the maximum number of lines allowed.
Type is int.
Default value is 150.
countEmpty - Control whether to count empty lines and single
line comments of the form //.
Type is boolean.
Default value is true.
tokens - tokens to check
Type is int[].
Default value is:
METHOD_DEF,
CTOR_DEF.
To configure the check:
<module name="MethodLength"/>
To configure the check so that it accepts methods with at most 60 lines:
<module name="MethodLength"> <property name="tokens" value="METHOD_DEF"/> <property name="max" value="60"/> </module>
To configure the check so that it accepts methods with at most 60 lines, not counting empty lines and single line comments:
<module name="MethodLength"> <property name="tokens" value="METHOD_DEF"/> <property name="max" value="60"/> <property name="countEmpty" value="false"/> </module>
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
maxLen.method
AutomaticBean.OutputStreamOptions| Modifier and Type | Field and Description |
|---|---|
static String |
MSG_KEY
A key is pointing to the warning message text in "messages.properties"
file.
|
| Constructor and Description |
|---|
MethodLengthCheck() |
| Modifier and Type | Method and Description |
|---|---|
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.
|
void |
setCountEmpty(boolean countEmpty)
Setter to control whether to count empty lines and single line comments
of the form
//. |
void |
setMax(int length)
Setter to specify the maximum number of lines allowed.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
beginTree, clearMessages, destroy, finishTree, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokensfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityconfigure, contextualize, getConfiguration, setupChildpublic static final String MSG_KEY
public MethodLengthCheck()
public int[] getDefaultTokens()
AbstractCheckgetDefaultTokens in class AbstractCheckTokenTypespublic int[] getAcceptableTokens()
AbstractCheckgetAcceptableTokens in class AbstractCheckTokenTypespublic int[] getRequiredTokens()
AbstractCheckgetRequiredTokens in class AbstractCheckTokenTypespublic void visitToken(DetailAST ast)
AbstractCheckvisitToken in class AbstractCheckast - the token to processpublic void setMax(int length)
length - the maximum length of a method.public void setCountEmpty(boolean countEmpty)
//.countEmpty - whether to count empty and single line comments
of the form //.Copyright © 2001–2020. All rights reserved.