mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
covariant compare to: correctly process methods with Comparable signature
This commit is contained in:
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
import java.lang.Comparable;
|
||||
|
||||
class Foo implements Comparable<Foo> {
|
||||
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.
|
||||
}
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<problems/>
|
||||
+29
@@ -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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user