public class ExplicitInitializationCheck extends AbstractCheck
Checks if any class or object member explicitly initialized
to default for its type value (null
for object
references, zero for numeric types and char
and false
for boolean
.
Rationale: each instance variable gets initialized twice, to the same value. Java initializes each instance variable to its default value (0 or null) before performing any initialization specified in the code. So in this case, x gets initialized to 0 twice, and bar gets initialized to null twice. So there is a minor inefficiency. This style of coding is a hold-over from C/C++ style coding, and it shows that the developer isn't really confident that Java really initializes instance variables to default values.
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 boolean |
onlyObjectReferences
Whether only explicit initialization made to null should be checked.
|
Constructor and Description |
---|
ExplicitInitializationCheck() |
Modifier and Type | Method and Description |
---|---|
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.
|
private static boolean |
isNumericType(int type)
Determine if a given type is a numeric type.
|
private static boolean |
isObjectType(DetailAST type)
Determines if a given type is an object type.
|
private static boolean |
isSkipCase(DetailAST ast)
Checks for cases that should be skipped: no assignment, local variable, final variables.
|
private static boolean |
isZero(DetailAST expr)
Checks if given node contains numeric constant for zero.
|
private static boolean |
isZeroChar(DetailAST exprStart)
Examine char literal for initializing to default value.
|
void |
setOnlyObjectReferences(boolean onlyObjectReferences)
Sets whether only explicit initialization made to null should be checked.
|
private void |
validateNonObjects(DetailAST ast)
Checks for explicit initializations made to 'false', '0' and '\0'.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
beginTree, 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_KEY
private boolean onlyObjectReferences
public final int[] getDefaultTokens()
AbstractCheck
getDefaultTokens
in class AbstractCheck
TokenTypes
public final int[] getRequiredTokens()
AbstractCheck
getRequiredTokens
in class AbstractCheck
TokenTypes
public final int[] getAcceptableTokens()
AbstractCheck
getAcceptableTokens
in class AbstractCheck
TokenTypes
public void setOnlyObjectReferences(boolean onlyObjectReferences)
onlyObjectReferences
- whether only explicit initialization made to null
should be checkedpublic void visitToken(DetailAST ast)
AbstractCheck
visitToken
in class AbstractCheck
ast
- the token to processprivate void validateNonObjects(DetailAST ast)
ast
- token being checked for explicit initializationsprivate static boolean isZeroChar(DetailAST exprStart)
exprStart
- expressionprivate static boolean isSkipCase(DetailAST ast)
ast
- Variable def ASTprivate static boolean isObjectType(DetailAST type)
type
- type to check.private static boolean isNumericType(int type)
type
- code of the type for check.TokenTypes
private static boolean isZero(DetailAST expr)
expr
- node to check.