public class IllegalInstantiationCheck extends AbstractCheck
Checks for illegal instantiations where a factory method is preferred.
Rationale: Depending on the project, for some classes it might be preferable to create instances through factory methods rather than calling the constructor.
A simple example is the java.lang.Boolean class.
For performance reasons, it is preferable to use the predefined constants
TRUE and FALSE.
Constructor invocations should be replaced by calls to Boolean.valueOf().
Some extremely performance sensitive projects may require the use of factory methods for other classes as well, to enforce the usage of number caches or object pools.
There is a limitation that it is currently not possible to specify array classes.
classes - Specify fully qualified class names that should not be instantiated.
Type is java.lang.String[].
Default value is {}.
tokens - tokens to check
Type is int[].
Default value is:
CLASS_DEF.
To configure the check to find instantiations of java.lang.Boolean:
<module name="IllegalInstantiation"> <property name="classes" value="java.lang.Boolean"/> </module>
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
instantiation.avoid
AutomaticBean.OutputStreamOptions| Modifier and Type | Field and Description |
|---|---|
static String |
MSG_KEY
A key is pointing to the warning message text in "messages.properties"
file.
|
| Constructor and Description |
|---|
IllegalInstantiationCheck() |
| Modifier and Type | Method and Description |
|---|---|
void |
beginTree(DetailAST rootAST)
Called before the starting to process a tree.
|
void |
finishTree(DetailAST rootAST)
Called after finished processing a tree.
|
int[] |
getAcceptableTokens()
The configurable token set.
|
int[] |
getDefaultTokens()
Returns the default token a check is interested in.
|
int[] |
getRequiredTokens()
The tokens that this check must be registered for.
|
void |
setClasses(String... names)
Setter to specify fully qualified class names that should not be instantiated.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
clearMessages, destroy, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokensfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityconfigure, contextualize, getConfiguration, setupChildpublic static final String MSG_KEY
public IllegalInstantiationCheck()
public int[] getDefaultTokens()
AbstractCheckgetDefaultTokens in class AbstractCheckTokenTypespublic int[] getAcceptableTokens()
AbstractCheckgetAcceptableTokens in class AbstractCheckTokenTypespublic int[] getRequiredTokens()
AbstractCheckgetRequiredTokens in class AbstractCheckTokenTypespublic void beginTree(DetailAST rootAST)
AbstractCheckbeginTree in class AbstractCheckrootAST - the root of the treepublic void visitToken(DetailAST ast)
AbstractCheckvisitToken in class AbstractCheckast - the token to processpublic void finishTree(DetailAST rootAST)
AbstractCheckfinishTree in class AbstractCheckrootAST - the root of the treepublic void setClasses(String... names)
names - a comma separate list of class namesCopyright © 2001–2020. All rights reserved.