From 623dc5cab27f4da116072cd6eb20de362f1731e4 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Fri, 15 May 2015 16:07:12 +0200 Subject: [PATCH] IG: teach "Non-constant String should be StringBuilder" inspection about polyadic expressions --- .../StringVariableIsAppendedToVisitor.java | 17 ++++---- ...ngReplaceableByStringBufferInspection.java | 23 ----------- .../StringReplaceableByStringBuffer.java | 41 +++++++++++++++++++ ...placeableByStringBufferInspectionTest.java | 36 ++++++++++++++++ 4 files changed, 85 insertions(+), 32 deletions(-) delete mode 100644 plugins/InspectionGadgets/test/com/siyeh/igtest/performance/StringReplaceableByStringBufferInspection.java create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igtest/performance/string_replaceable_by_string_buffer/StringReplaceableByStringBuffer.java create mode 100644 plugins/InspectionGadgets/testsrc/com/siyeh/ig/performance/StringReplaceableByStringBufferInspectionTest.java diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/StringVariableIsAppendedToVisitor.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/StringVariableIsAppendedToVisitor.java index 8899ac7a71e5..c61220fc9c15 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/StringVariableIsAppendedToVisitor.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/StringVariableIsAppendedToVisitor.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2008 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2015 Dave Griffith, Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -81,16 +81,15 @@ class StringVariableIsAppendedToVisitor extends JavaRecursiveElementVisitor { return variable.equals(referent); } if (expression instanceof PsiParenthesizedExpression) { - final PsiExpression body = - ((PsiParenthesizedExpression)expression).getExpression(); + final PsiParenthesizedExpression parenthesizedExpression = (PsiParenthesizedExpression)expression; + final PsiExpression body = parenthesizedExpression.getExpression(); return isConcatenation(body); } - if (expression instanceof PsiBinaryExpression) { - final PsiBinaryExpression binaryExpression = - (PsiBinaryExpression)expression; - final PsiExpression lhs = binaryExpression.getLOperand(); - final PsiExpression rhs = binaryExpression.getROperand(); - return isConcatenation(lhs) || isConcatenation(rhs); + if (expression instanceof PsiPolyadicExpression) { + final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression)expression; + for (PsiExpression operand : polyadicExpression.getOperands()) { + if (isConcatenation(operand)) return true; + } } return false; } diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/performance/StringReplaceableByStringBufferInspection.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/performance/StringReplaceableByStringBufferInspection.java deleted file mode 100644 index 6a2e7fab5f5b..000000000000 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/performance/StringReplaceableByStringBufferInspection.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.siyeh.igtest.performance; - -public class StringReplaceableByStringBufferInspection { - public void foo() - { - String buffer = "bar"; - buffer += "foo"; - System.out.println(buffer); - } - - public void foobar() - { - String buffer = "bar"; - buffer = buffer + "foo"; - System.out.println(buffer); - } - - public void foobaz() - { - final String buffer = "bar"; - System.out.println(buffer); - } -} diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/performance/string_replaceable_by_string_buffer/StringReplaceableByStringBuffer.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/performance/string_replaceable_by_string_buffer/StringReplaceableByStringBuffer.java new file mode 100644 index 000000000000..90ec88e53a09 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/performance/string_replaceable_by_string_buffer/StringReplaceableByStringBuffer.java @@ -0,0 +1,41 @@ +package com.siyeh.igtest.performance.string_replaceable_by_string_buffer; + +public class StringReplaceableByStringBuffer { + public void foo() + { + String buffer = "bar"; + buffer += "foo"; + System.out.println(buffer); + } + + public void foo2() + { + String buffer = "bar"; + for (int i = 0; i < 10; i++) { + buffer += "foo"; + } + System.out.println(buffer); + } + + public void foobar() + { + String buffer = "bar"; + buffer = buffer + "foo" + buffer; + System.out.println(buffer); + } + + public void foobar2() + { + String buffer = "bar"; + for (int i = 0; i < 10; i++) { + buffer = buffer + "foo" + buffer; + } + System.out.println(buffer); + } + + public void foobaz() + { + final String buffer = "bar"; + System.out.println(buffer); + } +} diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/performance/StringReplaceableByStringBufferInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/performance/StringReplaceableByStringBufferInspectionTest.java new file mode 100644 index 000000000000..411a00a6557a --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/performance/StringReplaceableByStringBufferInspectionTest.java @@ -0,0 +1,36 @@ +/* + * Copyright 2000-2015 JetBrains s.r.o. + * + * 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.performance; + +import com.intellij.codeInspection.InspectionProfileEntry; +import com.siyeh.ig.LightInspectionTestCase; +import org.jetbrains.annotations.Nullable; + +/** + * @author Bas Leijdekkers + */ +public class StringReplaceableByStringBufferInspectionTest extends LightInspectionTestCase { + + public void testStringReplaceableByStringBuffer() { + doTest(); + } + + @Nullable + @Override + protected InspectionProfileEntry getInspection() { + return new StringReplaceableByStringBufferInspection(); + } +} \ No newline at end of file