Class SingleUseConstantCheck
- All Implemented Interfaces:
com.puppycrawl.tools.checkstyle.api.Configurable,com.puppycrawl.tools.checkstyle.api.Contextualizable
A private static final field that the file mentions only
once is an indirection that pays for nothing: the reader has to jump
to the top of the class in order to learn what the single expression
below really says. Such a constant belongs inside its only usage,
since a constant starts to pay for itself at the second usage, where
it removes a duplicate.
Usages are counted as identifiers of the very same text anywhere in the file, the declaration of the constant included, which is why a constant used exactly once is mentioned exactly twice. A name that some other declaration happens to reuse is counted too, which only makes the check more forgiving.
Only a constant with a literal initializer is inspected. A
constant built by a method call or by new, like a compiled
Pattern, stays where it is, since inlining it
would move a computation into a method body and could turn a
once-per-class cost into a per-call one. The same goes for an array,
which is a mutable object no matter how final its field is.
A constant that a Javadoc comment refers to, the way {@value
#NAME} and {@link #NAME} do, is left alone as well, since
the documentation is a usage of its own and inlining the constant
would break it. So is serialVersionUID, which belongs to Java
serialization rather than to the class that declares it.
A constant that is not mentioned anywhere at all belongs to
ConstantUsageCheck, which reports it as unused.
- Since:
- 0.73.4
-
Nested Class Summary
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
com.puppycrawl.tools.checkstyle.AbstractAutomaticBean.OutputStreamOptions -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionint[]int[]int[]voidvisitToken(com.puppycrawl.tools.checkstyle.api.DetailAST ast) Methods inherited from class com.puppycrawl.tools.checkstyle.api.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 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
-
Constructor Details
-
SingleUseConstantCheck
public SingleUseConstantCheck()Default constructor.
-
-
Method Details
-
getDefaultTokens
public int[] getDefaultTokens()- Specified by:
getDefaultTokensin classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
getAcceptableTokens
public int[] getAcceptableTokens()- Specified by:
getAcceptableTokensin classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
getRequiredTokens
public int[] getRequiredTokens()- Specified by:
getRequiredTokensin classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
visitToken
public void visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST ast) - Overrides:
visitTokenin classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-