Class RedundantThisCheck
java.lang.Object
com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
com.puppycrawl.tools.checkstyle.api.AbstractCheck
com.puppycrawl.tools.checkstyle.checks.coding.RedundantThisCheck
- All Implemented Interfaces:
Configurable,Contextualizable
Checks that references to instance variables and methods of the current object
avoid unnecessary use of
this, unless it is required to resolve
ambiguity with a shadowed field.
By default, a redundant this is tolerated when the same statement, or the
statement immediately before or after it, contains a this that is required
to resolve a shadowed field. This keeps groups of assignments consistent, for example
constructor bodies where only some parameters shadow fields. Set
allowAdjacentToRequiredThis to false to report every redundant
this.
- Since:
- 14.4.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final recordA redundantthisusage waiting for a reporting decision.Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate booleanAllow a redundantthiswhen the same or an adjacent statement contains a requiredthis.private booleanControl to checking method calls.static final StringA key is pointing to the warning message text in "message.properties" file.static final StringA key is pointing to the warning message text in "message.properties" file.private final List<RedundantThisCheck.RedundantUsage> Redundantthisusages whose reporting is decided infinishTree.Statements containing athisthat is required to resolve shadowing.Tracks names (parameters and local variables) that can shadow a field. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate voidaddParametersToScope(DetailAST parametersNode) Adds all parameters found withinparametersNodeto the current (top-of-stack) scope.voidCalled before the starting to process a tree.private voidcheckUnnecessaryThis(DetailAST literalThis) Classifies the use of "this" as required (shadowed) or redundant.private static DetailASTFinds the statement (a direct child of anSLIST) enclosing the given node.voidfinishTree(DetailAST rootAST) Called after finished processing a tree.int[]The configurable token set.int[]Returns the default token a check is interested in.int[]The tokens that this check must be registered for.private booleanisNearRequiredThis(DetailAST statement) Checks whether the given statement, or the statement immediately before or after it, contains a requiredthis.private booleanisShadowedByLocalName(String name) Checks whether the given variable name is shadowed by any parameter or local variable currently in scope, meaning thatthis.nameis necessary to distinguish the field from the shadowing name.voidleaveToken(DetailAST ast) Called after all the child nodes have been process.private static DetailASTnextStatement(DetailAST statement) Gets the statement after the given one, stepping over theSEMIthat terminates the given statement.private static DetailASTpreviousStatement(DetailAST statement) Gets the statement before the given one, stepping over theSEMIthat terminates the previous statement.voidsetAllowAdjacentToRequiredThis(boolean allowAdjacentToRequiredThis) Setter to allow redundant "this" when the same statement, or the statement immediately before or after it, contains a required "this".voidsetCheckMethods(boolean checkMethods) Setter to check whether to check redundant "this" with method call.voidvisitToken(DetailAST ast) Called to process a token.Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
clearViolations, destroy, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, 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.AbstractAutomaticBean
configure, contextualize, getConfiguration, setupChild
-
Field Details
-
MSG_KEY_FIELD
A key is pointing to the warning message text in "message.properties" file.- See Also:
-
MSG_KEY_METHOD
A key is pointing to the warning message text in "message.properties" file.- See Also:
-
scopeStack
Tracks names (parameters and local variables) that can shadow a field. -
requiredStatements
Statements containing athisthat is required to resolve shadowing. -
redundantUsages
Redundantthisusages whose reporting is decided infinishTree. -
checkMethods
Control to checking method calls. -
allowAdjacentToRequiredThis
Allow a redundantthiswhen the same or an adjacent statement contains a requiredthis.
-
-
Constructor Details
-
RedundantThisCheck
public RedundantThisCheck()Creates a newRedundantThisCheckinstance.
-
-
Method Details
-
setCheckMethods
Setter to check whether to check redundant "this" with method call.- Parameters:
checkMethods- should we check method call- Since:
- 14.4.0
-
setAllowAdjacentToRequiredThis
Setter to allow redundant "this" when the same statement, or the statement immediately before or after it, contains a required "this".- Parameters:
allowAdjacentToRequiredThis- whether to allow such usages- Since:
- 14.4.0
-
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:
-
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
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:
-
beginTree
Description copied from class:AbstractCheckCalled before the starting to process a tree. Ideal place to initialize information that is to be collected whilst processing a tree.- Overrides:
beginTreein classAbstractCheck- Parameters:
rootAST- the root of the tree
-
visitToken
Description copied from class:AbstractCheckCalled to process a token.- Overrides:
visitTokenin classAbstractCheck- Parameters:
ast- the token to process
-
leaveToken
Description copied from class:AbstractCheckCalled after all the child nodes have been process.- Overrides:
leaveTokenin classAbstractCheck- Parameters:
ast- the token leaving
-
finishTree
Description copied from class:AbstractCheckCalled after finished processing a tree. Ideal place to report on information collected whilst processing a tree.- Overrides:
finishTreein classAbstractCheck- Parameters:
rootAST- the root of the tree
-
checkUnnecessaryThis
Classifies the use of "this" as required (shadowed) or redundant. Required usages are recorded by their enclosing statement; redundant usages are deferred so thatfinishTreecan decide whether to report them.- Parameters:
literalThis- theLITERAL_THIStoken to check
-
findEnclosingStatement
Finds the statement (a direct child of anSLIST) enclosing the given node.- Parameters:
node- the node- Returns:
- the statement, or
nullif the node is not inside a block (for example, a field initializer)
-
isNearRequiredThis
Checks whether the given statement, or the statement immediately before or after it, contains a requiredthis.- Parameters:
statement- the statement to check, may benull- Returns:
trueif a requiredthisis in the same or an adjacent statement
-
previousStatement
Gets the statement before the given one, stepping over theSEMIthat terminates the previous statement.- Parameters:
statement- the statement- Returns:
- the previous statement, or
nullif there is none
-
nextStatement
Gets the statement after the given one, stepping over theSEMIthat terminates the given statement.- Parameters:
statement- the statement- Returns:
- the next statement, or
nullif there is none
-
isShadowedByLocalName
Checks whether the given variable name is shadowed by any parameter or local variable currently in scope, meaning thatthis.nameis necessary to distinguish the field from the shadowing name.- Parameters:
name- the variable name following "this"- Returns:
trueif a shadowing name exists in any enclosing scope
-
addParametersToScope
Adds all parameters found withinparametersNodeto the current (top-of-stack) scope.parametersNodemay be either aPARAMETERSnode (for methods and constructors) or aLITERAL_CATCHnode (for catch clauses); both carry their parameter definitions as directPARAMETER_DEFchildren.- Parameters:
parametersNode- the node whosePARAMETER_DEFchildren should be added to the current scope
-