2 * Copyright 2003-2007 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
.bugs
;
18 import com
.intellij
.psi
.*;
19 import com
.siyeh
.InspectionGadgetsBundle
;
20 import com
.siyeh
.ig
.BaseInspection
;
21 import com
.siyeh
.ig
.BaseInspectionVisitor
;
22 import com
.siyeh
.ig
.InspectionGadgetsFix
;
23 import com
.siyeh
.ig
.fixes
.EqualityToEqualsFix
;
24 import com
.siyeh
.ig
.psiutils
.ComparisonUtils
;
25 import com
.siyeh
.ig
.psiutils
.TypeUtils
;
26 import org
.jetbrains
.annotations
.NotNull
;
28 public class StringEqualityInspection
extends BaseInspection
{
31 public String
getDisplayName() {
32 return InspectionGadgetsBundle
.message(
33 "string.comparison.display.name");
37 public String
buildErrorString(Object
... infos
) {
38 return InspectionGadgetsBundle
.message(
39 "string.comparison.problem.descriptor");
42 public boolean isEnabledByDefault(){
46 public BaseInspectionVisitor
buildVisitor() {
47 return new ObjectEqualityVisitor();
50 public InspectionGadgetsFix
buildFix(Object
... infos
) {
51 return new EqualityToEqualsFix();
54 private static class ObjectEqualityVisitor
extends BaseInspectionVisitor
{
56 @Override public void visitBinaryExpression(
57 @NotNull PsiBinaryExpression expression
) {
58 super.visitBinaryExpression(expression
);
59 if(!(expression
.getROperand() != null)){
62 if (!ComparisonUtils
.isEqualityComparison(expression
)) {
65 final PsiExpression lhs
= expression
.getLOperand();
66 if (!isStringType(lhs
)) {
69 final PsiExpression rhs
= expression
.getROperand();
70 if (!isStringType(rhs
)) {
73 final String lhsText
= lhs
.getText();
74 if (PsiKeyword
.NULL
.equals(lhsText
)) {
80 final String rhsText
= rhs
.getText();
81 if (PsiKeyword
.NULL
.equals(rhsText
)) {
84 final PsiJavaToken sign
= expression
.getOperationSign();
88 private static boolean isStringType(PsiExpression lhs
) {
92 final PsiType lhsType
= lhs
.getType();
93 if (lhsType
== null) {
96 return TypeUtils
.isJavaLangString(lhsType
);