IDEA-125486 (Comparison using == instead of .equals() should replace with Objects.equals() instead of the instance .equals() method)

This commit is contained in:
Bas Leijdekkers
2014-05-26 11:27:57 +02:00
parent 0cb2b06f64
commit 5859ed9fb8
6 changed files with 87 additions and 11 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2007 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2014 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,7 +22,7 @@ import com.intellij.psi.PsiBinaryExpression;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiExpression;
import com.intellij.psi.tree.IElementType;
import com.intellij.util.IncorrectOperationException;
import com.intellij.psi.util.PsiUtil;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.ig.PsiReplacementUtil;
@@ -31,11 +31,12 @@ import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
public class EqualityToEqualsFix extends InspectionGadgetsFix {
@Override
@NotNull
public String getFamilyName() {
return getName();
}
@Override
@NotNull
public String getFamilyName() {
return getName();
}
@Override
@NotNull
@@ -45,8 +46,7 @@ public class EqualityToEqualsFix extends InspectionGadgetsFix {
}
@Override
public void doFix(Project project, ProblemDescriptor descriptor)
throws IncorrectOperationException {
public void doFix(Project project, ProblemDescriptor descriptor) {
final PsiElement comparisonToken = descriptor.getPsiElement();
final PsiBinaryExpression expression = (PsiBinaryExpression)
comparisonToken.getParent();
@@ -71,7 +71,10 @@ public class EqualityToEqualsFix extends InspectionGadgetsFix {
return;
}
@NonNls final String expString;
if (ParenthesesUtils.getPrecedence(strippedLhs) >
if (PsiUtil.isLanguageLevel7OrHigher(expression)) {
expString = "java.util.Objects.equals(" + strippedLhs.getText() + ',' + strippedRhs.getText() + ')';
}
else if (ParenthesesUtils.getPrecedence(strippedLhs) >
ParenthesesUtils.METHOD_CALL_PRECEDENCE) {
expString = '(' + strippedLhs.getText() + ").equals(" +
strippedRhs.getText() + ')';
@@ -87,6 +90,6 @@ public class EqualityToEqualsFix extends InspectionGadgetsFix {
else {
newExpression = expString;
}
PsiReplacementUtil.replaceExpression(expression, newExpression);
PsiReplacementUtil.replaceExpressionAndShorten(expression, newExpression);
}
}
@@ -0,0 +1,8 @@
import java.util.Objects;
class Negated {
boolean two(Object o1, Object o2) {
return !Objects.equals(o1, o2);
}
}
@@ -0,0 +1,6 @@
class Negated {
boolean two(Object o1, Object o2) {
return o1 <caret>!= o2;
}
}
@@ -0,0 +1,8 @@
import java.util.Objects;
class Simple {
boolean one(Object o1, Object o2) {
return Objects.equals(o1, o2);
}
}
@@ -0,0 +1,6 @@
class Simple {
boolean one(Object o1, Object o2) {
return o1 ==<caret> o2;
}
}
@@ -0,0 +1,45 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.fixes.bugs;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.IGQuickFixesTestCase;
import com.siyeh.ig.bugs.ObjectEqualityInspection;
/**
* @author Bas Leijdekkers
*/
public class EqualityToEqualsFixTest extends IGQuickFixesTestCase {
public void testSimple() { doTest(); }
public void testNegated() { doTest(); }
@Override
protected void setUp() throws Exception {
super.setUp();
myFixture.enableInspections(new ObjectEqualityInspection());
myDefaultHint = InspectionGadgetsBundle.message("object.comparison.replace.quickfix");
myRelativePath = "bugs/equality_to_equals";
myFixture.addClass(
"package java.util;" +
"public class Objects {" +
" public static boolean equals(Object a, Object b) {\n" +
" return (a == b) || (a != null && a.equals(b));\n" +
" }" +
"}"
);
}
}