improve quickfix for "'StringBuffer' can be replaced with 'String'" inspection and...
authorBas Leijdekkers <basleijdekkers@gmail.com>
Mon, 30 Jan 2012 09:36:10 +0000 (10:36 +0100)
committerBas Leijdekkers <basleijdekkers@gmail.com>
Mon, 30 Jan 2012 09:36:10 +0000 (10:36 +0100)
plugins/InspectionGadgets/src/com/siyeh/ig/style/StringBufferReplaceableByStringInspection.java
plugins/InspectionGadgets/test/com/siyeh/igfixes/style/replace_with_string/SimpleStringBuffer.after.java [new file with mode: 0644]
plugins/InspectionGadgets/test/com/siyeh/igfixes/style/replace_with_string/SimpleStringBuffer.java [new file with mode: 0644]
plugins/InspectionGadgets/test/com/siyeh/igfixes/style/replace_with_string/StartsWithPrimitive.after.java [new file with mode: 0644]
plugins/InspectionGadgets/test/com/siyeh/igfixes/style/replace_with_string/StartsWithPrimitive.java [new file with mode: 0644]
plugins/InspectionGadgets/test/com/siyeh/igfixes/style/replace_with_string/StringBufferVariable.after.java [new file with mode: 0644]
plugins/InspectionGadgets/test/com/siyeh/igfixes/style/replace_with_string/StringBufferVariable.java [new file with mode: 0644]
plugins/InspectionGadgets/test/com/siyeh/igfixes/style/replace_with_string/StringBuilderAppend.after.java [new file with mode: 0644]
plugins/InspectionGadgets/test/com/siyeh/igfixes/style/replace_with_string/StringBuilderAppend.java [new file with mode: 0644]
plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/style/StringBufferReplaceableWithStringFixTest.java [new file with mode: 0644]

index 38eed03f36cd78c5fba3ecb229e206bc480c6f14..a7e13174cc6f99b9923132eed6861d5d07e00fac 100644 (file)
@@ -66,7 +66,8 @@ public class StringBufferReplaceableByStringInspection extends BaseInspection {
     public String getName() {
       if (isStringBuilder) {
         return InspectionGadgetsBundle.message("string.builder.replaceable.by.string.quickfix");
-      } else {
+      }
+      else {
         return InspectionGadgetsBundle.message("string.buffer.replaceable.by.string.quickfix");
       }
     }
@@ -110,20 +111,16 @@ public class StringBufferReplaceableByStringInspection extends BaseInspection {
         final PsiNewExpression newExpression = (PsiNewExpression)initializer;
         final PsiExpressionList argumentList = newExpression.getArgumentList();
         final PsiExpression[] arguments = argumentList.getExpressions();
-        if (arguments.length == 0) {
-          result.append("\"\"");
-        } else if (arguments.length == 1) {
+        if (arguments.length == 1) {
           final PsiExpression argument = arguments[0];
           final PsiType type = argument.getType();
-          if (PsiType.INT.equals(type)) {
-            result.append("\"\"");
-          } else {
+          if (!PsiType.INT.equals(type)) {
             result.append(argument.getText());
           }
-        } else {
-          return null;
         }
-      } else if (initializer instanceof PsiMethodCallExpression) {
+        return result;
+      }
+      else if (initializer instanceof PsiMethodCallExpression) {
         final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)initializer;
         final PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression();
         final PsiExpression qualifier = methodExpression.getQualifierExpression();
@@ -131,20 +128,34 @@ public class StringBufferReplaceableByStringInspection extends BaseInspection {
         if (result == null) {
           return null;
         }
-        if (!"toString".equals(methodExpression.getReferenceName())) {
+        if ("toString".equals(methodExpression.getReferenceName())) {
+          if (result.length() == 0) {
+            result.append("\"\"");
+          }
+        }
+        else {
           final PsiExpressionList argumentList = methodCallExpression.getArgumentList();
           final PsiExpression[] arguments = argumentList.getExpressions();
           if (arguments.length != 1) {
             return null;
           }
           final PsiExpression argument = arguments[0];
-          result.append('+');
-          result.append(argument.getText());
+          if (result.length() != 0) {
+            result.append('+').append(argument.getText());
+          }
+          else {
+            final PsiType type = argument.getType();
+            if (type instanceof PsiPrimitiveType) {
+              result.append("String.valueOf(").append(argument.getText()).append(")");
+            }
+            else {
+              result.append(argument.getText());
+            }
+          }
         }
-      } else {
-        return null;
+        return result;
       }
-      return result;
+      return null;
     }
   }
 
@@ -261,7 +272,7 @@ public class StringBufferReplaceableByStringInspection extends BaseInspection {
       }
       completeExpression = grandParent;
       if (found) {
-        return (PsiExpression) completeExpression;
+        return (PsiExpression)completeExpression;
       }
     }
     return null;
diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/style/replace_with_string/SimpleStringBuffer.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/style/replace_with_string/SimpleStringBuffer.after.java
new file mode 100644 (file)
index 0000000..f948a96
--- /dev/null
@@ -0,0 +1,7 @@
+package com.siyeh.igfixes.style.replace_with_string;
+
+public class SimpleStringBuffer {
+  String foo() {
+    return "";
+  }
+}
diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/style/replace_with_string/SimpleStringBuffer.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/style/replace_with_string/SimpleStringBuffer.java
new file mode 100644 (file)
index 0000000..77902d4
--- /dev/null
@@ -0,0 +1,7 @@
+package com.siyeh.igfixes.style.replace_with_string;
+
+public class SimpleStringBuffer {
+  String foo() {
+    return new <caret>StringBuffer().toString();
+  }
+}
diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/style/replace_with_string/StartsWithPrimitive.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/style/replace_with_string/StartsWithPrimitive.after.java
new file mode 100644 (file)
index 0000000..89721d3
--- /dev/null
@@ -0,0 +1,6 @@
+class StartsWithPrimitive {
+
+  String foo(int i) {
+    return String.valueOf(i);
+  }
+}
\ No newline at end of file
diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/style/replace_with_string/StartsWithPrimitive.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/style/replace_with_string/StartsWithPrimitive.java
new file mode 100644 (file)
index 0000000..c0f4d87
--- /dev/null
@@ -0,0 +1,6 @@
+class StartsWithPrimitive {
+
+  String foo(int i) {
+    return new Stri<caret>ngBuffer().append(i).toString();
+  }
+}
\ No newline at end of file
diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/style/replace_with_string/StringBufferVariable.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/style/replace_with_string/StringBufferVariable.after.java
new file mode 100644 (file)
index 0000000..9f7b9e2
--- /dev/null
@@ -0,0 +1,6 @@
+class StringBufferVariable {
+  void foo() {
+    String sb = "asdf" + "asdf";
+    System.out.println(sb.toString());
+  }
+}
\ No newline at end of file
diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/style/replace_with_string/StringBufferVariable.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/style/replace_with_string/StringBufferVariable.java
new file mode 100644 (file)
index 0000000..27b16ee
--- /dev/null
@@ -0,0 +1,6 @@
+class StringBufferVariable {
+  void foo() {
+    StringBuffer <caret>sb = new StringBuffer("asdf").append("asdf");
+    System.out.println(sb.toString());
+  }
+}
\ No newline at end of file
diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/style/replace_with_string/StringBuilderAppend.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/style/replace_with_string/StringBuilderAppend.after.java
new file mode 100644 (file)
index 0000000..8282bb2
--- /dev/null
@@ -0,0 +1,5 @@
+class StringBuilderAppend {
+  String foo(int i) {
+    return "test: " + i;
+  }
+}
\ No newline at end of file
diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/style/replace_with_string/StringBuilderAppend.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/style/replace_with_string/StringBuilderAppend.java
new file mode 100644 (file)
index 0000000..ff584cf
--- /dev/null
@@ -0,0 +1,5 @@
+class StringBuilderAppend {
+  String foo(int i) {
+    return new Strin<caret>gBuilder().append("test: ").append(i).toString();
+  }
+}
\ No newline at end of file
diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/style/StringBufferReplaceableWithStringFixTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/style/StringBufferReplaceableWithStringFixTest.java
new file mode 100644 (file)
index 0000000..0d03db2
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2012 Bas Leijdekkers
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.siyeh.ig.fixes.style;
+
+import com.siyeh.InspectionGadgetsBundle;
+import com.siyeh.ig.IGQuickFixesTestCase;
+import com.siyeh.ig.style.StringBufferReplaceableByStringInspection;
+
+public class StringBufferReplaceableWithStringFixTest extends IGQuickFixesTestCase {
+
+  @Override
+  public void setUp() throws Exception {
+    super.setUp();
+    myFixture.enableInspections(new StringBufferReplaceableByStringInspection());
+    myRelativePath = "style/replace_with_string";
+    myDefaultHint = InspectionGadgetsBundle.message("string.buffer.replaceable.by.string.quickfix");
+  }
+
+  public void testSimpleStringBuffer() { doTest(); }
+  public void testStringBuilderAppend() { doTest("StringBuilderAppend", InspectionGadgetsBundle.message("string.builder.replaceable.by.string.quickfix")); }
+  public void testStringBufferVariable() { doTest(); }
+  public void testStartsWithPrimitive() { doTest(); }
+}