com.puppycrawl.tools.checkstyle.checks.coding

Class IllegalInstantiationCheck

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
voidbeginTree(DetailAST aRootAST)
{@inheritDoc}
voidfinishTree(DetailAST aRootAST)
{@inheritDoc}
int[]getAcceptableTokens()
Prevent user from changing tokens in the configuration.
int[]getDefaultTokens()
{@inheritDoc}
int[]getRequiredTokens()
{@inheritDoc}
voidsetClasses(String aClassNames)
Sets the classes that are illegal to instantiate.
voidvisitToken(DetailAST aAST)
{@inheritDoc}

Method Detail

beginTree

public void beginTree(DetailAST aRootAST)
{@inheritDoc}

finishTree

public void finishTree(DetailAST aRootAST)
{@inheritDoc}

getAcceptableTokens

public int[] getAcceptableTokens()
Prevent user from changing tokens in the configuration.

Returns: empty array to not allow user to change configuration.

See Also: Check

getDefaultTokens

public int[] getDefaultTokens()
{@inheritDoc}

getRequiredTokens

public int[] getRequiredTokens()
{@inheritDoc}

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)
{@inheritDoc}