diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/CovariantCompareToInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/CovariantCompareToInspection.java index 244d9d833450..5cddf30e7f9e 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/CovariantCompareToInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/CovariantCompareToInspection.java @@ -76,42 +76,34 @@ public class CovariantCompareToInspection extends BaseInspection { } final PsiMethod[] methods = aClass.findMethodsByName( HardcodedMethodConstants.COMPARE_TO, false); - for (PsiMethod compareToMethod : methods) { - if (isNonVariantCompareTo(compareToMethod)) { - return; - } - } final Project project = method.getProject(); final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project); final GlobalSearchScope scope = method.getResolveScope(); - final PsiClass comparableClass = - psiFacade.findClass(CommonClassNames.JAVA_LANG_COMPARABLE, - scope); + final PsiClass comparableClass = psiFacade.findClass(CommonClassNames.JAVA_LANG_COMPARABLE, scope); + PsiType substitutedTypeParam = null; if (comparableClass != null && comparableClass.getTypeParameters().length == 1) { - final PsiSubstitutor superSubstitutor = - TypeConversionUtil.getClassSubstitutor(comparableClass, - aClass, PsiSubstitutor.EMPTY); + final PsiSubstitutor superSubstitutor = TypeConversionUtil.getClassSubstitutor(comparableClass, aClass, PsiSubstitutor.EMPTY); //null iff aClass is not inheritor of comparableClass if (superSubstitutor != null) { - final PsiType substituted = - superSubstitutor.substitute( - comparableClass.getTypeParameters()[0]); - if (paramType.equals(substituted)) { - return; - } + substitutedTypeParam = superSubstitutor.substitute(comparableClass.getTypeParameters()[0]); + } + } + for (PsiMethod compareToMethod : methods) { + if (isNonVariantCompareTo(compareToMethod, substitutedTypeParam)) { + return; } } registerMethodError(method); } - private static boolean isNonVariantCompareTo(PsiMethod method) { + private static boolean isNonVariantCompareTo(PsiMethod method, PsiType substitutedTypeParam) { final PsiManager manager = method.getManager(); final Project project = method.getProject(); final PsiClassType objectType = PsiType.getJavaLangObject( manager, GlobalSearchScope.allScope(project)); - return MethodUtils.methodMatches(method, null, PsiType.INT, - HardcodedMethodConstants.COMPARE_TO, objectType); + return MethodUtils.methodMatches(method, null, PsiType.INT, HardcodedMethodConstants.COMPARE_TO, objectType) || + (substitutedTypeParam != null && MethodUtils.methodMatches(method, null, PsiType.INT, HardcodedMethodConstants.COMPARE_TO, substitutedTypeParam)); } } } \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/covariantCompareTo/simple/Simple.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/covariantCompareTo/simple/Simple.java new file mode 100644 index 000000000000..52fed23d835d --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/covariantCompareTo/simple/Simple.java @@ -0,0 +1,11 @@ +import java.lang.Comparable; + +class Foo implements Comparable { + public int compareTo(Foo o) { + return 0; //To change body of implemented methods use File | Settings | File Templates. + } + + public int compareTo(String o) { + return 0; //To change body of implemented methods use File | Settings | File Templates. + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/covariantCompareTo/simple/expected.xml b/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/covariantCompareTo/simple/expected.xml new file mode 100644 index 000000000000..4704d91e891d --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/covariantCompareTo/simple/expected.xml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/bugs/CovariantCompareToInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/bugs/CovariantCompareToInspectionTest.java new file mode 100644 index 000000000000..2109c8b1fcf3 --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/bugs/CovariantCompareToInspectionTest.java @@ -0,0 +1,29 @@ +/* + * Copyright 2000-2012 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.siyeh.ig.IGInspectionTestCase; + +public class CovariantCompareToInspectionTest extends IGInspectionTestCase { + + public void testSimple() throws Exception { + doTest(); + } + + private void doTest() throws Exception { + doTest("com/siyeh/igtest/bugs/covariantCompareTo/" + getTestName(true), new CovariantCompareToInspection()); + } +}