2 * Copyright 2003-2008 Dave Griffith, Bas Leijdekkers
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com
.siyeh
.ig
.performance
;
18 import com
.intellij
.psi
.*;
19 import com
.intellij
.psi
.util
.PsiTreeUtil
;
20 import com
.siyeh
.InspectionGadgetsBundle
;
21 import com
.siyeh
.ig
.BaseInspection
;
22 import com
.siyeh
.ig
.BaseInspectionVisitor
;
23 import com
.siyeh
.ig
.psiutils
.TypeUtils
;
24 import com
.siyeh
.ig
.ui
.SingleCheckboxOptionsPanel
;
25 import org
.jetbrains
.annotations
.NotNull
;
29 public class StringReplaceableByStringBufferInspection
extends BaseInspection
{
31 @SuppressWarnings({"PublicField"})
32 public boolean onlyWarnOnLoop
= true;
35 public String
getID() {
36 return "NonConstantStringShouldBeStringBuffer";
40 public String
getDisplayName() {
41 return InspectionGadgetsBundle
.message(
42 "string.replaceable.by.string.buffer.display.name");
46 protected String
buildErrorString(Object
... infos
) {
47 return InspectionGadgetsBundle
.message(
48 "string.replaceable.by.string.buffer.problem.descriptor");
52 public JComponent
createOptionsPanel() {
53 return new SingleCheckboxOptionsPanel(InspectionGadgetsBundle
.message(
54 "string.replaceable.by.string.buffer.in.loop.option"),
55 this, "onlyWarnOnLoop");
58 public BaseInspectionVisitor
buildVisitor() {
59 return new StringReplaceableByStringBufferVisitor();
62 private class StringReplaceableByStringBufferVisitor
63 extends BaseInspectionVisitor
{
65 @Override public void visitLocalVariable(
66 @NotNull PsiLocalVariable variable
) {
67 super.visitLocalVariable(variable
);
68 final PsiCodeBlock codeBlock
=
69 PsiTreeUtil
.getParentOfType(variable
, PsiCodeBlock
.class);
70 if (codeBlock
== null) {
73 final PsiType type
= variable
.getType();
74 if (!TypeUtils
.typeEquals("java.lang.String", type
)) {
77 if (!variableIsAppendedTo(variable
, codeBlock
)) {
80 registerVariableError(variable
);
83 public boolean variableIsAppendedTo(PsiVariable variable
,
85 final StringVariableIsAppendedToVisitor visitor
=
86 new StringVariableIsAppendedToVisitor(variable
,
88 context
.accept(visitor
);
89 return visitor
.isAppendedTo();