com.jimischopp.checkstyle
Class ResourceUseInLoopCheck

java.lang.Object
  extended by com.puppycrawl.tools.checkstyle.api.AutomaticBean
      extended by com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
          extended by com.puppycrawl.tools.checkstyle.api.Check
              extended by com.jimischopp.checkstyle.AbstractBaseChecker
                  extended by com.jimischopp.checkstyle.ResourceUseInLoopCheck
All Implemented Interfaces:
com.puppycrawl.tools.checkstyle.api.Configurable, com.puppycrawl.tools.checkstyle.api.Contextualizable

public class ResourceUseInLoopCheck
extends AbstractBaseChecker

Finds calls to resource objects which should not be performed in loops. For example, assuming your resource is a database object, you should avoid calling queries in loops (better to find some mechanism to call it once on the outside of the loop).

The way of "detecting" a resource object is by simply looking at a regular expression on all method calls (is resourceIsParam is false) or on all parameters to method calls (if resourceIsParam is true). If the expression is matched, we then look to see if it is found within a loop structure.

  
      
      
   
  
For example, the above will look for anywhere that a variable named "conn*" is used as parameter to a method call (as this most likely indicates you are using a DB connection in a loop).
  
      
      
   
  
The above will look for anywhere that a method name "execute*" is called (regardless of the object it is called on) (as this most likely indicates you are using a statement in a loop).

Copyright 2003, James Schopp

Since:
Oct 27, 2003
Author:
James Schopp
See Also:
Check

Field Summary
 java.lang.String DEFAULT_RESOURCE_FORMAT
           
 
Constructor Summary
ResourceUseInLoopCheck()
           
 
Method Summary
 int[] getAcceptableTokens()
           
 int[] getDefaultTokens()
           
 java.lang.String getFormat()
           
 java.util.regex.Pattern getRegexp()
           
 int[] getRequiredTokens()
           
 boolean isResourceIsParam()
           
 void setFormat(java.lang.String aFormat)
          Set the format to the specified regular expression.
 void setResourceIsParam(boolean resourceIsParam)
           
 void visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
           
 
Methods inherited from class com.jimischopp.checkstyle.AbstractBaseChecker
isInsideLoop
 
Methods inherited from class com.puppycrawl.tools.checkstyle.api.Check
beginTree, destroy, finishTree, getClassLoader, getFileContents, getLines, getTokenNames, init, leaveToken, setClassLoader, setFileContents, setMessages, setTabWidth, setTokens
 
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
getId, getSeverity, getSeverityLevel, setId, setSeverity
 
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
configure, contextualize
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_RESOURCE_FORMAT

public final java.lang.String DEFAULT_RESOURCE_FORMAT
See Also:
Constant Field Values
Constructor Detail

ResourceUseInLoopCheck

public ResourceUseInLoopCheck()
Method Detail

setFormat

public void setFormat(java.lang.String aFormat)
Set the format to the specified regular expression.

Parameters:
aFormat - a String value

getRegexp

public java.util.regex.Pattern getRegexp()
Returns:
the regexp to match against

getFormat

public java.lang.String getFormat()
Returns:
the regexp format

getDefaultTokens

public int[] getDefaultTokens()
Specified by:
getDefaultTokens in class com.puppycrawl.tools.checkstyle.api.Check

getAcceptableTokens

public int[] getAcceptableTokens()
Overrides:
getAcceptableTokens in class com.puppycrawl.tools.checkstyle.api.Check

getRequiredTokens

public int[] getRequiredTokens()
Overrides:
getRequiredTokens in class com.puppycrawl.tools.checkstyle.api.Check

visitToken

public void visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
Overrides:
visitToken in class com.puppycrawl.tools.checkstyle.api.Check

isResourceIsParam

public boolean isResourceIsParam()

setResourceIsParam

public void setResourceIsParam(boolean resourceIsParam)