From faa180a74ffeeb7cf624341c275cef63debe9300 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Wed, 20 Nov 2013 16:42:39 +0100 Subject: [PATCH] IDEA-116758 ('equals()' between objects of inconvertible types - not working for ...= equals("differentClass")); --- ...lsBetweenInconvertibleTypesInspection.java | 33 +++++++------ .../src/com/siyeh/ig/psiutils/TypeUtils.java | 4 ++ ...lsBetweenInconvertibleTypesInspection.java | 11 ----- ...tweenInconvertibleTypesInspectionTest.java | 46 +++++++++++++++++++ 4 files changed, 69 insertions(+), 25 deletions(-) delete mode 100644 plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/EqualsBetweenInconvertibleTypesInspection.java create mode 100644 plugins/InspectionGadgets/testsrc/com/siyeh/ig/bugs/EqualsBetweenInconvertibleTypesInspectionTest.java diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/EqualsBetweenInconvertibleTypesInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/EqualsBetweenInconvertibleTypesInspection.java index 717e10e98cbd..fc0a72508c81 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/EqualsBetweenInconvertibleTypesInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/EqualsBetweenInconvertibleTypesInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2007 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2013 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. @@ -16,11 +16,13 @@ package com.siyeh.ig.bugs; import com.intellij.psi.*; +import com.intellij.psi.util.PsiTreeUtil; import com.intellij.psi.util.TypeConversionUtil; import com.siyeh.InspectionGadgetsBundle; import com.siyeh.ig.BaseInspection; import com.siyeh.ig.BaseInspectionVisitor; import com.siyeh.ig.psiutils.MethodCallUtils; +import com.siyeh.ig.psiutils.TypeUtils; import org.jetbrains.annotations.NotNull; public class EqualsBetweenInconvertibleTypesInspection @@ -64,31 +66,34 @@ public class EqualsBetweenInconvertibleTypesInspection if (!MethodCallUtils.isEqualsCall(expression)) { return; } - final PsiReferenceExpression methodExpression = - expression.getMethodExpression(); + final PsiReferenceExpression methodExpression = expression.getMethodExpression(); final PsiExpressionList argumentList = expression.getArgumentList(); - final PsiExpression[] args = argumentList.getExpressions(); - if (args.length != 1) { + final PsiExpression[] arguments = argumentList.getExpressions(); + if (arguments.length != 1) { return; } - final PsiExpression expression1 = args[0]; - final PsiExpression expression2 = - methodExpression.getQualifierExpression(); + final PsiExpression expression1 = arguments[0]; + final PsiExpression expression2 = methodExpression.getQualifierExpression(); + final PsiType comparisonType; if (expression2 == null) { + final PsiClass aClass = PsiTreeUtil.getParentOfType(expression, PsiClass.class); + if (aClass == null) { + return; + } + comparisonType = TypeUtils.getType(aClass); + } else { + comparisonType = expression2.getType(); + } + if (comparisonType == null) { return; } final PsiType comparedType = expression1.getType(); if (comparedType == null) { return; } - final PsiType comparisonType = expression2.getType(); - if (comparisonType == null) { - return; - } final PsiType comparedTypeErasure = TypeConversionUtil.erasure(comparedType); final PsiType comparisonTypeErasure = TypeConversionUtil.erasure(comparisonType); - if (comparedTypeErasure == null || - comparisonTypeErasure == null || + if (comparedTypeErasure == null || comparisonTypeErasure == null || TypeConversionUtil.areTypesConvertible(comparedTypeErasure, comparisonTypeErasure)) { return; } diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/TypeUtils.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/TypeUtils.java index 5bb992dbbc90..34110a4cc702 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/TypeUtils.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/TypeUtils.java @@ -38,6 +38,10 @@ public class TypeUtils { return factory.createTypeByFQClassName(fqName, scope); } + public static PsiClassType getType(@NotNull PsiClass aClass) { + return JavaPsiFacade.getInstance(aClass.getProject()).getElementFactory().createType(aClass); + } + public static PsiClassType getObjectType(@NotNull PsiElement context) { return getType(CommonClassNames.JAVA_LANG_OBJECT, context); } diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/EqualsBetweenInconvertibleTypesInspection.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/EqualsBetweenInconvertibleTypesInspection.java deleted file mode 100644 index 5a99f5bbd026..000000000000 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/EqualsBetweenInconvertibleTypesInspection.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.siyeh.igtest.bugs; - -public class EqualsBetweenInconvertibleTypesInspection { - - public void foo() - { - final Integer foo = new Integer(3); - final Double bar = new Double(3); - foo.equals(bar); - } -} diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/bugs/EqualsBetweenInconvertibleTypesInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/bugs/EqualsBetweenInconvertibleTypesInspectionTest.java new file mode 100644 index 000000000000..3168d9ec5b0b --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/bugs/EqualsBetweenInconvertibleTypesInspectionTest.java @@ -0,0 +1,46 @@ +/* + * Copyright 2000-2013 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.ig.bugs; + +import com.intellij.codeInspection.InspectionProfileEntry; +import com.siyeh.ig.LightInspectionTestCase; + +/** + * @author Bas Leijdekkers + */ +public class EqualsBetweenInconvertibleTypesInspectionTest extends LightInspectionTestCase { + + public void testSimple() { + doMemberTest("public void foo() {\n" + + " final Integer foo = new Integer(3);\n" + + " final Double bar = new Double(3);\n" + + " foo./*'equals()' between objects of inconvertible types 'Double' and 'Integer'*/equals/**/(bar);\n" + + "}\n"); + } + + public void testWithoutQualifier() { + doTest("class Clazz {\n" + + " void foo() {\n" + + " boolean bar = /*'equals()' between objects of inconvertible types 'String' and 'Clazz'*/equals/**/(\"differentClass\");\n" + + " }\n" + + "}"); + } + + @Override + protected InspectionProfileEntry getInspection() { + return new EqualsBetweenInconvertibleTypesInspection(); + } +}