mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
restore lost "Replace '==' with safe '.equals()'" intention and test
This commit is contained in:
+2
-1
@@ -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());
|
||||
}
|
||||
|
||||
|
||||
@@ -103,6 +103,12 @@
|
||||
<categoryKey>intention.category.boolean</categoryKey>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>com.siyeh.ipp.equality.ReplaceEqualityWithSafeEqualsIntention</className>
|
||||
<bundleName>com.siyeh.IntentionPowerPackBundle</bundleName>
|
||||
<categoryKey>intention.category.boolean</categoryKey>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>com.siyeh.ipp.trivialif.ExpandBooleanIntention</className>
|
||||
<bundleName>com.siyeh.IntentionPowerPackBundle</bundleName>
|
||||
|
||||
@@ -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
|
||||
|
||||
+37
-26
@@ -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());
|
||||
}
|
||||
}
|
||||
+10
@@ -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 ==<caret> b;
|
||||
}
|
||||
}
|
||||
+8
@@ -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 !=<caret> b;
|
||||
}
|
||||
}
|
||||
+8
@@ -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);
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.siyeh.ipp.equality.replace_equality_with_safe_equals;
|
||||
|
||||
public class NullComparison {
|
||||
|
||||
boolean a(Object a) {
|
||||
return a ==<caret> null;
|
||||
}
|
||||
}
|
||||
+8
@@ -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 <caret>== j;
|
||||
}
|
||||
}
|
||||
+8
@@ -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<caret>;
|
||||
}
|
||||
}
|
||||
+8
@@ -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);
|
||||
}
|
||||
}
|
||||
+42
@@ -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";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user