Class LambdaBodyLengthCheck
java.lang.Object
com.puppycrawl.tools.checkstyle.api.AutomaticBean
com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
com.puppycrawl.tools.checkstyle.api.AbstractCheck
com.puppycrawl.tools.checkstyle.checks.sizes.LambdaBodyLengthCheck
- All Implemented Interfaces:
Configurable,Contextualizable
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.
-
Property
max- Specify the maximum number of lines allowed. Type isint. Default value is10.
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
- Since:
- 8.37
-
Nested Class Summary
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
AutomaticBean.OutputStreamOptions -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final intDefault maximum number of lines.private intSpecify the maximum number of lines allowed.static final StringA key is pointing to the warning message text in "messages.properties" file. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionint[]The configurable token set.int[]Returns the default token a check is interested in.private static intgetFirstNodeLineNumber(DetailAST lambdaBody) Get first child node in the tree line number.private static intgetLastNodeLineNumber(DetailAST lambdaBody) Get last child node in the tree line number.private static intGet length of lambda body.int[]The tokens that this check must be registered for.voidsetMax(int length) Setter to specify the maximum number of lines allowed.voidvisitToken(DetailAST ast) Called to process a token.Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
beginTree, clearViolations, destroy, finishTree, getFileContents, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokensMethods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityMethods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
configure, contextualize, getConfiguration, setupChild
-
Field Details
-
MSG_KEY
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
DEFAULT_MAX
private static final int DEFAULT_MAXDefault maximum number of lines.- See Also:
-
max
private int maxSpecify the maximum number of lines allowed.
-
-
Constructor Details
-
LambdaBodyLengthCheck
public LambdaBodyLengthCheck()
-
-
Method Details
-
setMax
public void setMax(int length) Setter to specify the maximum number of lines allowed.- Parameters:
length- the maximum length of lambda body.
-
getDefaultTokens
public int[] getDefaultTokens()Description copied from class:AbstractCheckReturns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
getDefaultTokensin classAbstractCheck- Returns:
- the default tokens
- See Also:
-
getAcceptableTokens
public int[] getAcceptableTokens()Description copied from class:AbstractCheckThe configurable token set. Used to protect Checks against malicious users who specify an unacceptable token set in the configuration file. The default implementation returns the check's default tokens.- Specified by:
getAcceptableTokensin classAbstractCheck- Returns:
- the token set this check is designed for.
- See Also:
-
getRequiredTokens
public int[] getRequiredTokens()Description copied from class:AbstractCheckThe tokens that this check must be registered for.- Specified by:
getRequiredTokensin classAbstractCheck- Returns:
- the token set this must be registered for.
- See Also:
-
visitToken
Description copied from class:AbstractCheckCalled to process a token.- Overrides:
visitTokenin classAbstractCheck- Parameters:
ast- the token to process
-
getLength
Get length of lambda body.- Parameters:
ast- lambda body node.- Returns:
- length of lambda body.
-
getLastNodeLineNumber
Get last child node in the tree line number.- Parameters:
lambdaBody- lambda body node.- Returns:
- last child node in the tree line number.
-
getFirstNodeLineNumber
Get first child node in the tree line number.- Parameters:
lambdaBody- lambda body node.- Returns:
- first child node in the tree line number.
-