diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ClassUtils.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ClassUtils.java
index 40daa3332896..4656d0c4610a 100644
--- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ClassUtils.java
+++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ClassUtils.java
@@ -19,6 +19,7 @@ import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.TypeConversionUtil;
import org.jetbrains.annotations.Contract;
+import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.Nullable;
import java.util.HashSet;
@@ -80,7 +81,7 @@ public class ClassUtils {
private ClassUtils() {}
@Nullable
- public static PsiClass findClass(String fqClassName, PsiElement context) {
+ public static PsiClass findClass(@NonNls String fqClassName, PsiElement context) {
return JavaPsiFacade.getInstance(context.getProject()).findClass(fqClassName, context.getResolveScope());
}
diff --git a/plugins/IntentionPowerPak/src/META-INF/IntentionPowerPack.xml b/plugins/IntentionPowerPak/src/META-INF/IntentionPowerPack.xml
index 73e279fa91a9..95c585fef36c 100644
--- a/plugins/IntentionPowerPak/src/META-INF/IntentionPowerPack.xml
+++ b/plugins/IntentionPowerPak/src/META-INF/IntentionPowerPack.xml
@@ -103,6 +103,12 @@
intention.category.boolean
+
+ com.siyeh.ipp.equality.ReplaceEqualityWithSafeEqualsIntention
+ com.siyeh.IntentionPowerPackBundle
+ intention.category.boolean
+
+
com.siyeh.ipp.trivialif.ExpandBooleanIntention
com.siyeh.IntentionPowerPackBundle
diff --git a/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties b/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties
index 25670bd1299a..923df3eb28f4 100644
--- a/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties
+++ b/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties
@@ -14,6 +14,7 @@ change.to.end.of.line.comment.intention.family.name=Replace with End Of Line Com
assert.to.if.intention.name=Replace 'assert' with 'if' statement
assert.to.if.intention.family.name=Replace Assert with If Statement
replace.equality.with.safe.equals.intention.name=Replace '==' with safe '.equals()'
+replace.equality.with.safe.not.equals.intention.name=Replace '!=' with safe '!equals()'
replace.equality.with.safe.equals.intention.family.name=Replace Equality with Safe Equals
replace.for.each.loop.with.indexed.for.loop.intention.name=Replace 'for each' loop with indexed 'for' loop
replace.for.each.loop.with.iterator.for.loop.intention.name=Replace 'for each' loop with iterator 'for' loop
diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/equality/ReplaceEqualityWithSafeEqualsIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/equality/ReplaceEqualityWithSafeEqualsIntention.java
index d14804ae2cda..12acaa006f54 100644
--- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/equality/ReplaceEqualityWithSafeEqualsIntention.java
+++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/equality/ReplaceEqualityWithSafeEqualsIntention.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.
@@ -17,25 +17,36 @@ package com.siyeh.ipp.equality;
import com.intellij.psi.*;
import com.intellij.psi.tree.IElementType;
-import com.intellij.util.IncorrectOperationException;
+import com.intellij.psi.util.PsiUtil;
+import com.siyeh.IntentionPowerPackBundle;
import com.siyeh.ig.PsiReplacementUtil;
+import com.siyeh.ig.psiutils.ClassUtils;
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 ReplaceEqualityWithSafeEqualsIntention extends Intention {
+public class ReplaceEqualityWithSafeEqualsIntention extends MutablyNamedIntention {
+
+ @Override
+ protected String getTextForElement(PsiElement element) {
+ final PsiBinaryExpression binaryExpression = (PsiBinaryExpression)element;
+ if (JavaTokenType.NE.equals(binaryExpression.getOperationTokenType())) {
+ return IntentionPowerPackBundle.message("replace.equality.with.safe.not.equals.intention.name");
+ }
+ else {
+ return IntentionPowerPackBundle.message("replace.equality.with.safe.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) {
@@ -56,26 +67,26 @@ public class ReplaceEqualityWithSafeEqualsIntention extends Intention {
final PsiJavaToken operationSign = exp.getOperationSign();
final IElementType tokenType = operationSign.getTokenType();
final String signText = operationSign.getText();
- @NonNls final StringBuilder buffer = new StringBuilder(lhsText);
- buffer.append("==null?");
- buffer.append(rhsText);
- buffer.append(signText);
- buffer.append(" null:");
- if (tokenType.equals(JavaTokenType.NE)) {
- buffer.append('!');
- }
- if (ParenthesesUtils.getPrecedence(strippedLhs) >
- ParenthesesUtils.METHOD_CALL_PRECEDENCE) {
- buffer.append('(');
- buffer.append(lhsText);
- buffer.append(')');
+ @NonNls final StringBuilder newExpression = new StringBuilder();
+ if (PsiUtil.isLanguageLevel7OrHigher(element) && ClassUtils.findClass("java.util.Objects", element) != null) {
+ if (tokenType.equals(JavaTokenType.NE)) {
+ newExpression.append('!');
+ }
+ newExpression.append("java.util.Objects.equals(").append(lhsText).append(',').append(rhsText).append(')');
}
else {
- buffer.append(lhsText);
+ newExpression.append(lhsText).append("==null?").append(rhsText).append(signText).append(" null:");
+ if (tokenType.equals(JavaTokenType.NE)) {
+ newExpression.append('!');
+ }
+ if (ParenthesesUtils.getPrecedence(strippedLhs) > ParenthesesUtils.METHOD_CALL_PRECEDENCE) {
+ newExpression.append('(').append(lhsText).append(')');
+ }
+ else {
+ newExpression.append(lhsText);
+ }
+ newExpression.append(".equals(").append(rhsText).append(')');
}
- buffer.append(".equals(");
- buffer.append(rhsText);
- buffer.append(')');
- PsiReplacementUtil.replaceExpression(exp, buffer.toString());
+ PsiReplacementUtil.replaceExpressionAndShorten(exp, newExpression.toString());
}
}
\ No newline at end of file
diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_safe_equals/EnumComparison.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_safe_equals/EnumComparison.java
new file mode 100644
index 000000000000..43b16a421c25
--- /dev/null
+++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_safe_equals/EnumComparison.java
@@ -0,0 +1,10 @@
+package com.siyeh.ipp.equality.replace_equality_with_safe_equals;
+
+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_safe_equals/NegatedObjectComparison.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_safe_equals/NegatedObjectComparison.java
new file mode 100644
index 000000000000..018def909d05
--- /dev/null
+++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_safe_equals/NegatedObjectComparison.java
@@ -0,0 +1,8 @@
+package com.siyeh.ipp.equality.replace_equality_with_safe_equals;
+
+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_safe_equals/NegatedObjectComparison_after.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_safe_equals/NegatedObjectComparison_after.java
new file mode 100644
index 000000000000..b7f48289a4b8
--- /dev/null
+++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_safe_equals/NegatedObjectComparison_after.java
@@ -0,0 +1,8 @@
+package com.siyeh.ipp.equality.replace_equality_with_safe_equals;
+
+public class NegatedObjectComparison {
+
+ boolean a(Object a, Object b) {
+ return a == null ? b != null : !a.equals(b);
+ }
+}
\ No newline at end of file
diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_safe_equals/NullComparison.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_safe_equals/NullComparison.java
new file mode 100644
index 000000000000..10aeb19f6fed
--- /dev/null
+++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_safe_equals/NullComparison.java
@@ -0,0 +1,8 @@
+package com.siyeh.ipp.equality.replace_equality_with_safe_equals;
+
+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_safe_equals/PrimitiveComparison.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_safe_equals/PrimitiveComparison.java
new file mode 100644
index 000000000000..8ae058592d0a
--- /dev/null
+++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_safe_equals/PrimitiveComparison.java
@@ -0,0 +1,8 @@
+package com.siyeh.ipp.equality.replace_equality_with_safe_equals;
+
+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_safe_equals/SimpleObjectComparison.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_safe_equals/SimpleObjectComparison.java
new file mode 100644
index 000000000000..304b06ff67a1
--- /dev/null
+++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_safe_equals/SimpleObjectComparison.java
@@ -0,0 +1,8 @@
+package com.siyeh.ipp.equality.replace_equality_with_safe_equals;
+
+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_safe_equals/SimpleObjectComparison_after.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_safe_equals/SimpleObjectComparison_after.java
new file mode 100644
index 000000000000..5ee0ddf5d6df
--- /dev/null
+++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/equality/replace_equality_with_safe_equals/SimpleObjectComparison_after.java
@@ -0,0 +1,8 @@
+package com.siyeh.ipp.equality.replace_equality_with_safe_equals;
+
+public class SimpleObjectComparison {
+
+ boolean a(Object a, Object b) {
+ return a == null ? b == null : a.equals(b);
+ }
+}
\ No newline at end of file
diff --git a/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/equality/ReplaceEqualityWithSafeEqualsIntentionTest.java b/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/equality/ReplaceEqualityWithSafeEqualsIntentionTest.java
new file mode 100644
index 000000000000..c2fe09148f9a
--- /dev/null
+++ b/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/equality/ReplaceEqualityWithSafeEqualsIntentionTest.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.ReplaceEqualityWithSafeEqualsIntention
+ * @author Bas Leijdekkers
+ */
+public class ReplaceEqualityWithSafeEqualsIntentionTest 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.safe.not.equals.intention.name")); }
+
+ @Override
+ protected String getIntentionName() {
+ return IntentionPowerPackBundle.message("replace.equality.with.safe.equals.intention.name");
+ }
+
+ @Override
+ protected String getRelativePath() {
+ return "equality/replace_equality_with_safe_equals";
+ }
+}
\ No newline at end of file