2 * Copyright 2000-2007 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
;
18 import com
.intellij
.psi
.PsiClass
;
19 import com
.intellij
.psi
.PsiClassType
;
20 import com
.intellij
.psi
.PsiManager
;
21 import com
.intellij
.psi
.PsiType
;
22 import com
.intellij
.util
.Processor
;
23 import gnu
.trove
.THashSet
;
24 import org
.jetbrains
.annotations
.NonNls
;
25 import org
.jetbrains
.annotations
.NotNull
;
26 import org
.jetbrains
.annotations
.Nullable
;
30 public class InheritanceUtil
{
32 * @deprecated Use {@link PsiClass#isInheritor(com.intellij.psi.PsiClass, boolean)} instead.
34 public static boolean isInheritor(@NotNull PsiClass candidateClass
, @NotNull PsiClass baseClass
, boolean checkDeep
) {
35 return candidateClass
.isInheritor(baseClass
, checkDeep
);
39 * @return true if aClass is the baseClass or baseClass inheritor
41 public static boolean isInheritorOrSelf(@Nullable PsiClass aClass
, @Nullable PsiClass baseClass
, boolean checkDeep
) { //TODO: remove this method!!
42 if (aClass
== null || baseClass
== null) return false;
43 PsiManager manager
= aClass
.getManager();
44 return manager
.areElementsEquivalent(baseClass
, aClass
) || aClass
.isInheritor(baseClass
, checkDeep
);
48 * @return true if aClass is the baseClass or baseClass inheritor
50 public static boolean isCorrectDescendant(@Nullable PsiClass aClass
, @Nullable PsiClass baseClass
, boolean checkDeep
) {
51 return isInheritorOrSelf(aClass
, baseClass
, checkDeep
);
54 public static boolean processSupers(@Nullable PsiClass aClass
, boolean includeSelf
, Processor
<PsiClass
> superProcessor
) {
55 if (aClass
== null) return true;
57 if (includeSelf
&& !superProcessor
.process(aClass
)) return false;
59 return processSupers(aClass
, superProcessor
, new THashSet
<PsiClass
>());
62 private static boolean processSupers(@NotNull PsiClass aClass
, Processor
<PsiClass
> superProcessor
, Set
<PsiClass
> visited
) {
63 if (!visited
.add(aClass
)) return true;
65 for (final PsiClass intf
: aClass
.getInterfaces()) {
66 if (!superProcessor
.process(intf
) || !processSupers(intf
, superProcessor
, visited
)) return false;
68 final PsiClass superClass
= aClass
.getSuperClass();
69 if (superClass
!= null) {
70 if (!superProcessor
.process(superClass
) || !processSupers(superClass
, superProcessor
, visited
)) return false;
75 public static boolean isInheritor(@Nullable PsiType type
, @NotNull @NonNls final String baseClassName
) {
76 if (type
instanceof PsiClassType
) {
77 final PsiClassType classType
= (PsiClassType
)type
;
78 final PsiClass psiClass
= classType
.resolve();
79 if (psiClass
== null) {
83 return !processSupers(psiClass
, true, new Processor
<PsiClass
>() {
84 public boolean process(PsiClass psiClass
) {
85 if (baseClassName
.equals(psiClass
.getQualifiedName())) {