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:
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"})
| Constructor and Description |
|---|
ForbidAnnotationElementValueCheck() |
| Modifier and Type | Method and 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) |
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 - The annotation name.public void setElementName(String elementName)
elementName - The annotation element name.public void setForbiddenElementValueRegexp(String forbiddenElementValueRegexp)
forbiddenElementValueRegexp - The forbidden element value pattern to set.public int[] getDefaultTokens()
getDefaultTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic int[] getAcceptableTokens()
getAcceptableTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic int[] getRequiredTokens()
getRequiredTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic void visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
visitToken in class com.puppycrawl.tools.checkstyle.api.AbstractCheckCopyright © 2021. All rights reserved.