Class ForbidAnnotationElementValueCheck

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

    public class ForbidAnnotationElementValueCheck
    extends com.puppycrawl.tools.checkstyle.api.AbstractCheck

    Forbids specific element value for specific annotation. You can configure this check using following options:

    • Annotation name
    • Annotation element name
    • Forbidden annotation element value pattern
    Example of usage:

    Here is XML configs and according code samples needed to forbid.

    To configure the check to forbid junit Test annotations with the element name "expected":

    Config

     <module name="ForbidAnnotationElementValue">
     </module>
     

    Code

      @Test(expected = Exception.class)
     

    To configure the check to forbid single-element annotation element value, like 'SuppressWarnings', elementName option should be specified as "value".

    Config

     <module name="ForbidAnnotationElementValue">
     <property name="annotationName" value="SuppressWarnings"/>
     <property name="elementName" value="value"/>
     <property name="forbiddenElementValueRegexp" value="unchecked"/>
     </module>
     

    Code

     @SuppressWarnings("unchecked")
     

    To forbid any array-valued element, forbiddenElementValueRegexp option should be: "\{.*\}".

    Config

     <module name="ForbidAnnotationElementValue">
     <property name="annotationName" value="SuppressWarnings"/>
     <property name="elementName" value="value"/>
     <property name="forbiddenElementValueRegexp" value="\{.*\}"/>
     </module>
     

    Code

     @SuppressWarnings({"unused", "unchecked"})
     
    Since:
    1.22.0
    Author:
    Sergey Drozd , Richard Veach
    • 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
      Message key.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int[] getAcceptableTokens()  
      int[] getDefaultTokens()  
      int[] getRequiredTokens()  
      void setAnnotationName​(String annotationName)
      Sets Annotation Name Check property.
      void setElementName​(String elementName)
      Sets Annotation Element Check property.
      void setForbiddenElementValueRegexp​(String forbiddenElementValueRegexp)
      Sets Forbidden Element Value Pattern Check property.
      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
    • Constructor Detail

      • ForbidAnnotationElementValueCheck

        public ForbidAnnotationElementValueCheck()
    • Method Detail

      • setAnnotationName

        public void setAnnotationName​(String annotationName)
        Sets Annotation Name Check property.
        Parameters:
        annotationName - The annotation name.
      • setElementName

        public void setElementName​(String elementName)
        Sets Annotation Element Check property.
        Parameters:
        elementName - The annotation element name.
      • setForbiddenElementValueRegexp

        public void setForbiddenElementValueRegexp​(String forbiddenElementValueRegexp)
        Sets Forbidden Element Value Pattern Check property.
        Parameters:
        forbiddenElementValueRegexp - The forbidden element value pattern to set.
      • 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