com.puppycrawl.tools.checkstyle.checks.coding

Class IllegalInstantiationCheck

Implemented Interfaces:
Configurable, Contextualizable

public class IllegalInstantiationCheck
extends Check

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, to save memory and CPU cycles it is preferable to use the predeifined 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.

Limitations: It is currently not possible to specify array classes.

An example of how to configure the check is:

 <module name="IllegalInstantiation"/>
 
Author:
lkuehne

Method Summary

void
beginTree(DetailAST aRootAST)
void
finishTree(DetailAST aRootAST)
int[]
getAcceptableTokens()
Prevent user from changing tokens in the configuration.
int[]
getDefaultTokens()
int[]
getRequiredTokens()
void
setClasses(String aClassNames)
Sets the classes that are illegal to instantiate.
void
visitToken(DetailAST aAST)

Methods inherited from class com.puppycrawl.tools.checkstyle.api.Check

beginTree, destroy, finishTree, getAcceptableTokens, getClassLoader, getDefaultTokens, getFileContents, getLines, getRequiredTokens, getTabWidth, getTokenNames, init, leaveToken, log, log, setClassLoader, setFileContents, setMessages, setTabWidth, setTokens, visitToken

Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter

getId, getMessageBundle, getSeverity, getSeverityLevel, log, log, log, log, log, log, log, log, log, log, log, setId, setSeverity

Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean

configure, contextualize, finishLocalSetup, getConfiguration, setupChild

Method Details

beginTree

public void beginTree(DetailAST aRootAST)
Overrides:
beginTree in interface Check

finishTree

public void finishTree(DetailAST aRootAST)
Overrides:
finishTree in interface Check

getAcceptableTokens

public int[] getAcceptableTokens()
Prevent user from changing tokens in the configuration.
Overrides:
getAcceptableTokens in interface Check
Returns:
empty array to not allow user to change configuration.
See Also:
Check

getDefaultTokens

public int[] getDefaultTokens()
Overrides:
getDefaultTokens in interface Check

getRequiredTokens

public int[] getRequiredTokens()
Overrides:
getRequiredTokens in interface Check

setClasses

public void setClasses(String aClassNames)
Sets the classes that are illegal to instantiate.
Parameters:
aClassNames - a comma seperate list of class names

visitToken

public void visitToken(DetailAST aAST)
Overrides:
visitToken in interface Check