Class StaticMethodCandidateCheck

  • All Implemented Interfaces:
    com.puppycrawl.tools.checkstyle.api.Configurable, com.puppycrawl.tools.checkstyle.api.Contextualizable

    public class StaticMethodCandidateCheck
    extends com.puppycrawl.tools.checkstyle.api.AbstractCheck
    Checks whether private methods can be declared as static.

    The check has option skippedMethods which allows to specify the list of comma separated names of methods to skip during the check. By default the private methods which a class can have when it implements Serializable are skipped: "readObject, writeObject, readObjectNoData, readResolve, writeReplace".

    The following configuration allows to skip method foo and bar:

         <module name="NestedSwitchCheck">
             <property name="skippedMethods" value="foo, bar"/>
         </module>
     

    Limitations:

    • Due to limitation of Checkstyle, there is no ability to distinguish overloaded methods, so we skip them from candidates.
    • Private methods called by reflection are not supported and have to be suppressed.
    Since:
    1.17.0
    Author:
    Vladislav Lisetskiy
    • Nested Class Summary

      • Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean

        com.puppycrawl.tools.checkstyle.api.AutomaticBean.OutputStreamOptions
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static String MSG_KEY
      Warning message key.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void beginTree​(com.puppycrawl.tools.checkstyle.api.DetailAST rootAST)  
      void finishTree​(com.puppycrawl.tools.checkstyle.api.DetailAST ast)  
      int[] getAcceptableTokens()  
      int[] getDefaultTokens()  
      int[] getRequiredTokens()  
      void leaveToken​(com.puppycrawl.tools.checkstyle.api.DetailAST ast)  
      void setSkippedMethods​(String skippedMethods)
      Sets custom skipped methods.
      void visitToken​(com.puppycrawl.tools.checkstyle.api.DetailAST ast)  
      • Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck

        clearViolations, destroy, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, 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.api.AutomaticBean

        configure, contextualize, getConfiguration, setupChild
    • Constructor Detail

      • StaticMethodCandidateCheck

        public StaticMethodCandidateCheck()
    • Method Detail

      • setSkippedMethods

        public void setSkippedMethods​(String skippedMethods)
        Sets custom skipped methods.
        Parameters:
        skippedMethods - user's skipped methods.
      • getAcceptableTokens

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

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

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

        public void beginTree​(com.puppycrawl.tools.checkstyle.api.DetailAST rootAST)
        Overrides:
        beginTree 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
      • leaveToken

        public void leaveToken​(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
        Overrides:
        leaveToken in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • finishTree

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