public class MagicNumberCheck extends Check
Checks that there are no "magic numbers" where a magic number is a numeric literal that is not defined as a constant. By default, -1, 0, 1, and 2 are not considered to be magic numbers.
Check have following options: ignoreHashCodeMethod - ignore magic numbers in hashCode methods; ignoreAnnotation - ignore magic numbers in annotation declarations; ignoreFieldDeclaration - ignore magic numbers in field declarations.
To configure the check with default configuration:
<module name="MagicNumber"/>
results is following violations:
@MyAnnotation(6) // violation
class MyClass {
private field = 7; // violation
void foo() {
int i = i + 1; // no violation
int j = j + 8; // violation
}
}
To configure the check so that it checks floating-point numbers that are not 0, 0.5, or 1:
<module name="MagicNumber"> <property name="tokens" value="NUM_DOUBLE, NUM_FLOAT"/> <property name="ignoreNumbers" value="0, 0.5, 1"/> <property name="ignoreFieldDeclaration" value="true"/> <property name="ignoreAnnotation" value="true"/> </module>
results is following violations:
@MyAnnotation(6) // no violation
class MyClass {
private field = 7; // no violation
void foo() {
int i = i + 1; // no violation
int j = j + 8; // violation
}
}
Modifier and Type | Field and Description |
---|---|
static String |
MSG_KEY
A key is pointing to the warning message text in "messages.properties"
file.
|
Constructor and Description |
---|
MagicNumberCheck() |
Modifier and Type | Method and Description |
---|---|
int[] |
getAcceptableTokens()
The configurable token set.
|
int[] |
getDefaultTokens()
Returns the default token a check is interested in.
|
void |
setIgnoreAnnotation(boolean ignoreAnnotation)
Set whether to ignore Annotations.
|
void |
setIgnoreFieldDeclaration(boolean ignoreFieldDeclaration)
Set whether to ignore magic numbers in field declaration.
|
void |
setIgnoreHashCodeMethod(boolean ignoreHashCodeMethod)
Set whether to ignore hashCode methods.
|
void |
setIgnoreNumbers(double[] list)
Sets the numbers to ignore in the check.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
beginTree, destroy, finishTree, getClassLoader, getFileContents, getLine, getLines, getRequiredTokens, 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 String MSG_KEY
public int[] getDefaultTokens()
Check
getDefaultTokens
in class Check
TokenTypes
public int[] getAcceptableTokens()
Check
getAcceptableTokens
in class Check
TokenTypes
public void visitToken(DetailAST ast)
Check
visitToken
in class Check
ast
- the token to processpublic void setIgnoreNumbers(double[] list)
list
- list of numbers to ignore.public void setIgnoreHashCodeMethod(boolean ignoreHashCodeMethod)
ignoreHashCodeMethod
- decide whether to ignore
hash code methodspublic void setIgnoreAnnotation(boolean ignoreAnnotation)
ignoreAnnotation
- decide whether to ignore annotationspublic void setIgnoreFieldDeclaration(boolean ignoreFieldDeclaration)
ignoreFieldDeclaration
- decide whether to ignore magic numbers
in field declarationCopyright © 2001–2016. All rights reserved.