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
.fixes
;
18 import com
.intellij
.codeInspection
.ProblemDescriptor
;
19 import com
.intellij
.openapi
.project
.Project
;
20 import com
.intellij
.psi
.*;
21 import com
.intellij
.psi
.tree
.IElementType
;
22 import com
.intellij
.util
.IncorrectOperationException
;
23 import com
.siyeh
.InspectionGadgetsBundle
;
24 import com
.siyeh
.ig
.InspectionGadgetsFix
;
25 import com
.siyeh
.ig
.psiutils
.ParenthesesUtils
;
26 import org
.jetbrains
.annotations
.NonNls
;
27 import org
.jetbrains
.annotations
.NotNull
;
29 public class EqualityToEqualsFix
extends InspectionGadgetsFix
{
32 public String
getName() {
33 return InspectionGadgetsBundle
.message(
34 "object.comparison.replace.quickfix");
37 public void doFix(Project project
, ProblemDescriptor descriptor
)
38 throws IncorrectOperationException
{
39 final PsiElement comparisonToken
= descriptor
.getPsiElement();
40 final PsiBinaryExpression expression
= (PsiBinaryExpression
)
41 comparisonToken
.getParent();
42 if (expression
== null) {
45 boolean negated
=false;
46 final PsiJavaToken sign
= expression
.getOperationSign();
47 final IElementType tokenType
= sign
.getTokenType();
48 if (JavaTokenType
.NE
.equals(tokenType
)) {
51 final PsiExpression lhs
= expression
.getLOperand();
52 final PsiExpression strippedLhs
=
53 ParenthesesUtils
.stripParentheses(lhs
);
54 if (strippedLhs
== null) {
57 final PsiExpression rhs
= expression
.getROperand();
58 final PsiExpression strippedRhs
=
59 ParenthesesUtils
.stripParentheses(rhs
);
60 if (strippedRhs
== null) {
63 @NonNls final String expString
;
64 if (ParenthesesUtils
.getPrecedence(strippedLhs
) >
65 ParenthesesUtils
.METHOD_CALL_PRECEDENCE
) {
66 expString
= '(' + strippedLhs
.getText() + ").equals(" +
67 strippedRhs
.getText() + ')';
69 expString
= strippedLhs
.getText() + ".equals(" +
70 strippedRhs
.getText() + ')';
72 @NonNls final String newExpression
;
74 newExpression
= '!' + expString
;
76 newExpression
= expString
;
78 replaceExpression(expression
, newExpression
);