public class AvoidModifiersForTypesCheck
extends com.puppycrawl.tools.checkstyle.api.AbstractCheck
Disallow some set of modifiers for Java types specified by regexp.
Field modifiers types according to Java Spec: (https://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.3.1)
Example 1: Forbid use of 'static' modifiers for 'ULCComponents' (http://ulc.canoo.com/ulccommunity/Contributions/Extensions/GoodPractices.html)
Never keep instances of ULC classes in static variables (ULCIcons neither!). They cannot be shared between different sessions.
So we can disallow "static" modifier for all ULC* components by setting up an "forbiddenClassesRegexpStatic" option to "ULC.+" regexp String.
Configuration:
<module name="TreeWalker">
<module name="AvoidModifiersForTypesCheck">
<property name="forbiddenClassesRegexpStatic" value="ULC.+"/>
</module>
</module>
Example 2: Forbid using annotation for fields: (e.g. @Autowired ). This
can be done by setting up the "forbiddenClassesRegexpAnnotation" option to "Person" regexp
String.
Configuration:
<module name="TreeWalker">
<module name="AvoidModifiersForTypesCheck">
<property name="forbiddenClassesRegexpAnnotation" value="Person"/>
</module>
</module>
public class Customer {
@Autowired
private Person person; // Violation
private int type; // OK
private String action; // OK
}
Example 3: Forbid returning Logger out of the class, since it is a very bad practice as it produce logs that are hard to investigate as logging class does not contains that code and search should be done in other classes or in hierarchy (if filed is public or accessible by other protected or package).
This check can be activated by setting up the "forbiddenClassesRegexpPublic", "forbiddenClassesRegexpPackagePrivate" and "forbiddenClassesRegexpProtected" options to "Logger" regexp String.
Configuration:
<module name="TreeWalker">
<module name="AvoidModifiersForTypesCheck">
<property name="forbiddenClassesRegexpProtected" value="Logger"/>
<property name="forbiddenClassesRegexpPublic" value="Logger"/>
<property name="forbiddenClassesRegexpPackagePrivate" value="Logger"/>
<module>
</module>
public class Check {
private Logger log1 = Logger.getLogger(getClass().getName()); // OK
protected Logger log2 = Logger.getLogger(getClass().getName()); // Violation
public Logger log3 = Logger.getLogger(getClass().getName()); // Violation
Logger log4 = Logger.getLogger(getClass().getName()); // Violation
}
| Modifier and Type | Field and Description |
|---|---|
static String |
MSG_KEY
The key is pointing to the message text String in
"messages.properties file".
|
| Constructor and Description |
|---|
AvoidModifiersForTypesCheck() |
| Modifier and Type | Method and Description |
|---|---|
int[] |
getAcceptableTokens() |
int[] |
getDefaultTokens() |
int[] |
getRequiredTokens() |
void |
setForbiddenClassesRegexpAnnotation(String forbiddenClassesRegexpAnnotation)
Sets the regexp for the names of classes, that could not have 'annotation'
modifier.
|
void |
setForbiddenClassesRegexpFinal(String forbiddenClassesRegexpFinal)
Sets the regexp for the names of classes, that could not have 'final'
modifier.
|
void |
setForbiddenClassesRegexpPackagePrivate(String forbiddenClassesRegexpPackagePrivate)
Sets the regexp for the names of classes, that could not have no modifier
('package-private').
|
void |
setForbiddenClassesRegexpPrivate(String forbiddenClassesRegexpPrivate)
Sets the regexp for the names of classes, that could not have 'private'
modifier.
|
void |
setForbiddenClassesRegexpProtected(String forbiddenClassesRegexpProtected)
Sets the regexp for the names of classes, that could not have 'protected'
modifier.
|
void |
setForbiddenClassesRegexpPublic(String forbiddenClassesRegexpPublic)
Sets the regexp for the names of classes, that could not have 'public'
modifier.
|
void |
setForbiddenClassesRegexpStatic(String forbiddenClassesRegexpStatic)
Sets the regexp for the names of classes, that could not have 'static'
modifier.
|
void |
setForbiddenClassesRegexpTransient(String forbiddenClassesRegexpTransient)
Sets the regexp for the names of classes, that could not have 'transient'
modifier.
|
void |
setForbiddenClassesRegexpVolatile(String forbiddenClassesRegexpVolatile)
Sets the regexp for the names of classes, that could not have 'volatile'
modifier.
|
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 setForbiddenClassesRegexpAnnotation(String forbiddenClassesRegexpAnnotation)
forbiddenClassesRegexpAnnotation - String contains the regex to set for the names of classes, that
could not have 'annotation' modifier.public void setForbiddenClassesRegexpFinal(String forbiddenClassesRegexpFinal)
forbiddenClassesRegexpFinal - String contains the regex to set for the names of classes, that
could not have 'final' modifier.public void setForbiddenClassesRegexpStatic(String forbiddenClassesRegexpStatic)
forbiddenClassesRegexpStatic - String contains the regex to set for the names of classes, that
could not have 'static' modifier.public void setForbiddenClassesRegexpTransient(String forbiddenClassesRegexpTransient)
forbiddenClassesRegexpTransient - String contains the regex to set for the names of classes, that
could not have 'transient' modifier.public void setForbiddenClassesRegexpVolatile(String forbiddenClassesRegexpVolatile)
forbiddenClassesRegexpVolatile - String contains the regex to set for the names of classes, that
could not have 'volatile' modifier.public void setForbiddenClassesRegexpPrivate(String forbiddenClassesRegexpPrivate)
forbiddenClassesRegexpPrivate - String contains the regex to set for the names of classes, that
could not have 'private' modifier.public void setForbiddenClassesRegexpPackagePrivate(String forbiddenClassesRegexpPackagePrivate)
forbiddenClassesRegexpPackagePrivate - String contains the regex to set for the names of classes, that
could not have no modifier ('package-private').public void setForbiddenClassesRegexpProtected(String forbiddenClassesRegexpProtected)
forbiddenClassesRegexpProtected - String contains the regex to set for the names of classes, that
could not have 'protected' modifier.public void setForbiddenClassesRegexpPublic(String forbiddenClassesRegexpPublic)
forbiddenClassesRegexpPublic - String contains the regex to set for the names of classes, that
could not have 'public' modifier.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.