From f14d113480ef9faf0d8333f83bf572442015d7af Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Thu, 29 May 2008 20:08:38 +0400 Subject: [PATCH] start of "assertEquals() may be assertSame()" inspection --- .../com/siyeh/InspectionGadgetsBundle.properties | 4 +- .../AssertEqualsMayBeAssertSameInspection.java | 74 ++++++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 plugins/InspectionGadgets/src/com/siyeh/ig/junit/AssertEqualsMayBeAssertSameInspection.java diff --git a/plugins/InspectionGadgets/src/com/siyeh/InspectionGadgetsBundle.properties b/plugins/InspectionGadgets/src/com/siyeh/InspectionGadgetsBundle.properties index b20e104c4b..2f927bde96 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/InspectionGadgetsBundle.properties +++ b/plugins/InspectionGadgets/src/com/siyeh/InspectionGadgetsBundle.properties @@ -1657,4 +1657,6 @@ ambiguous.method.call.problem.descriptor=#ref() inherited from ''{0 ambiguous.method.call.quickfix=Qualify method call with 'this.' change.modifier.quickfix=Make ''{0}'' the.whole.project=the whole project -this.class=this class \ No newline at end of file +this.class=this class +assertequals.may.be.assertsame.display.name=assertEquals() may be assertSame() +assertequals.may.be.assertsame.problem.descriptor=#ref may be assertSame() \ No newline at end of file diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/junit/AssertEqualsMayBeAssertSameInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/junit/AssertEqualsMayBeAssertSameInspection.java new file mode 100644 index 0000000000..4527717245 --- /dev/null +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/junit/AssertEqualsMayBeAssertSameInspection.java @@ -0,0 +1,74 @@ +/* + * Copyright 2008 Bas Leijdekkers + * + * 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.ig.junit; + +import com.intellij.psi.PsiMethodCallExpression; +import com.intellij.psi.PsiReferenceExpression; +import com.intellij.psi.PsiExpressionList; +import com.intellij.psi.PsiExpression; +import com.siyeh.InspectionGadgetsBundle; +import com.siyeh.ig.BaseInspection; +import com.siyeh.ig.BaseInspectionVisitor; +import org.jetbrains.annotations.NotNull; +import org.junit.Test; +import org.junit.Assert; + +public class AssertEqualsMayBeAssertSameInspection extends BaseInspection { + + @NotNull + public String getDisplayName() { + return InspectionGadgetsBundle.message( + "assertequals.may.be.assertsame.display.name"); + } + + @NotNull + protected String buildErrorString(Object... infos) { + return InspectionGadgetsBundle.message( + "assertequals.may.be.assertsame.problem.descriptor"); + } + + public BaseInspectionVisitor buildVisitor() { + return new AssertEqualsMayBeAssertSameVisitor(); + } + + private static class AssertEqualsMayBeAssertSameVisitor + extends BaseInspectionVisitor { + + public void visitMethodCallExpression( + PsiMethodCallExpression expression) { + super.visitMethodCallExpression(expression); + final PsiReferenceExpression methodExpression = + expression.getMethodExpression(); + final String name = methodExpression.getReferenceName(); + if (!"assertEquals".equals(name)) { + return; + } + final PsiExpressionList argumentList = expression.getArgumentList(); + final PsiExpression[] arguments = argumentList.getExpressions(); + if (arguments.length == 3 && arguments.length == 2) { + return; + } + + // only consider final classes + } + + @Test + void foo() { + Assert.assertEquals(new Object(), new Object()); + Assert.assertSame(new Object(), new Object()); + } + } +} -- 2.11.4.GIT