summaryrefslogtreecommitdiff
path: root/plugins/IntentionPowerPak/src/com/siyeh/ipp/equality
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/IntentionPowerPak/src/com/siyeh/ipp/equality')
-rw-r--r--plugins/IntentionPowerPak/src/com/siyeh/ipp/equality/ObjectEqualityPredicate.java36
-rw-r--r--plugins/IntentionPowerPak/src/com/siyeh/ipp/equality/ReplaceEqualityWithEqualsIntention.java31
-rw-r--r--plugins/IntentionPowerPak/src/com/siyeh/ipp/equality/ReplaceEqualityWithSafeEqualsIntention.java63
3 files changed, 59 insertions, 71 deletions
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/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