From fc9df600f55dd6539acf73151c1c491260b117c9 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Sat, 30 Aug 2014 13:00:46 +0200 Subject: [PATCH] better intention name "Replace '==' with 'equals()'" and test --- .../siyeh/IntentionPowerPackBundle.properties | 3 +- .../ipp/equality/ObjectEqualityPredicate.java | 36 ++-------------- .../ReplaceEqualityWithEqualsIntention.java | 31 ++++++++------ .../EnumComparison.java | 8 ++++ .../NegatedObjectComparison.java | 6 +++ .../NegatedObjectComparison_after.java | 6 +++ .../NullComparison.java | 6 +++ .../PrimitiveComparison.java | 6 +++ .../SimpleObjectComparison.java | 6 +++ .../SimpleObjectComparison_after.java | 6 +++ ...eplaceEqualityWithEqualsIntentionTest.java | 42 +++++++++++++++++++ 11 files changed, 110 insertions(+), 46 deletions(-) create mode 100644 plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_equals/EnumComparison.java create mode 100644 plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_equals/NegatedObjectComparison.java create mode 100644 plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_equals/NegatedObjectComparison_after.java create mode 100644 plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_equals/NullComparison.java create mode 100644 plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_equals/PrimitiveComparison.java create mode 100644 plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_equals/SimpleObjectComparison.java create mode 100644 plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_equals/SimpleObjectComparison_after.java create mode 100644 plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/equality/ReplaceEqualityWithEqualsIntentionTest.java diff --git a/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties b/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties index 952afa890e96..25670bd1299a 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties +++ b/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties @@ -81,7 +81,8 @@ char.to.string.intention.name=Replace character literal with string char.to.string.intention.family.name=Replace Char with String replace.if.with.conditional.intention.name=Replace 'if else' with '?:' replace.if.with.conditional.intention.family.name=Replace If Else with Conditional -replace.equality.with.equals.intention.name=Replace '==' with '.equals()' +replace.equality.with.equals.intention.name=Replace '==' with 'equals()' +replace.equality.with.not.equals.intention.name=Replace '!=' with '!equals()' replace.equality.with.equals.intention.family.name=Replace Equality with Equals make.call.chain.into.call.sequence.intention.name=Make method call chain into call sequence make.call.chain.into.call.sequence.intention.family.name=Make Call Chain Into Call Sequence diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/equality/ObjectEqualityPredicate.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/equality/ObjectEqualityPredicate.java index 18d55c35fe97..4bea8688ae32 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/equality/ObjectEqualityPredicate.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/equality/ObjectEqualityPredicate.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2005 Dave Griffith + * 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. @@ -19,7 +19,6 @@ import com.intellij.psi.*; import com.intellij.psi.tree.IElementType; import com.intellij.psi.util.TypeConversionUtil; import com.siyeh.ipp.base.PsiElementPredicate; -import com.siyeh.ipp.psiutils.ErrorUtil; class ObjectEqualityPredicate implements PsiElementPredicate { @@ -34,44 +33,15 @@ class ObjectEqualityPredicate implements PsiElementPredicate { return false; } final PsiExpression lhs = expression.getLOperand(); - final String lhsText = lhs.getText(); - if (PsiKeyword.NULL.equals(lhsText)) { - return false; - } final PsiType lhsType = lhs.getType(); - if (lhsType == null) { + if (lhsType == null || lhsType instanceof PsiPrimitiveType || TypeConversionUtil.isEnumType(lhsType)) { return false; } final PsiExpression rhs = expression.getROperand(); if (rhs == null) { return false; } - final String rhsText = rhs.getText(); - if (PsiKeyword.NULL.equals(rhsText)) { - return false; - } final PsiType rhsType = rhs.getType(); - if (rhsType == null) { - return false; - } - if (TypeConversionUtil.isPrimitiveAndNotNull(lhsType) || - TypeConversionUtil.isPrimitiveAndNotNull(rhsType)) { - return false; - } - if (rhsType instanceof PsiClassType) { - final PsiClassType rhsClassType = (PsiClassType)rhsType; - final PsiClass rhsClass = rhsClassType.resolve(); - if (rhsClass != null && rhsClass.isEnum()) { - return false; - } - } - if (lhsType instanceof PsiClassType) { - final PsiClassType lhsClassType = (PsiClassType)lhsType; - final PsiClass lhsClass = lhsClassType.resolve(); - if (lhsClass != null && lhsClass.isEnum()) { - return false; - } - } - return !ErrorUtil.containsError(element); + return !(rhsType == null || rhsType instanceof PsiPrimitiveType || TypeConversionUtil.isEnumType(rhsType)); } } \ No newline at end of file diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/equality/ReplaceEqualityWithEqualsIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/equality/ReplaceEqualityWithEqualsIntention.java index 9c0119a92797..d0f56b8cc305 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/equality/ReplaceEqualityWithEqualsIntention.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/equality/ReplaceEqualityWithEqualsIntention.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2013 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. @@ -15,30 +15,37 @@ */ package com.siyeh.ipp.equality; -import com.intellij.psi.JavaTokenType; -import com.intellij.psi.PsiBinaryExpression; -import com.intellij.psi.PsiElement; -import com.intellij.psi.PsiExpression; +import com.intellij.psi.*; import com.intellij.psi.tree.IElementType; -import com.intellij.util.IncorrectOperationException; +import com.siyeh.IntentionPowerPackBundle; import com.siyeh.ig.PsiReplacementUtil; import com.siyeh.ig.psiutils.ParenthesesUtils; -import com.siyeh.ipp.base.Intention; +import com.siyeh.ipp.base.MutablyNamedIntention; import com.siyeh.ipp.base.PsiElementPredicate; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; -public class ReplaceEqualityWithEqualsIntention extends Intention { +public class ReplaceEqualityWithEqualsIntention extends MutablyNamedIntention { + + @Override + protected String getTextForElement(PsiElement element) { + final PsiBinaryExpression binaryExpression = (PsiBinaryExpression)element; + final IElementType tokenType = binaryExpression.getOperationTokenType(); + if (JavaTokenType.NE.equals(tokenType)) { + return IntentionPowerPackBundle.message("replace.equality.with.not.equals.intention.name"); + } + else { + return IntentionPowerPackBundle.message("replace.equality.with.equals.intention.name"); + } + } @NotNull public PsiElementPredicate getElementPredicate() { return new ObjectEqualityPredicate(); } - public void processIntention(PsiElement element) - throws IncorrectOperationException { - final PsiBinaryExpression exp = - (PsiBinaryExpression)element; + public void processIntention(PsiElement element) { + final PsiBinaryExpression exp = (PsiBinaryExpression)element; final PsiExpression lhs = exp.getLOperand(); final PsiExpression rhs = exp.getROperand(); if (rhs == null) { diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_equals/EnumComparison.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_equals/EnumComparison.java new file mode 100644 index 000000000000..bf1f08c198c5 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_equals/EnumComparison.java @@ -0,0 +1,8 @@ +public class EnumComparison { + + enum E { A, B } + + boolean a(E a, E b) { + return a == b; + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_equals/NegatedObjectComparison.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_equals/NegatedObjectComparison.java new file mode 100644 index 000000000000..863fa48d0c78 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_equals/NegatedObjectComparison.java @@ -0,0 +1,6 @@ +public class NegatedObjectComparison { + + boolean a(Object a, Object b) { + return a != b; + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_equals/NegatedObjectComparison_after.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_equals/NegatedObjectComparison_after.java new file mode 100644 index 000000000000..72eda7377647 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_equals/NegatedObjectComparison_after.java @@ -0,0 +1,6 @@ +public class NegatedObjectComparison { + + boolean a(Object a, Object b) { + return !a.equals(b); + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_equals/NullComparison.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_equals/NullComparison.java new file mode 100644 index 000000000000..559ec3aec144 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_equals/NullComparison.java @@ -0,0 +1,6 @@ +public class NullComparison { + + boolean a(Object a) { + return a == null; + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_equals/PrimitiveComparison.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_equals/PrimitiveComparison.java new file mode 100644 index 000000000000..0f25977323ab --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_equals/PrimitiveComparison.java @@ -0,0 +1,6 @@ +public class PrimitiveComparison { + + boolean a(int i, int j) { + return i == j; + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_equals/SimpleObjectComparison.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_equals/SimpleObjectComparison.java new file mode 100644 index 000000000000..baccea789e04 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_equals/SimpleObjectComparison.java @@ -0,0 +1,6 @@ +public class SimpleObjectComparison { + + boolean a(Object a, Object b) { + return a == b; + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_equals/SimpleObjectComparison_after.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_equals/SimpleObjectComparison_after.java new file mode 100644 index 000000000000..ab1bcce4c87b --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_equals/SimpleObjectComparison_after.java @@ -0,0 +1,6 @@ +public class SimpleObjectComparison { + + boolean a(Object a, Object b) { + return a.equals(b); + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/equality/ReplaceEqualityWithEqualsIntentionTest.java b/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/equality/ReplaceEqualityWithEqualsIntentionTest.java new file mode 100644 index 000000000000..f9150050fbdd --- /dev/null +++ b/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/equality/ReplaceEqualityWithEqualsIntentionTest.java @@ -0,0 +1,42 @@ +/* + * 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.ipp.equality; + +import com.siyeh.IntentionPowerPackBundle; +import com.siyeh.ipp.IPPTestCase; + +/** + * @see com.siyeh.ipp.equality.ReplaceEqualityWithEqualsIntention + * @author Bas Leijdekkers + */ +public class ReplaceEqualityWithEqualsIntentionTest extends IPPTestCase { + + public void testEnumComparison() { assertIntentionNotAvailable(); } + public void testNullComparison() { assertIntentionNotAvailable(); } + public void testPrimitiveComparison() { assertIntentionNotAvailable(); } + public void testSimpleObjectComparison() { doTest(); } + public void testNegatedObjectComparison() { doTest(IntentionPowerPackBundle.message("replace.equality.with.not.equals.intention.name")); } + + @Override + protected String getIntentionName() { + return IntentionPowerPackBundle.message("replace.equality.with.equals.intention.name"); + } + + @Override + protected String getRelativePath() { + return "equality/replace_equality_with_equals"; + } +} \ No newline at end of file