Class MagicNumberCheck
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.MagicNumberCheck
- All Implemented Interfaces:
Configurable, Contextualizable
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.
Constant definition is any variable/field that has 'final' modifier. It is fine to have one constant defining multiple numeric literals within one expression:
static final int SECONDS_PER_DAY = 24 * 60 * 60;
static final double SPECIAL_RATIO = 4.0 / 3.0;
static final double SPECIAL_SUM = 1 + Math.E;
static final double SPECIAL_DIFFERENCE = 4 - Math.PI;
static final Border STANDARD_BORDER = BorderFactory.createEmptyBorder(3, 3, 3, 3);
static final Integer ANSWER_TO_THE_ULTIMATE_QUESTION_OF_LIFE = new Integer(42);
- Since:
- 3.1
-
Nested Class Summary
Nested classes/interfaces inherited from class AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate BitSetSpecify tokens that are allowed in the AST path from the number literal to the enclosing constant definition.private booleanIgnore magic numbers in annotation declarations.private booleanIgnore magic numbers in annotation elements defaults.private booleanIgnore magic numbers in field declarations.private booleanIgnore magic numbers in hashCode methods.private double[]Specify non-magic numbers.static final StringA key is pointing to the warning message text in "messages.properties" file. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static DetailASTFinds the constant definition that contains aAST.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 static booleanDetermines if the given AST node has a parent node with given token type code.private static booleanDetermines whether or not the given AST is field declaration.private static booleanDetermines whether or not the given AST is in a valid hash code method.private booleanisInIgnoreList(DetailAST ast) Decides whether the number of an AST is in the ignore list of this check.private booleanisMagicNumberExists(DetailAST ast, DetailAST constantDefAST) Is magic number somewhere at ast tree.private voidReports aAST as a magic number, includes unary operators as needed.voidsetConstantWaiverParentToken(String... tokens) Setter to specify tokens that are allowed in the AST path from the number literal to the enclosing constant definition.voidsetIgnoreAnnotation(boolean ignoreAnnotation) Setter to ignore magic numbers in annotation declarations.voidsetIgnoreAnnotationElementDefaults(boolean ignoreAnnotationElementDefaults) Setter to ignore magic numbers in annotation elements defaults.voidsetIgnoreFieldDeclaration(boolean ignoreFieldDeclaration) Setter to ignore magic numbers in field declarations.voidsetIgnoreHashCodeMethod(boolean ignoreHashCodeMethod) Setter to ignore magic numbers in hashCode methods.voidsetIgnoreNumbers(double... list) Setter to specify non-magic numbers.private booleanChecks if the given AST node is a field declaration and should be checked.private booleanChecks if the given AST node is a HashCode Method and should be checked.private booleanChecks if ast is annotation argument and should be checked.private booleanChecks if ast is annotation element default value and should be checked.voidvisitToken(DetailAST ast) Called to process a token.Methods inherited from class AbstractCheck
beginTree, clearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokensMethods inherited from class AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityMethods inherited from class AbstractAutomaticBean
configure, contextualize, getConfiguration, setupChild
-
Field Details
-
MSG_KEY
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
constantWaiverParentToken
Specify tokens that are allowed in the AST path from the number literal to the enclosing constant definition. -
ignoreNumbers
private double[] ignoreNumbersSpecify non-magic numbers. -
ignoreHashCodeMethod
private boolean ignoreHashCodeMethodIgnore magic numbers in hashCode methods. -
ignoreAnnotation
private boolean ignoreAnnotationIgnore magic numbers in annotation declarations. -
ignoreFieldDeclaration
private boolean ignoreFieldDeclarationIgnore magic numbers in field declarations. -
ignoreAnnotationElementDefaults
private boolean ignoreAnnotationElementDefaultsIgnore magic numbers in annotation elements defaults.
-
-
Constructor Details
-
MagicNumberCheck
public MagicNumberCheck()
-
-
Method Details
-
getDefaultTokens
public int[] 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
public int[] 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:
-
getRequiredTokens
public int[] 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:
-
visitToken
Description copied from class:AbstractCheckCalled to process a token.- Overrides:
visitTokenin classAbstractCheck- Parameters:
ast- the token to process
-
shouldTestAnnotationArgs
Checks if ast is annotation argument and should be checked.- Parameters:
ast- token to check- Returns:
- true if element is skipped, false otherwise
-
shouldTestAnnotationDefaults
Checks if ast is annotation element default value and should be checked.- Parameters:
ast- token to check- Returns:
- true if element is skipped, false otherwise
-
shouldCheckHashCodeMethod
Checks if the given AST node is a HashCode Method and should be checked.- Parameters:
ast- the AST node to check- Returns:
- true if element should be checked, false otherwise
-
shouldCheckFieldDeclaration
Checks if the given AST node is a field declaration and should be checked.- Parameters:
ast- the AST node to check- Returns:
- true if element should be checked, false otherwise
-
isMagicNumberExists
-
findContainingConstantDef
-
reportMagicNumber
Reports aAST as a magic number, includes unary operators as needed.- Parameters:
ast- the AST node that contains the number to report
-
isInHashCodeMethod
Determines whether or not the given AST is in a valid hash code method. A valid hash code method is considered to be a method of the signaturepublic int hashCode().- Parameters:
ast- the AST from which to search for an enclosing hash code method definition- Returns:
trueifastis in the scope of a valid hash code method.
-
isInIgnoreList
Decides whether the number of an AST is in the ignore list of this check.- Parameters:
ast- the AST to check- Returns:
- true if the number of ast is in the ignore list of this check.
-
isFieldDeclaration
Determines whether or not the given AST is field declaration.- Parameters:
ast- AST from which to search for an enclosing field declaration- Returns:
trueifastis in the scope of field declaration
-
setConstantWaiverParentToken
Setter to specify tokens that are allowed in the AST path from the number literal to the enclosing constant definition.- Parameters:
tokens- The string representation of the tokens interested in- Since:
- 6.11
-
setIgnoreNumbers
public void setIgnoreNumbers(double... list) Setter to specify non-magic numbers.- Parameters:
list- numbers to ignore.- Since:
- 3.1
-
setIgnoreHashCodeMethod
public void setIgnoreHashCodeMethod(boolean ignoreHashCodeMethod) Setter to ignore magic numbers in hashCode methods.- Parameters:
ignoreHashCodeMethod- decide whether to ignore hash code methods- Since:
- 5.3
-
setIgnoreAnnotation
public void setIgnoreAnnotation(boolean ignoreAnnotation) Setter to ignore magic numbers in annotation declarations.- Parameters:
ignoreAnnotation- decide whether to ignore annotations- Since:
- 5.4
-
setIgnoreFieldDeclaration
public void setIgnoreFieldDeclaration(boolean ignoreFieldDeclaration) Setter to ignore magic numbers in field declarations.- Parameters:
ignoreFieldDeclaration- decide whether to ignore magic numbers in field declaration- Since:
- 6.6
-
setIgnoreAnnotationElementDefaults
public void setIgnoreAnnotationElementDefaults(boolean ignoreAnnotationElementDefaults) Setter to ignore magic numbers in annotation elements defaults.- Parameters:
ignoreAnnotationElementDefaults- decide whether to ignore annotation elements defaults- Since:
- 8.23
-
isChildOf
Determines if the given AST node has a parent node with given token type code.- Parameters:
ast- the AST from which to search for annotationstype- the type code of parent token- Returns:
trueif the AST node has a parent with given token type.
-