2 * Copyright 2000-2009 JetBrains s.r.o.
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
.intellij
.psi
.util
.proximity
;
18 import com
.intellij
.psi
.*;
19 import com
.intellij
.psi
.util
.ProximityLocation
;
20 import com
.intellij
.psi
.util
.PsiTreeUtil
;
21 import org
.jetbrains
.annotations
.NotNull
;
22 import org
.jetbrains
.annotations
.NonNls
;
23 import org
.jetbrains
.annotations
.Nullable
;
28 public class JavaInheritanceWeigher
extends ProximityWeigher
{
30 public Comparable
weigh(@NotNull final PsiElement element
, final ProximityLocation location
) {
31 final PsiElement position
= location
.getPosition();
32 PsiClass placeClass
= PsiTreeUtil
.getContextOfType(element
, PsiClass
.class, false);
33 if (position
.getParent() instanceof PsiReferenceExpression
) {
34 final PsiExpression qualifierExpression
= ((PsiReferenceExpression
)position
.getParent()).getQualifierExpression();
35 if (qualifierExpression
!= null) {
36 final PsiType type
= qualifierExpression
.getType();
37 if (type
instanceof PsiClassType
) {
38 final PsiClass psiClass
= ((PsiClassType
)type
).resolve();
39 if (psiClass
!= null) {
40 placeClass
= psiClass
;
46 if (element
instanceof PsiClass
&& isTooGeneral((PsiClass
)element
)) return false;
47 if (element
instanceof PsiMethod
&& isTooGeneral(((PsiMethod
)element
).getContainingClass())) return false;
49 PsiClass contextClass
= PsiTreeUtil
.getContextOfType(position
, PsiClass
.class, false);
50 while (contextClass
!= null) {
51 PsiClass elementClass
= placeClass
;
52 while (elementClass
!= null) {
53 if (contextClass
.isInheritor(elementClass
, true)) return true;
54 elementClass
= elementClass
.getContainingClass();
56 contextClass
= contextClass
.getContainingClass();
61 private static boolean isTooGeneral(@Nullable final PsiClass element
) {
62 if (element
== null) return true;
64 @NonNls final String qname
= element
.getQualifiedName();
65 return qname
== null || qname
.startsWith("java.lang.");