public class RequireThisCheck extends AbstractCheck
Checks that code doesn't rely on the "this" default. That is references to instance variables and methods of the present object are explicitly of the form "this.varName" or "this.methodName(args)".
Check has the following options:checkFields - whether to check references to fields. Default value is true.
checkMethods - whether to check references to methods. Default value is true.
validateOnlyOverlapping - whether to check only overlapping by variables or arguments. Default value is true.
Warning: the Check is very controversial if 'validateOnlyOverlapping' option is set to 'false' and not that actual nowadays.
Examples of use:
<module name="RequireThis"/>An example of how to configure to check
this
qualifier for
methods only:
<module name="RequireThis"> <property name="checkFields" value="false"/> <property name="checkMethods" value="true"/> </module>
Rationale:
Limitations: Nothing is currently done about static variables or catch-blocks. Static methods invoked on a class name seem to be OK; both the class name and the method name have a DOT parent. Non-static methods invoked on either this or a variable name seem to be OK, likewise.
Modifier and Type | Class and Description |
---|---|
private static class |
RequireThisCheck.AbstractFrame
A declaration frame.
|
private static class |
RequireThisCheck.AnonymousClassFrame
An anonymous class frame; holds instance variable names.
|
private static class |
RequireThisCheck.BlockFrame
A frame initiated on entering a statement list; holds local variable names.
|
private static class |
RequireThisCheck.ClassFrame
A frame initiated at class, enum or interface definition; holds instance variable names.
|
private static class |
RequireThisCheck.ConstructorFrame
A frame initiated at constructor definition.
|
private static class |
RequireThisCheck.FrameType
An AbstractFrame type.
|
private static class |
RequireThisCheck.MethodFrame
A frame initiated at method definition; holds a method definition token.
|
Modifier and Type | Field and Description |
---|---|
private static java.util.Set<java.lang.Integer> |
ASSIGN_TOKENS
Set of all assign tokens.
|
private boolean |
checkFields
Whether we should check fields usage.
|
private boolean |
checkMethods
Whether we should check methods usage.
|
private static java.util.Set<java.lang.Integer> |
COMPOUND_ASSIGN_TOKENS
Set of all compound assign tokens.
|
private RequireThisCheck.AbstractFrame |
current
Frame for the currently processed AST.
|
private static java.util.Set<java.lang.Integer> |
DECLARATION_TOKENS
Set of all declaration tokens.
|
private java.util.Map<DetailAST,RequireThisCheck.AbstractFrame> |
frames
Tree of all the parsed frames.
|
static java.lang.String |
MSG_METHOD
A key is pointing to the warning message text in "messages.properties"
file.
|
static java.lang.String |
MSG_VARIABLE
A key is pointing to the warning message text in "messages.properties"
file.
|
private boolean |
validateOnlyOverlapping
Whether we should check only overlapping by variables or arguments.
|
Constructor and Description |
---|
RequireThisCheck() |
Modifier and Type | Method and Description |
---|---|
void |
beginTree(DetailAST rootAST)
Called before the starting to process a tree.
|
private boolean |
canAssignValueToClassField(DetailAST ast)
Checks whether a value can be assigned to a field.
|
private boolean |
canBeReferencedFromStaticContext(DetailAST ident)
Checks whether a field can be referenced from a static context.
|
private static void |
collectDeclarations(java.util.Deque<RequireThisCheck.AbstractFrame> frameStack,
DetailAST ast)
Parses the next AST for declarations.
|
private static void |
collectVariableDeclarations(DetailAST ast,
RequireThisCheck.AbstractFrame frame)
Collects variable declarations.
|
private void |
endCollectingDeclarations(java.util.Queue<RequireThisCheck.AbstractFrame> frameStack,
DetailAST ast)
Ends parsing of the AST for declarations.
|
private RequireThisCheck.AbstractFrame |
findClassFrame(DetailAST name,
boolean lookForMethod)
Find the class frame containing declaration.
|
private RequireThisCheck.AbstractFrame |
findFrame(DetailAST name,
boolean lookForMethod)
Find frame containing declaration.
|
private static RequireThisCheck.AbstractFrame |
findFrame(RequireThisCheck.AbstractFrame frame,
DetailAST name,
boolean lookForMethod)
Find frame containing declaration.
|
int[] |
getAcceptableTokens()
The configurable token set.
|
private static java.util.Set<DetailAST> |
getAllTokensOfType(DetailAST ast,
int tokenType)
Collects all tokens of specific type starting with the current ast node.
|
private static java.util.Set<DetailAST> |
getAllTokensOfType(DetailAST ast,
int tokenType,
int endLineNumber)
Collects all tokens of specific type starting with the current ast node and which line
number is lower or equal to the end line number.
|
private static java.util.Set<DetailAST> |
getAllTokensWhichAreEqualToCurrent(DetailAST ast,
DetailAST token,
int endLineNumber)
Collects all tokens which are equal to current token starting with the current ast node and
which line number is lower or equal to the end line number.
|
private static DetailAST |
getBlockEndToken(DetailAST blockNameIdent,
DetailAST blockStartToken)
Returns the token which ends the code block.
|
private RequireThisCheck.AbstractFrame |
getClassFrameWhereViolationIsFound(DetailAST ast)
Returns the class frame where violation is found (where the field is used without 'this')
or null otherwise.
|
private static DetailAST |
getCodeBlockDefinitionToken(DetailAST ident)
Returns code block definition token for current identifier.
|
int[] |
getDefaultTokens()
Returns the default token a check is interested in.
|
private RequireThisCheck.AbstractFrame |
getFieldWithoutThis(DetailAST ast,
int parentType)
Returns the frame where the field is declared, if the given field is used without
'this', and null otherwise.
|
private RequireThisCheck.AbstractFrame |
getMethodWithoutThis(DetailAST ast)
Returns the frame where the method is declared, if the given method is used without
'this' and null otherwise.
|
private java.lang.String |
getNearestClassFrameName()
Gets the name of the nearest parent ClassFrame.
|
int[] |
getRequiredTokens()
The tokens that this check must be registered for.
|
private static boolean |
isAnonymousClassDef(DetailAST ast)
Whether the AST is a definition of an anonymous class.
|
private static boolean |
isAssignToken(int tokenType)
Check that token is related to assign tokens.
|
private static boolean |
isCompoundAssignToken(int tokenType)
Check that token is related to compound assign tokens.
|
private static boolean |
isDeclarationToken(int parentType)
Check that token is related to Definition tokens.
|
private static boolean |
isInsideConstructorFrame(RequireThisCheck.AbstractFrame frame)
Checks whether a field usage frame is inside constructor frame.
|
private static boolean |
isLambdaParameter(DetailAST ast)
Checks if the token is a Lambda parameter.
|
private boolean |
isOverlappingByArgument(DetailAST ast)
Checks whether an overlapping by method or constructor argument takes place.
|
private boolean |
isOverlappingByLocalVariable(DetailAST ast)
Checks whether an overlapping by local variable takes place.
|
private static boolean |
isReturnedVariable(RequireThisCheck.AbstractFrame currentFrame,
DetailAST ident)
Checks whether the current variable is returned from the method.
|
private static boolean |
isUserDefinedArrangementOfThis(RequireThisCheck.AbstractFrame currentFrame,
DetailAST ident)
Checks whether user arranges 'this' for variable in method, constructor, or block on his own.
|
private void |
logViolation(java.lang.String msgKey,
DetailAST ast,
RequireThisCheck.AbstractFrame frame)
Helper method to log a LocalizedMessage.
|
private void |
processIdent(DetailAST ast)
Checks if a given IDENT is method call or field name which
requires explicit
this qualifier. |
void |
setCheckFields(boolean checkFields)
Setter for checkFields property.
|
void |
setCheckMethods(boolean checkMethods)
Setter for checkMethods property.
|
void |
setValidateOnlyOverlapping(boolean validateOnlyOverlapping)
Setter for validateOnlyOverlapping property.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
destroy, finishTree, getClassLoader, getFileContents, getLine, getLines, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, setClassLoader, setFileContents, setMessages, setTabWidth, setTokens
getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, log, setId, setSeverity
configure, contextualize, finishLocalSetup, getConfiguration, setupChild
public static final java.lang.String MSG_METHOD
public static final java.lang.String MSG_VARIABLE
private static final java.util.Set<java.lang.Integer> DECLARATION_TOKENS
private static final java.util.Set<java.lang.Integer> ASSIGN_TOKENS
private static final java.util.Set<java.lang.Integer> COMPOUND_ASSIGN_TOKENS
private java.util.Map<DetailAST,RequireThisCheck.AbstractFrame> frames
private RequireThisCheck.AbstractFrame current
private boolean checkFields
private boolean checkMethods
private boolean validateOnlyOverlapping
public void setCheckFields(boolean checkFields)
checkFields
- should we check fields usage or not.public void setCheckMethods(boolean checkMethods)
checkMethods
- should we check methods usage or not.public void setValidateOnlyOverlapping(boolean validateOnlyOverlapping)
validateOnlyOverlapping
- should we check only overlapping by variables or arguments.public int[] getDefaultTokens()
AbstractCheck
getDefaultTokens
in class AbstractCheck
TokenTypes
public int[] getRequiredTokens()
AbstractCheck
getRequiredTokens
in class AbstractCheck
TokenTypes
public int[] getAcceptableTokens()
AbstractCheck
getAcceptableTokens
in class AbstractCheck
TokenTypes
public void beginTree(DetailAST rootAST)
AbstractCheck
beginTree
in class AbstractCheck
rootAST
- the root of the treepublic void visitToken(DetailAST ast)
AbstractCheck
visitToken
in class AbstractCheck
ast
- the token to processprivate void processIdent(DetailAST ast)
this
qualifier.ast
- IDENT to check.private void logViolation(java.lang.String msgKey, DetailAST ast, RequireThisCheck.AbstractFrame frame)
ast
- a node to get line id column numbers associated with the message.msgKey
- key to locale message format.frame
- the class frame where the violation is found.private RequireThisCheck.AbstractFrame getFieldWithoutThis(DetailAST ast, int parentType)
ast
- field definition ast token.parentType
- type of the parent.private static void collectDeclarations(java.util.Deque<RequireThisCheck.AbstractFrame> frameStack, DetailAST ast)
frameStack
- stack containing the FrameTree being built.ast
- AST to parse.private static void collectVariableDeclarations(DetailAST ast, RequireThisCheck.AbstractFrame frame)
ast
- variable token.frame
- current frame.private void endCollectingDeclarations(java.util.Queue<RequireThisCheck.AbstractFrame> frameStack, DetailAST ast)
frameStack
- Stack containing the FrameTree being built.ast
- AST that was parsed.private static boolean isAnonymousClassDef(DetailAST ast)
ast
- the AST to process.private RequireThisCheck.AbstractFrame getClassFrameWhereViolationIsFound(DetailAST ast)
ast
- IDENT ast to check.private static boolean isUserDefinedArrangementOfThis(RequireThisCheck.AbstractFrame currentFrame, DetailAST ident)
currentFrame
- current frame.ident
- ident token.private static DetailAST getBlockEndToken(DetailAST blockNameIdent, DetailAST blockStartToken)
blockNameIdent
- block name identifier.blockStartToken
- token which starts the block.private static boolean isReturnedVariable(RequireThisCheck.AbstractFrame currentFrame, DetailAST ident)
currentFrame
- current frame.ident
- variable ident token.private boolean canBeReferencedFromStaticContext(DetailAST ident)
ident
- ident token.private static DetailAST getCodeBlockDefinitionToken(DetailAST ident)
ident
- ident token.private boolean canAssignValueToClassField(DetailAST ast)
ast
- an identifier token.private static boolean isInsideConstructorFrame(RequireThisCheck.AbstractFrame frame)
frame
- frame, where field is used.private boolean isOverlappingByArgument(DetailAST ast)
ast
- an identifier.private boolean isOverlappingByLocalVariable(DetailAST ast)
ast
- an identifier.private static java.util.Set<DetailAST> getAllTokensOfType(DetailAST ast, int tokenType)
ast
- ast node.tokenType
- token type.private static java.util.Set<DetailAST> getAllTokensOfType(DetailAST ast, int tokenType, int endLineNumber)
ast
- ast node.tokenType
- token type.endLineNumber
- end line number.private static java.util.Set<DetailAST> getAllTokensWhichAreEqualToCurrent(DetailAST ast, DetailAST token, int endLineNumber)
ast
- ast node.token
- token.endLineNumber
- end line number.private RequireThisCheck.AbstractFrame getMethodWithoutThis(DetailAST ast)
ast
- the IDENT ast of the name to check.private RequireThisCheck.AbstractFrame findClassFrame(DetailAST name, boolean lookForMethod)
name
- IDENT ast of the declaration to find.lookForMethod
- whether we are looking for a method name.private RequireThisCheck.AbstractFrame findFrame(DetailAST name, boolean lookForMethod)
name
- IDENT ast of the declaration to find.lookForMethod
- whether we are looking for a method name.private static RequireThisCheck.AbstractFrame findFrame(RequireThisCheck.AbstractFrame frame, DetailAST name, boolean lookForMethod)
frame
- The parent frame to searching in.name
- IDENT ast of the declaration to find.lookForMethod
- whether we are looking for a method name.private static boolean isDeclarationToken(int parentType)
parentType
- token Type.private static boolean isAssignToken(int tokenType)
tokenType
- token type.private static boolean isCompoundAssignToken(int tokenType)
tokenType
- token type.private java.lang.String getNearestClassFrameName()
private static boolean isLambdaParameter(DetailAST ast)
ast
- the DetailAST
value of the token to be checked