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
.impl
;
18 import com
.intellij
.openapi
.progress
.ProgressManager
;
19 import com
.intellij
.openapi
.vfs
.VirtualFile
;
20 import com
.intellij
.psi
.*;
21 import com
.intellij
.psi
.impl
.java
.stubs
.index
.JavaFieldNameIndex
;
22 import com
.intellij
.psi
.impl
.java
.stubs
.index
.JavaMethodNameIndex
;
23 import com
.intellij
.psi
.impl
.java
.stubs
.index
.JavaShortClassNameIndex
;
24 import com
.intellij
.psi
.search
.FilenameIndex
;
25 import com
.intellij
.psi
.search
.GlobalSearchScope
;
26 import com
.intellij
.psi
.search
.PsiShortNamesCache
;
27 import com
.intellij
.psi
.stubs
.StubIndex
;
28 import com
.intellij
.util
.ArrayUtil
;
29 import com
.intellij
.util
.containers
.HashSet
;
30 import gnu
.trove
.THashSet
;
31 import gnu
.trove
.TObjectHashingStrategy
;
32 import org
.jetbrains
.annotations
.NonNls
;
33 import org
.jetbrains
.annotations
.NotNull
;
35 import java
.util
.ArrayList
;
36 import java
.util
.Collection
;
37 import java
.util
.List
;
40 class PsiShortNamesCacheImpl
extends PsiShortNamesCache
{
41 private final PsiManagerEx myManager
;
43 public PsiShortNamesCacheImpl(PsiManagerEx manager
) {
48 public PsiFile
[] getFilesByName(@NotNull String name
) {
49 return FilenameIndex
.getFilesByName(myManager
.getProject(), name
, GlobalSearchScope
.projectScope(myManager
.getProject()));
53 public String
[] getAllFileNames() {
54 return FilenameIndex
.getAllFilenames(myManager
.getProject());
58 public PsiClass
[] getClassesByName(@NotNull String name
, @NotNull final GlobalSearchScope scope
) {
59 final Collection
<PsiClass
> classes
= JavaShortClassNameIndex
.getInstance().get(name
, myManager
.getProject(), scope
);
61 if (classes
.isEmpty()) return PsiClass
.EMPTY_ARRAY
;
62 ArrayList
<PsiClass
> list
= new ArrayList
<PsiClass
>(classes
.size());
65 for (PsiClass aClass
: classes
) {
66 VirtualFile vFile
= aClass
.getContainingFile().getVirtualFile();
67 if (!scope
.contains(vFile
)) continue;
69 for (int j
= 0; j
< list
.size(); j
++) {
70 PsiClass aClass1
= list
.get(j
);
72 String qName
= aClass
.getQualifiedName();
73 String qName1
= aClass1
.getQualifiedName();
74 if (qName
!= null && qName1
!= null && qName
.equals(qName1
)) {
75 VirtualFile vFile1
= aClass1
.getContainingFile().getVirtualFile();
76 int res
= scope
.compare(vFile1
, vFile
);
78 continue OuterLoop
; // aClass1 hides aClass
82 //noinspection AssignmentToForLoopParameter
83 j
--; // aClass hides aClass1
90 return list
.toArray(new PsiClass
[list
.size()]);
94 public String
[] getAllClassNames() {
95 final Collection
<String
> names
= JavaShortClassNameIndex
.getInstance().getAllKeys(myManager
.getProject());
96 return ArrayUtil
.toStringArray(names
);
99 public void getAllClassNames(@NotNull HashSet
<String
> set
) {
100 set
.addAll(JavaShortClassNameIndex
.getInstance().getAllKeys(myManager
.getProject()));
104 public PsiMethod
[] getMethodsByName(@NotNull String name
, @NotNull final GlobalSearchScope scope
) {
105 final Collection
<PsiMethod
> methods
=
106 StubIndex
.getInstance().get(JavaMethodNameIndex
.KEY
, name
, myManager
.getProject(), scope
);
107 if (methods
.isEmpty()) return PsiMethod
.EMPTY_ARRAY
;
109 List
<PsiMethod
> list
= filterMembers(methods
, scope
);
110 return list
.toArray(new PsiMethod
[list
.size()]);
115 public PsiMethod
[] getMethodsByNameIfNotMoreThan(@NonNls @NotNull final String name
, @NotNull final GlobalSearchScope scope
, final int maxCount
) {
116 return getMethodsByName(name
, scope
); // TODO!!!
120 public String
[] getAllMethodNames() {
121 final Collection
<String
> names
= JavaMethodNameIndex
.getInstance().getAllKeys(myManager
.getProject());
122 return ArrayUtil
.toStringArray(names
);
125 public void getAllMethodNames(@NotNull HashSet
<String
> set
) {
126 set
.addAll(JavaMethodNameIndex
.getInstance().getAllKeys(myManager
.getProject()));
130 public PsiField
[] getFieldsByName(@NotNull String name
, @NotNull final GlobalSearchScope scope
) {
131 final Collection
<PsiField
> fields
= JavaFieldNameIndex
.getInstance().get(name
, myManager
.getProject(), scope
);
133 if (fields
.isEmpty()) return PsiField
.EMPTY_ARRAY
;
135 List
<PsiField
> list
= filterMembers(fields
, scope
);
136 return list
.toArray(new PsiField
[list
.size()]);
140 public String
[] getAllFieldNames() {
141 final Collection
<String
> names
= JavaFieldNameIndex
.getInstance().getAllKeys(myManager
.getProject());
142 return ArrayUtil
.toStringArray(names
);
145 public void getAllFieldNames(@NotNull HashSet
<String
> set
) {
146 set
.addAll(JavaFieldNameIndex
.getInstance().getAllKeys(myManager
.getProject()));
149 private <T
extends PsiMember
> List
<T
> filterMembers(Collection
<T
> members
, final GlobalSearchScope scope
) {
150 List
<T
> result
= new ArrayList
<T
>();
151 Set
<PsiMember
> set
= new THashSet
<PsiMember
>(members
.size(), new TObjectHashingStrategy
<PsiMember
>() {
152 public int computeHashCode(PsiMember member
) {
154 final PsiClass clazz
= member
.getContainingClass();
156 String name
= clazz
.getName();
158 code
+= name
.hashCode();
160 //anonymous classes are not equivalent
161 code
+= clazz
.hashCode();
164 if (member
instanceof PsiMethod
) {
165 code
+= 37 * ((PsiMethod
)member
).getParameterList().getParametersCount();
170 public boolean equals(PsiMember object
, PsiMember object1
) {
171 return myManager
.areElementsEquivalent(object
, object1
);
175 for (T member
: members
) {
176 ProgressManager
.getInstance().checkCanceled();
178 if (!scope
.contains(member
.getContainingFile().getVirtualFile())) continue;
179 if (!set
.add(member
)) continue;