diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/InefficientEmptyStringCheckRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/InefficientEmptyStringCheckRule.java index 4f84d1f058..317b596296 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/InefficientEmptyStringCheckRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/InefficientEmptyStringCheckRule.java @@ -10,20 +10,22 @@ import net.sourceforge.pmd.lang.java.rule.AbstractInefficientZeroCheck; import net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence; /** - * This rule finds code which inefficiently determines empty strings. This code + * This rule finds code which inefficiently determines empty strings. * + *
*
* 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: - *
+ * or + *+ * str.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- * or Apache commons-lang's StringUtils.isBlank
. + * 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 */ diff --git a/pmd-java/src/main/resources/category/java/performance.xml b/pmd-java/src/main/resources/category/java/performance.xml index 3f7f975050..edd4fb27c9 100644 --- a/pmd-java/src/main/resources/category/java/performance.xml +++ b/pmd-java/src/main/resources/category/java/performance.xml @@ -366,18 +366,34 @@ buf.append("1m"); // good class="net.sourceforge.pmd.lang.java.rule.performance.InefficientEmptyStringCheckRule" externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_performance.html#inefficientemptystringcheck">-String.trim().length() is an inefficient way to check if a String is really empty, as it + 3 0) { + if (string != null && string.trim().length() > 0) { doSomething(); } }