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 {}
| Modifier and Type | Field and Description |
|---|---|
static String |
MSG_KEY
Key for error message.
|
| Constructor and Description |
|---|
RequiredParameterForAnnotationCheck() |
| Modifier and Type | Method and 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) |
beginTree, clearMessages, destroy, finishTree, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokensfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeveritypublic static final String MSG_KEY
public void setAnnotationName(String annotationName)
annotationName - set annotation namepublic void setRequiredParameters(String... requiredPropertiesParameter)
requiredPropertiesParameter - set required list of parameterspublic int[] getDefaultTokens()
getDefaultTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic int[] getRequiredTokens()
getRequiredTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic int[] getAcceptableTokens()
getAcceptableTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic void visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST annotationNode)
visitToken in class com.puppycrawl.tools.checkstyle.api.AbstractCheckCopyright © 2021. All rights reserved.