2 * Copyright 2003-2007 Dave Griffith, Bas Leijdekkers
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com
.siyeh
.ig
.bugs
;
18 import com
.intellij
.openapi
.project
.Project
;
19 import com
.intellij
.psi
.*;
20 import com
.intellij
.psi
.search
.GlobalSearchScope
;
21 import com
.intellij
.psi
.util
.TypeConversionUtil
;
22 import com
.siyeh
.HardcodedMethodConstants
;
23 import com
.siyeh
.InspectionGadgetsBundle
;
24 import com
.siyeh
.ig
.BaseInspection
;
25 import com
.siyeh
.ig
.BaseInspectionVisitor
;
26 import com
.siyeh
.ig
.psiutils
.MethodUtils
;
27 import com
.siyeh
.ig
.psiutils
.TypeUtils
;
28 import org
.jetbrains
.annotations
.NotNull
;
30 public class CovariantCompareToInspection
extends BaseInspection
{
33 public String
getDisplayName() {
34 return InspectionGadgetsBundle
.message(
35 "covariant.compareto.display.name");
39 public String
buildErrorString(Object
... infos
) {
40 return InspectionGadgetsBundle
.message(
41 "covariant.compareto.problem.descriptor");
44 public BaseInspectionVisitor
buildVisitor() {
45 return new CovariantCompareToVisitor();
48 private static class CovariantCompareToVisitor
49 extends BaseInspectionVisitor
{
51 @Override public void visitMethod(@NotNull PsiMethod method
) {
52 // note: no call to super
53 final String name
= method
.getName();
54 if (!HardcodedMethodConstants
.COMPARE_TO
.equals(name
)) {
57 if (!method
.hasModifierProperty(PsiModifier
.PUBLIC
)) {
60 final PsiParameterList parameterList
= method
.getParameterList();
61 if (parameterList
.getParametersCount() != 1) {
64 final PsiParameter
[] parameters
= parameterList
.getParameters();
65 final PsiType paramType
= parameters
[0].getType();
66 if (TypeUtils
.isJavaLangObject(paramType
)) {
69 final PsiClass aClass
= method
.getContainingClass();
73 final PsiMethod
[] methods
= aClass
.findMethodsByName(
74 HardcodedMethodConstants
.COMPARE_TO
, false);
75 for(PsiMethod compareToMethod
: methods
){
76 if(isNonVariantCompareTo(compareToMethod
)){
80 final PsiManager manager
= method
.getManager();
81 final PsiClass comparableClass
=
82 JavaPsiFacade
.getInstance(manager
.getProject()).findClass("java.lang.Comparable", method
.getResolveScope());
83 if (comparableClass
!= null &&
84 comparableClass
.getTypeParameters().length
== 1) {
85 final PsiSubstitutor superSubstitutor
=
86 TypeConversionUtil
.getClassSubstitutor(comparableClass
,
87 aClass
, PsiSubstitutor
.EMPTY
);
88 //null iff aClass is not inheritor of comparableClass
89 if (superSubstitutor
!= null) {
90 final PsiType substituted
=
91 superSubstitutor
.substitute(
92 comparableClass
.getTypeParameters()[0]);
93 if (paramType
.equals(substituted
)) {
98 registerMethodError(method
);
101 private static boolean isNonVariantCompareTo(PsiMethod method
) {
102 final PsiManager manager
= method
.getManager();
103 final Project project
= method
.getProject();
104 final PsiClassType objectType
= PsiType
.getJavaLangObject(
105 manager
, GlobalSearchScope
.allScope(project
));
106 return MethodUtils
.methodMatches(method
, null, PsiType
.INT
,
107 HardcodedMethodConstants
.COMPARE_TO
, objectType
);