better intention name "Replace '==' with 'equals()'" and test

This commit is contained in:
Bas Leijdekkers
2014-09-01 11:44:42 +02:00
parent af137663d1
commit fc9df600f5
11 changed files with 110 additions and 46 deletions
@@ -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
@@ -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));
}
}
@@ -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) {
@@ -0,0 +1,8 @@
public class EnumComparison {
enum E { A, B }
boolean a(E a, E b) {
return a ==<caret> b;
}
}
@@ -0,0 +1,6 @@
public class NegatedObjectComparison {
boolean a(Object a, Object b) {
return a !=<caret> b;
}
}
@@ -0,0 +1,6 @@
public class NegatedObjectComparison {
boolean a(Object a, Object b) {
return !a.equals(b);
}
}
@@ -0,0 +1,6 @@
public class NullComparison {
boolean a(Object a) {
return a ==<caret> null;
}
}
@@ -0,0 +1,6 @@
public class PrimitiveComparison {
boolean a(int i, int j) {
return i <caret>== j;
}
}
@@ -0,0 +1,6 @@
public class SimpleObjectComparison {
boolean a(Object a, Object b) {
return a == b<caret>;
}
}
@@ -0,0 +1,6 @@
public class SimpleObjectComparison {
boolean a(Object a, Object b) {
return a.equals(b);
}
}
@@ -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";
}
}