Class ForbidCertainMethodCheck

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

    public class ForbidCertainMethodCheck
    extends com.puppycrawl.tools.checkstyle.api.AbstractCheck
    Check that forbidden method is not used. We can forbid a method by method name and number of arguments. This can be used to enforce things like:
    • exit() method of System class should not be called.
    • assertTrue() and assertFalse() methods of Assert class have a 1 arg variant that does not provide a helpful message on failure. These methods should not be used.
    Parameters are:
    • methodName - Regex to match name of the method to be forbidden. When blank or unspecified, all the methods are allowed.
    • argumentCount - Number or range to match number of arguments the method takes. Multiple numbers/ranges must be comma separated. When unspecified, defaults to "0-".

    An example configuration:

     <module name="ForbidCertainMethodCheck">
        <property name="methodName" value="exit"/>
     </module>
     <module name="ForbidCertainMethodCheck">
        <property name="methodName" value="assert(True|False)"/>
        <property name="argumentCount" value="1"/>
     </module>
     <module name="ForbidCertainMethodCheck">
        <property name="methodName" value="assertEquals"/>
        <property name="argumentCount" value="2"/>
     </module>
     
    Argument count can be bounded range (e.g.: 2-4) or unbounded range (e.g.: -5, 6-). Unbounded range can be unbounded only on one side. Multiple ranges must be comma separated. For example, the following will allow only 4 and 8 arguments.
     <module name="ForbidCertainMethodCheck">
        <property name="methodName" value="asList"/>
        <property name="argumentCount" value="-3, 5-7, 9-"/>
     </module>
     

    Note: The check only matches method name. Matching on class/object of the method is not done. For e.g. there is no way to forbid only "System.exit()". You can match by methodName="exit", but beware that it will violate "System.exit()" and "MySystem.exit()", so use it with caution.

    Since:
    1.28.0
    Author:
    Raghav Kumar Gautam
    • 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
      Key is pointing to the warning message text in "messages.properties" file.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int[] getAcceptableTokens()  
      int[] getDefaultTokens()  
      int[] getRequiredTokens()  
      void setArgumentCount​(String argumentCount)
      Set number or range to match number of arguments of the forbidden method.
      void setMethodName​(String methodName)
      Set method name regex for the forbidden method.
      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.api.AutomaticBean

        configure, contextualize, getConfiguration, setupChild
    • Field Detail

      • MSG_KEY

        public static final String MSG_KEY
        Key is pointing to the warning message text in "messages.properties" file.
        See Also:
        Constant Field Values
    • Constructor Detail

      • ForbidCertainMethodCheck

        public ForbidCertainMethodCheck()
    • Method Detail

      • setMethodName

        public void setMethodName​(String methodName)
        Set method name regex for the forbidden method.
        Parameters:
        methodName - regex for the method name
      • setArgumentCount

        public void setArgumentCount​(String argumentCount)
                              throws com.puppycrawl.tools.checkstyle.api.CheckstyleException
        Set number or range to match number of arguments of the forbidden method. Multiple values must be comma separated.
        Parameters:
        argumentCount - range for matching number of arguments
        Throws:
        com.puppycrawl.tools.checkstyle.api.CheckstyleException - when argumentCount is not a valid range
      • 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