2 * Copyright 2007-2008 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
.bugs
;
18 import com
.intellij
.codeInspection
.ProblemDescriptor
;
19 import com
.intellij
.openapi
.project
.Project
;
20 import com
.intellij
.psi
.*;
21 import com
.intellij
.util
.IncorrectOperationException
;
22 import com
.siyeh
.InspectionGadgetsBundle
;
23 import com
.siyeh
.ig
.BaseInspection
;
24 import com
.siyeh
.ig
.BaseInspectionVisitor
;
25 import com
.siyeh
.ig
.InspectionGadgetsFix
;
26 import com
.siyeh
.ig
.psiutils
.ClassUtils
;
27 import org
.jetbrains
.annotations
.Nls
;
28 import org
.jetbrains
.annotations
.NotNull
;
29 import org
.jetbrains
.annotations
.Nullable
;
31 public class NewStringBufferWithCharArgumentInspection
extends BaseInspection
{
35 public String
getDisplayName() {
36 return InspectionGadgetsBundle
.message(
37 "new.string.buffer.with.char.argument.display.name");
41 protected String
buildErrorString(Object
... infos
) {
42 return InspectionGadgetsBundle
.message(
43 "new.string.buffer.with.char.argument.problem.descriptor");
46 public boolean isEnabledByDefault() {
51 protected InspectionGadgetsFix
buildFix(Object
... infos
) {
52 final PsiExpression argument
= (PsiExpression
) infos
[0];
53 if (!(argument
instanceof PsiLiteralExpression
)) {
56 return new NewStringBufferWithCharArgumentFix();
59 private static class NewStringBufferWithCharArgumentFix
60 extends InspectionGadgetsFix
{
63 public String
getName() {
64 return InspectionGadgetsBundle
.message(
65 "new.string.buffer.with.char.argument.quickfix");
68 protected void doFix(Project project
, ProblemDescriptor descriptor
)
69 throws IncorrectOperationException
{
70 final PsiElement element
= descriptor
.getPsiElement();
71 final PsiNewExpression newExpression
=
72 (PsiNewExpression
) element
.getParent();
73 final PsiExpressionList argumentList
=
74 newExpression
.getArgumentList();
75 if (argumentList
== null) {
78 final PsiExpression
[] arguments
= argumentList
.getExpressions();
79 if (arguments
.length
!= 1) {
82 final PsiExpression argument
= arguments
[0];
83 final String text
= argument
.getText();
84 final String newArgument
=
85 '"' + text
.substring(1, text
.length() - 1) + '"';
86 replaceExpression(argument
, newArgument
);
90 public BaseInspectionVisitor
buildVisitor() {
91 return new StringBufferWithCharArgumentVisitor();
94 private static class StringBufferWithCharArgumentVisitor
95 extends BaseInspectionVisitor
{
97 @Override public void visitNewExpression(PsiNewExpression expression
) {
98 super.visitNewExpression(expression
);
99 final PsiExpressionList argumentList
= expression
.getArgumentList();
100 if (argumentList
== null) {
103 final PsiExpression
[] arguments
= argumentList
.getExpressions();
104 if (arguments
.length
!= 1) {
107 final PsiExpression argument
= arguments
[0];
108 final PsiType type
= argument
.getType();
109 if (!PsiType
.CHAR
.equals(type
)) {
112 final PsiMethod constructor
= expression
.resolveConstructor();
113 if (constructor
== null) {
116 final PsiClass aClass
= constructor
.getContainingClass();
117 if (!ClassUtils
.isSubclass(aClass
,
118 "java.lang.AbstractStringBuilder")) {
121 registerNewExpressionError(expression
, argument
);