public class ForbidCertainMethodCheck
extends com.puppycrawl.tools.checkstyle.api.AbstractCheck
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.
| Modifier and Type | Field and Description |
|---|---|
static String |
MSG_KEY
Key is pointing to the warning message text in "messages.properties" file.
|
| Constructor and Description |
|---|
ForbidCertainMethodCheck() |
| Modifier and Type | Method and 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) |
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 setMethodName(String methodName)
methodName - regex for the method namepublic void setArgumentCount(String argumentCount) throws com.puppycrawl.tools.checkstyle.api.CheckstyleException
argumentCount - range for matching number of argumentscom.puppycrawl.tools.checkstyle.api.CheckstyleException - when argumentCount is not a valid rangepublic 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.