add parentheses when necessary in quickfix for 'StringBuffer' can be replaced with...
authorBas Leijdekkers <basleijdekkers@gmail.com>
Tue, 31 Jan 2012 19:38:30 +0000 (20:38 +0100)
committerBas Leijdekkers <basleijdekkers@gmail.com>
Tue, 31 Jan 2012 19:38:30 +0000 (20:38 +0100)
plugins/InspectionGadgets/src/com/siyeh/ig/style/StringBufferReplaceableByStringInspection.java
plugins/InspectionGadgets/test/com/siyeh/igfixes/style/replace_with_string/Precedence.after.java [new file with mode: 0644]
plugins/InspectionGadgets/test/com/siyeh/igfixes/style/replace_with_string/Precedence.java [new file with mode: 0644]
plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/style/StringBufferReplaceableWithStringFixTest.java

index a7e13174cc6f99b9923132eed6861d5d07e00fac..ce539165b14023aeaea5581c661cebd685bdf8f2 100644 (file)
@@ -24,6 +24,7 @@ import com.siyeh.InspectionGadgetsBundle;
 import com.siyeh.ig.BaseInspection;
 import com.siyeh.ig.BaseInspectionVisitor;
 import com.siyeh.ig.InspectionGadgetsFix;
+import com.siyeh.ig.psiutils.ParenthesesUtils;
 import com.siyeh.ig.psiutils.TypeUtils;
 import com.siyeh.ig.psiutils.VariableAccessUtils;
 import org.jetbrains.annotations.NotNull;
@@ -141,7 +142,13 @@ public class StringBufferReplaceableByStringInspection extends BaseInspection {
           }
           final PsiExpression argument = arguments[0];
           if (result.length() != 0) {
-            result.append('+').append(argument.getText());
+            result.append('+');
+            if (ParenthesesUtils.getPrecedence(argument) > ParenthesesUtils.ADDITIVE_PRECEDENCE) {
+              result.append('(').append(argument.getText()).append(')');
+            }
+            else {
+              result.append(argument.getText());
+            }
           }
           else {
             final PsiType type = argument.getType();
@@ -149,7 +156,12 @@ public class StringBufferReplaceableByStringInspection extends BaseInspection {
               result.append("String.valueOf(").append(argument.getText()).append(")");
             }
             else {
-              result.append(argument.getText());
+              if (ParenthesesUtils.getPrecedence(argument) > ParenthesesUtils.ADDITIVE_PRECEDENCE) {
+                result.append('(').append(argument.getText()).append(')');
+              }
+              else {
+                result.append(argument.getText());
+              }
             }
           }
         }
diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/style/replace_with_string/Precedence.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/style/replace_with_string/Precedence.after.java
new file mode 100644 (file)
index 0000000..5b82285
--- /dev/null
@@ -0,0 +1,9 @@
+package com.siyeh.igfixes.style.replace_with_string;
+
+import java.awt.*;
+
+class Precedence {
+  public static String toRgbColor( final Color color) {
+    return "rgba(" + color.getRed() + ',' + color.getGreen() + ',' + color.getBlue() + ',' + (color.getAlpha() == 0 ? '0' : String.format("0.%2d", (int) (color.getAlpha() / 255.0 * 100))) + ')' + 9 * 9;
+  }
+}
\ No newline at end of file
diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/style/replace_with_string/Precedence.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/style/replace_with_string/Precedence.java
new file mode 100644 (file)
index 0000000..bc35808
--- /dev/null
@@ -0,0 +1,12 @@
+package com.siyeh.igfixes.style.replace_with_string;
+
+import java.awt.*;
+
+class Precedence {
+  public static String toRgbColor( final Color color) {
+    return new StringB<caret>uilder("rgba(").append(color.getRed()).append(',')
+      .append(color.getGreen()).append(',').append(color.getBlue())
+      .append(',').append(color.getAlpha() == 0 ? '0' : String.format("0.%2d", (int) (color.getAlpha() / 255.0 * 100)))
+      .append(')').append(9*9).toString();
+  }
+}
\ No newline at end of file
index 0d03db24fa176bd5fc053e27641407aae8645653..1db03b736071a603ebe3fbd52719130609e5d01d 100644 (file)
@@ -33,4 +33,5 @@ public class StringBufferReplaceableWithStringFixTest extends IGQuickFixesTestCa
   public void testStringBuilderAppend() { doTest("StringBuilderAppend", InspectionGadgetsBundle.message("string.builder.replaceable.by.string.quickfix")); }
   public void testStringBufferVariable() { doTest(); }
   public void testStartsWithPrimitive() { doTest(); }
+  public void testPrecedence() { doTest("Precedence", InspectionGadgetsBundle.message("string.builder.replaceable.by.string.quickfix")); }
 }