Class SingleUseConstantCheck

java.lang.Object
com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
com.puppycrawl.tools.checkstyle.api.AbstractCheck
com.qulice.checkstyle.SingleUseConstantCheck
All Implemented Interfaces:
com.puppycrawl.tools.checkstyle.api.Configurable, com.puppycrawl.tools.checkstyle.api.Contextualizable

public final class SingleUseConstantCheck extends com.puppycrawl.tools.checkstyle.api.AbstractCheck
Checks that a private static constant is mentioned more than once.

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
    Constructor
    Description
    Default constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    int[]
     
    int[]
     
    int[]
     
    void
    visitToken(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, setTokens

    Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter

    finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity

    Methods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean

    configure, contextualize, getConfiguration, setupChild

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • SingleUseConstantCheck

      public SingleUseConstantCheck()
      Default constructor.
  • Method Details

    • getDefaultTokens

      public int[] getDefaultTokens()
      Specified by:
      getDefaultTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
    • getAcceptableTokens

      public int[] getAcceptableTokens()
      Specified by:
      getAcceptableTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
    • getRequiredTokens

      public int[] getRequiredTokens()
      Specified by:
      getRequiredTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
    • visitToken

      public void visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
      Overrides:
      visitToken in class com.puppycrawl.tools.checkstyle.api.AbstractCheck