public final class ParameterAssignmentCheck extends AbstractCheck
Disallows assignment of parameters.
Rationale: Parameter assignment is often considered poor programming practice. Forcing developers to declare parameters as final is often onerous. Having a check ensure that parameters are never assigned would give the best of both worlds.
To configure the check:
<module name="ParameterAssignment"/>
Example:
class MyClass {
int methodOne(int parameter) {
if (parameter <= 0 ) {
throw new IllegalArgumentException("A positive value is expected");
}
parameter -= 2; // violation
return parameter;
}
int methodTwo(int parameter) {
if (parameter <= 0 ) {
throw new IllegalArgumentException("A positive value is expected");
}
int local = parameter;
local -= 2; // OK
return local;
}
IntPredicate obj = a -> ++a == 12; // violation
IntBinaryOperator obj2 = (int a, int b) -> {
a++; // violation
b += 12; // violation
return a + b;
};
IntPredicate obj3 = a -> {
int b = a; // ok
return ++b == 12;
};
}
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
parameter.assignment
AutomaticBean.OutputStreamOptions| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
MSG_KEY
A key is pointing to the warning message text in "messages.properties"
file.
|
private java.util.Set<java.lang.String> |
parameterNames
Current set of parameters.
|
private java.util.Deque<java.util.Set<java.lang.String>> |
parameterNamesStack
Stack of methods' parameters.
|
| Constructor and Description |
|---|
ParameterAssignmentCheck() |
| Modifier and Type | Method and Description |
|---|---|
void |
beginTree(DetailAST rootAST)
Called before the starting to process a tree.
|
private void |
checkNestedIdent(DetailAST ast)
Check if nested ident is parameter.
|
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 |
leaveToken(DetailAST ast)
Called after all the child nodes have been process.
|
private void |
visitLambda(DetailAST lambdaAst)
Creates new set of parameters and store old one in stack.
|
private void |
visitLambdaParameters(DetailAST ast)
Creates new parameter set for given lambda expression.
|
private void |
visitMethodDef(DetailAST ast)
Creates new set of parameters and store old one in stack.
|
private void |
visitMethodParameters(DetailAST ast)
Creates new parameter set for given method.
|
private void |
visitParameters(DetailAST parametersAst)
Visits parameter list and adds parameter names to the set.
|
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 final java.util.Deque<java.util.Set<java.lang.String>> parameterNamesStack
private java.util.Set<java.lang.String> parameterNames
public ParameterAssignmentCheck()
public int[] getDefaultTokens()
AbstractCheckgetDefaultTokens in class AbstractCheckTokenTypespublic int[] getRequiredTokens()
AbstractCheckgetRequiredTokens in class AbstractCheckTokenTypespublic int[] getAcceptableTokens()
AbstractCheckgetAcceptableTokens in class AbstractCheckTokenTypespublic void beginTree(DetailAST rootAST)
AbstractCheckbeginTree in class AbstractCheckrootAST - the root of the treepublic void visitToken(DetailAST ast)
AbstractCheckvisitToken in class AbstractCheckast - the token to processpublic void leaveToken(DetailAST ast)
AbstractCheckleaveToken in class AbstractCheckast - the token leavingprivate void checkNestedIdent(DetailAST ast)
ast - parent of node of identprivate void visitMethodDef(DetailAST ast)
ast - a method to process.private void visitLambda(DetailAST lambdaAst)
lambdaAst - node of type TokenTypes.LAMBDA.private void visitMethodParameters(DetailAST ast)
ast - a method for process.private void visitLambdaParameters(DetailAST ast)
ast - a lambda expression parameter to processprivate void visitParameters(DetailAST parametersAst)
parametersAst - ast node of type TokenTypes.PARAMETERS.Copyright © 2001-2022. All Rights Reserved.