From 16ad6904bd2157ef4e5c870b1fc93c1d437c83a3 Mon Sep 17 00:00:00 2001 From: Karl-Philipp Richter Date: Mon, 21 May 2018 22:59:41 +0200 Subject: [PATCH] Adjust InefficientEmptyStringCheck documentation InefficientEmptyStringCheckRule.java and the corresponding section both contain information which the other doesn't. The class Javadoc and the description section in performance.xml has thus been adjusted. --- .../InefficientEmptyStringCheckRule.java | 22 ++++++++++------ .../resources/category/java/performance.xml | 26 +++++++++++++++---- 2 files changed, 35 insertions(+), 13 deletions(-) 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's StringUtils#isBlank
+ * (in commons-lang), Spring's StringUtils#hasText (in the Spring
+ * framework) or Google's CharMatcher#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(); } }