public class MethodLengthCheck extends AbstractCheck
Checks for long methods.
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.
The default maximum method length is 150 lines. To change the maximum number of lines, set property max.
An example of how to configure the check is:
<module name="MethodLength"/>
An example of how to configure the check so that it accepts methods with at most 60 lines is:
<module name="MethodLength">
<property name="max" value="60"/>
</module>
| Modifier and Type | Field and Description |
|---|---|
private boolean |
countEmpty
Whether to ignore empty lines and single line comments.
|
private static int |
DEFAULT_MAX_LINES
Default maximum number of lines.
|
private int |
max
The maximum number of lines.
|
static java.lang.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.
|
private int |
getLengthOfBlock(DetailAST openingBrace,
DetailAST closingBrace)
Returns length of code only without comments and blank lines.
|
int[] |
getRequiredTokens()
The tokens that this check must be registered for.
|
void |
setCountEmpty(boolean countEmpty)
Sets countEmpty.
|
void |
setMax(int length)
Sets maximum length of a method.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
beginTree, destroy, finishTree, getClassLoader, getFileContents, getLine, getLines, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, setClassLoader, setFileContents, setMessages, setTabWidth, setTokensgetCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, log, setId, setSeverityconfigure, contextualize, finishLocalSetup, getConfiguration, setupChildpublic static final java.lang.String MSG_KEY
private static final int DEFAULT_MAX_LINES
private boolean countEmpty
private int max
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 processprivate int getLengthOfBlock(DetailAST openingBrace, DetailAST closingBrace)
openingBrace - block opening braceclosingBrace - block closing bracepublic 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 //.