net.sourceforge.pmd.rules.strings
Class InefficientEmptyStringCheck

java.lang.Object
  extended by net.sourceforge.pmd.CommonAbstractRule
      extended by net.sourceforge.pmd.AbstractJavaRule
          extended by net.sourceforge.pmd.AbstractRule
              extended by net.sourceforge.pmd.rules.AbstractInefficientZeroCheck
                  extended by net.sourceforge.pmd.rules.strings.InefficientEmptyStringCheck
All Implemented Interfaces:
JavaParserVisitor, Rule

public class InefficientEmptyStringCheck
extends AbstractInefficientZeroCheck

This rule finds code which inefficiently determines empty strings. This code

         if(str.trim().length()==0){....
 

is quite inefficient as trim() causes a new String to be created. Smarter code to check for an empty string would be:

 Character.isWhitespace(str.charAt(i));
 

Author:
acaplan

Field Summary
 
Fields inherited from interface net.sourceforge.pmd.Rule
LOWEST_PRIORITY, PRIORITIES
 
Constructor Summary
InefficientEmptyStringCheck()
           
 
Method Summary
 boolean appliesToClassName(java.lang.String name)
           
 boolean isTargetMethod(NameOccurrence occ)
          Determine if we're dealing with String.length method
 
Methods inherited from class net.sourceforge.pmd.rules.AbstractInefficientZeroCheck
visit
 
Methods inherited from class net.sourceforge.pmd.AbstractJavaRule
addViolation, addViolation, addViolation, addViolationWithMessage, apply, getDeclaringType, importsPackage, isQualifiedName, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visitAll
 
Methods inherited from class net.sourceforge.pmd.CommonAbstractRule
addExample, addProperties, addProperty, addRuleChainVisit, asFixedMap, asFixedMap, end, equals, getBooleanProperties, getBooleanProperty, getBooleanProperty, getDescription, getDoubleProperties, getDoubleProperty, getDoubleProperty, getExample, getExamples, getExternalInfoUrl, getIntProperties, getIntProperty, getIntProperty, getMessage, getName, getPriority, getPriorityName, getProperties, getRuleChainVisits, getRuleClass, getRuleSetName, getSince, getStringProperties, getStringProperty, getStringProperty, getTypeProperties, getTypeProperty, hashCode, hasProperty, include, propertiesByName, propertyDescriptorFor, setDescription, setExternalInfoUrl, setInclude, setMessage, setName, setPriority, setProperties, setProperty, setRuleClass, setRuleSetName, setSince, setUsesDFA, setUsesTypeResolution, start, usesDFA, usesRuleChain, usesTypeResolution
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

InefficientEmptyStringCheck

public InefficientEmptyStringCheck()
Method Detail

isTargetMethod

public boolean isTargetMethod(NameOccurrence occ)
Determine if we're dealing with String.length method

Specified by:
isTargetMethod in class AbstractInefficientZeroCheck
Parameters:
occ - The name occurance
Returns:
true if it's String.length, else false

appliesToClassName

public boolean appliesToClassName(java.lang.String name)
Specified by:
appliesToClassName in class AbstractInefficientZeroCheck


Copyright © 2002-2011 InfoEther. All Rights Reserved.