public class LambdaBodyLengthCheck extends AbstractCheck
Checks lambda body length.
Rationale: Similar to anonymous inner classes, if lambda body becomes very long it is hard to understand and to see the flow of the method where the lambda is defined. Therefore long lambda body should usually be extracted to method.
max - Specify the maximum number of lines allowed.
Type is int.
Default value is 10.
To configure the check to accept lambda bodies with up to 10 lines:
<module name="LambdaBodyLength"/>
Example:
class Test {
Runnable r = () -> { // violation, 11 lines
System.out.println(2); // line 2 of lambda
System.out.println(3);
System.out.println(4);
System.out.println(5);
System.out.println(6);
System.out.println(7);
System.out.println(8);
System.out.println(9);
System.out.println(10);
}; // line 11
Runnable r2 = () -> // violation, 11 lines
"someString".concat("1") // line 1 of lambda
.concat("2")
.concat("3")
.concat("4")
.concat("5")
.concat("6")
.concat("7")
.concat("8")
.concat("9")
.concat("10")
.concat("11"); // line 11
Runnable r3 = () -> { // ok, 10 lines
System.out.println(2); // line 2 of lambda
System.out.println(3);
System.out.println(4);
System.out.println(5);
System.out.println(6);
System.out.println(7);
System.out.println(8);
System.out.println(9);
}; // line 10
}
To configure the check to accept lambda bodies with max 5 lines:
<module name="LambdaBodyLength"> <property name="max" value="5"/> </module>
Example:
class Test {
Runnable r = () -> { // violation, 6 lines
System.out.println(2); // line 2 of lambda
System.out.println(3);
System.out.println(4);
System.out.println(5);
};
Runnable r2 = () -> // violation, 6 lines
"someString".concat("1")
.concat("2")
.concat("3")
.concat("4")
.concat("5")
.concat("6");
Runnable r3 = () -> { // ok, 5 lines
System.out.println(2);
System.out.println(3);
System.out.println(4);
};
}
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
maxLen.lambdaBody
AutomaticBean.OutputStreamOptions| Modifier and Type | Field and Description |
|---|---|
private static int |
DEFAULT_MAX
Default maximum number of lines.
|
private int |
max
Specify the maximum number of lines allowed.
|
static java.lang.String |
MSG_KEY
A key is pointing to the warning message text in "messages.properties"
file.
|
| Constructor and Description |
|---|
LambdaBodyLengthCheck() |
| Modifier and Type | Method and Description |
|---|---|
int[] |
getAcceptableTokens()
The configurable token set.
|
int[] |
getDefaultTokens()
Returns the default token a check is interested in.
|
private static int |
getFirstNodeLineNumber(DetailAST lambdaBody)
Get first child node in the tree line number.
|
private static int |
getLastNodeLineNumber(DetailAST lambdaBody)
Get last child node in the tree line number.
|
private static int |
getLength(DetailAST ast)
Get length of lambda body.
|
int[] |
getRequiredTokens()
The tokens that this check must be registered for.
|
void |
setMax(int length)
Setter to specify the maximum number of lines allowed.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
beginTree, clearViolations, destroy, finishTree, getFileContents, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, 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 int DEFAULT_MAX
private int max
public LambdaBodyLengthCheck()
public void setMax(int length)
length - the maximum length of lambda body.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 static int getLength(DetailAST ast)
ast - lambda body node.private static int getLastNodeLineNumber(DetailAST lambdaBody)
lambdaBody - lambda body node.private static int getFirstNodeLineNumber(DetailAST lambdaBody)
lambdaBody - lambda body node.Copyright © 2001-2022. All Rights Reserved.