Class InefficientEmptyStringCheckRule
- java.lang.Object
-
- net.sourceforge.pmd.properties.AbstractPropertySource
-
- net.sourceforge.pmd.lang.rule.AbstractRule
-
- net.sourceforge.pmd.lang.java.rule.AbstractJavaRule
-
- net.sourceforge.pmd.lang.java.rule.AbstractInefficientZeroCheck
-
- net.sourceforge.pmd.lang.java.rule.performance.InefficientEmptyStringCheckRule
-
- All Implemented Interfaces:
JavaParserVisitor
,net.sourceforge.pmd.lang.rule.ImmutableLanguage
,net.sourceforge.pmd.properties.PropertySource
,net.sourceforge.pmd.Rule
public class InefficientEmptyStringCheckRule extends AbstractInefficientZeroCheck
This rule finds code which inefficiently determines empty strings.str.trim().length()==0
orstr.trim().isEmpty()
(for the same reason) is quite inefficient as trim() causes a new String to be created. A Smarter code to check for an empty string would be:private boolean checkTrimEmpty(String str) { for(int i = 0; i < str.length(); i++) { if(!Character.isWhitespace(str.charAt(i))) { return false; } } return true; }
or you can refer to Apache'sStringUtils#isBlank
(in commons-lang), Spring'sStringUtils#hasText
(in the Spring framework) or Google'sCharMatcher#whitespace
(in Guava) for existing implementations (some might include the check for != null).- Author:
- acaplan
-
-
Constructor Summary
Constructors Constructor Description InefficientEmptyStringCheckRule()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
appliesToClassName(java.lang.String name)
boolean
isTargetMethod(JavaNameOccurrence occ)
java.lang.Object
visit(ASTPrimaryExpression node, java.lang.Object data)
-
Methods inherited from class net.sourceforge.pmd.lang.java.rule.AbstractInefficientZeroCheck
checkNodeAndReport, getComparisonTargets, visit
-
Methods inherited from class net.sourceforge.pmd.lang.java.rule.AbstractJavaRule
apply, getDeclaringType, importsPackage, isQualifiedName, isSuppressed, 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, visit, visit, visit, visit, visitAll
-
Methods inherited from class net.sourceforge.pmd.lang.rule.AbstractRule
addExample, addRuleChainVisit, addRuleChainVisit, addViolation, addViolation, addViolation, addViolationWithMessage, addViolationWithMessage, addViolationWithMessage, deepCopy, deepCopyValuesTo, end, equals, getDescription, getExamples, getExternalInfoUrl, getLanguage, getMaximumLanguageVersion, getMessage, getMinimumLanguageVersion, getName, getParserOptions, getPriority, getPropertySourceType, getRuleChainVisits, getRuleClass, getRuleSetName, getSince, hashCode, isDeprecated, isDfa, isMultifile, isRuleChain, isTypeResolution, setDeprecated, setDescription, setDfa, setExternalInfoUrl, setLanguage, setMaximumLanguageVersion, setMessage, setMinimumLanguageVersion, setMultifile, setName, setPriority, setRuleClass, setRuleSetName, setSince, setTypeResolution, setUsesDFA, setUsesMultifile, setUsesTypeResolution, start, usesDFA, usesMultifile, usesRuleChain, usesTypeResolution
-
Methods inherited from class net.sourceforge.pmd.properties.AbstractPropertySource
copyPropertyDescriptors, copyPropertyValues, definePropertyDescriptor, dysfunctionReason, getOverriddenPropertiesByPropertyDescriptor, getOverriddenPropertyDescriptors, getPropertiesByPropertyDescriptor, getProperty, getPropertyDescriptor, getPropertyDescriptors, hasDescriptor, ignoredProperties, isPropertyOverridden, setProperty, setProperty, useDefaultValueFor, usesDefaultValues
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface net.sourceforge.pmd.properties.PropertySource
definePropertyDescriptor, dysfunctionReason, getOverriddenPropertiesByPropertyDescriptor, getOverriddenPropertyDescriptors, getPropertiesByPropertyDescriptor, getProperty, getPropertyDescriptor, getPropertyDescriptors, hasDescriptor, ignoredProperties, isPropertyOverridden, setProperty, setProperty, useDefaultValueFor, usesDefaultValues
-
-
-
-
Method Detail
-
isTargetMethod
public boolean isTargetMethod(JavaNameOccurrence occ)
- Specified by:
isTargetMethod
in classAbstractInefficientZeroCheck
-
appliesToClassName
public boolean appliesToClassName(java.lang.String name)
- Specified by:
appliesToClassName
in classAbstractInefficientZeroCheck
-
visit
public java.lang.Object visit(ASTPrimaryExpression node, java.lang.Object data)
- Specified by:
visit
in interfaceJavaParserVisitor
- Overrides:
visit
in classAbstractJavaRule
-
-