Class RequiredParameterForAnnotationCheck

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

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

    Check that annotation is used with all required parameters.

    Parameters:
    annotationName - The name of the target annotation where enforcement of parameter should happen.
    requiredParameters - Set of parameter names that are required on the target annotation. Names can be specified on any order in target annotation.

    Example 1.
    Configuration:

     <module name="RequiredParameterForAnnotation">
         <property name="annotationName" value="TheAnnotation"/>
         <property name="requiredParameters" value="ThePropertyName1"/>
     </module>
     

    Result:

     
     @TheAnnotation() //Violation. ThePropertyName1 missing.
     someMethod() {}
    
     @TheAnnotation(ThePropertyName2=2) //Violation. ThePropertyName1 missing.
     class SomeClass {}
    
     @TheAnnotation(ThePropertyName1=1) //Correct.
     class SomeClass {}
    
     @TheAnnotation(ThePropertyName2=2, ThePropertyName3=3, ThePropertyName1=1) //Correct.
     class SomeClass {}
     
     

    Example 2.
    Configuration:

     <module name="RequiredParameterForAnnotation">
         <property name="annotationName" value="TheAnnotation"/>
         <property name="requiredParameters" value="ThePropertyName1,ThePropertyName2,
             ThePropertyName3"/>
     </module>
     

    Result:

     
     @TheAnnotation() //Violation. ThePropertyName 1, 2, 3 missing.
     someMethod() {}
    
     @TheAnnotation(ThePropertyName2=2) //Violation. ThePropertyName 1 and 3 missing.
     class SomeClass {}
    
     @TheAnnotation(ThePropertyName3=3, ThePropertyName2=2, ThePropertyName1=1) //Correct.
     class SomeClass {}
     
     
    Since:
    1.13.0
    Author:
    Andrew Uljanenko
    • 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 for error message.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int[] getAcceptableTokens()  
      int[] getDefaultTokens()  
      int[] getRequiredTokens()  
      void setAnnotationName​(String annotationName)
      The annotation name we are interested in.
      void setRequiredParameters​(String... requiredPropertiesParameter)
      The required list of parameters we have to use.
      void visitToken​(com.puppycrawl.tools.checkstyle.api.DetailAST annotationNode)  
      • 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

      • RequiredParameterForAnnotationCheck

        public RequiredParameterForAnnotationCheck()
    • Method Detail

      • setAnnotationName

        public void setAnnotationName​(String annotationName)
        The annotation name we are interested in.
        Parameters:
        annotationName - set annotation name
      • setRequiredParameters

        public void setRequiredParameters​(String... requiredPropertiesParameter)
        The required list of parameters we have to use.
        Parameters:
        requiredPropertiesParameter - set required list of parameters
      • 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
      • getAcceptableTokens

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

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