Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

com.puppycrawl.tools.checkstyle.checks
Class IllegalInstantiationCheck  view IllegalInstantiationCheck download IllegalInstantiationCheck.java

java.lang.Object
  extended bycom.puppycrawl.tools.checkstyle.api.AutomaticBean
      extended bycom.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
          extended bycom.puppycrawl.tools.checkstyle.api.Check
              extended bycom.puppycrawl.tools.checkstyle.checks.AbstractImportCheck
                  extended bycom.puppycrawl.tools.checkstyle.checks.IllegalInstantiationCheck
All Implemented Interfaces:
com.puppycrawl.tools.checkstyle.api.Configurable, com.puppycrawl.tools.checkstyle.api.Contextualizable

public class IllegalInstantiationCheck
extends AbstractImportCheck

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"/>
 


Field Summary
private  java.util.Set mIllegalClasses
          Set of fully qualified classnames.
private  java.util.Set mImports
          the imports for the file
private  java.lang.String mPkgName
          name of the package
 
Fields inherited from class com.puppycrawl.tools.checkstyle.api.Check
 
Fields inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
 
Fields inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
 
Constructor Summary
IllegalInstantiationCheck()
           
 
Method Summary
private static java.lang.String basename(java.lang.String aType)
           
 void beginTree()
           
 int[] getAcceptableTokens()
          Prevent user from changing tokens in the configuration.
 int[] getDefaultTokens()
          Returns the default token a check is interested in.
private  java.lang.String getIllegalInstantiation(java.lang.String aClassName)
          Checks illegal instantiations.
 int[] getRequiredTokens()
          The tokens that this check must be registered for.
private  void processImport(com.puppycrawl.tools.checkstyle.api.DetailAST aAST)
          Perform processing for an import token
private  void processLiteralNew(com.puppycrawl.tools.checkstyle.api.DetailAST aAST)
          Perform processing for an "new" token
private  void processPackageDef(com.puppycrawl.tools.checkstyle.api.DetailAST aAST)
          Perform processing for an package token
 void setClasses(java.lang.String aClassNames)
          Sets the classes that are illegal to instantiate.
 void visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST aAST)
          Called to process a token.
 
Methods inherited from class com.puppycrawl.tools.checkstyle.checks.AbstractImportCheck
getImportText
 
Methods inherited from class com.puppycrawl.tools.checkstyle.api.Check
beginTree, destroy, finishTree, getClassLoader, getFileContents, getLines, getTabWidth, getTokenNames, init, leaveToken, log, log, setClassLoader, setFileContents, setMessages, setTabWidth, setTokens
 
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
getMessageBundle, getSeverity, getSeverityLevel, log, log, log, log, log, log, setSeverity
 
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
configure, contextualize, finishLocalSetup, getConfiguration, setupChild
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

mIllegalClasses

private final java.util.Set mIllegalClasses
Set of fully qualified classnames. E.g. "java.lang.Boolean"


mPkgName

private java.lang.String mPkgName
name of the package


mImports

private final java.util.Set mImports
the imports for the file

Constructor Detail

IllegalInstantiationCheck

public IllegalInstantiationCheck()
Method Detail

getDefaultTokens

public int[] getDefaultTokens()
Description copied from class: com.puppycrawl.tools.checkstyle.api.Check
Returns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.


getAcceptableTokens

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


getRequiredTokens

public int[] getRequiredTokens()
Description copied from class: com.puppycrawl.tools.checkstyle.api.Check
The tokens that this check must be registered for.


beginTree

public void beginTree()

visitToken

public void visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST aAST)
Description copied from class: com.puppycrawl.tools.checkstyle.api.Check
Called to process a token.


processImport

private void processImport(com.puppycrawl.tools.checkstyle.api.DetailAST aAST)
Perform processing for an import token


processPackageDef

private void processPackageDef(com.puppycrawl.tools.checkstyle.api.DetailAST aAST)
Perform processing for an package token


processLiteralNew

private void processLiteralNew(com.puppycrawl.tools.checkstyle.api.DetailAST aAST)
Perform processing for an "new" token


getIllegalInstantiation

private java.lang.String getIllegalInstantiation(java.lang.String aClassName)
Checks illegal instantiations.


basename

private static java.lang.String basename(java.lang.String aType)

setClasses

public void setClasses(java.lang.String aClassNames)
Sets the classes that are illegal to instantiate.