IDEADEV-40452
[fedora-idea.git] / plugins / InspectionGadgets / src / com / siyeh / ig / bugs / StringEqualityInspection.java
blobcd6371699fbe1dbfb22fa007c5c90b43f285be9f
1 /*
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 {
30 @NotNull
31 public String getDisplayName() {
32 return InspectionGadgetsBundle.message(
33 "string.comparison.display.name");
36 @NotNull
37 public String buildErrorString(Object... infos) {
38 return InspectionGadgetsBundle.message(
39 "string.comparison.problem.descriptor");
42 public boolean isEnabledByDefault(){
43 return true;
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)){
60 return;
62 if (!ComparisonUtils.isEqualityComparison(expression)) {
63 return;
65 final PsiExpression lhs = expression.getLOperand();
66 if (!isStringType(lhs)) {
67 return;
69 final PsiExpression rhs = expression.getROperand();
70 if (!isStringType(rhs)) {
71 return;
73 final String lhsText = lhs.getText();
74 if (PsiKeyword.NULL.equals(lhsText)) {
75 return;
77 if (rhs == null) {
78 return;
80 final String rhsText = rhs.getText();
81 if (PsiKeyword.NULL.equals(rhsText)) {
82 return;
84 final PsiJavaToken sign = expression.getOperationSign();
85 registerError(sign);
88 private static boolean isStringType(PsiExpression lhs) {
89 if (lhs == null) {
90 return false;
92 final PsiType lhsType = lhs.getType();
93 if (lhsType == null) {
94 return false;
96 return TypeUtils.isJavaLangString(lhsType);