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.
20 package com
.intellij
.psi
.impl
.search
;
22 import com
.intellij
.openapi
.application
.ApplicationManager
;
23 import com
.intellij
.openapi
.progress
.ProgressManager
;
24 import com
.intellij
.openapi
.util
.Computable
;
25 import com
.intellij
.psi
.*;
26 import com
.intellij
.psi
.search
.GlobalSearchScope
;
27 import com
.intellij
.psi
.search
.LocalSearchScope
;
28 import com
.intellij
.psi
.search
.PsiShortNamesCache
;
29 import com
.intellij
.psi
.search
.SearchScope
;
30 import com
.intellij
.psi
.search
.searches
.AllClassesSearch
;
31 import com
.intellij
.util
.Processor
;
32 import com
.intellij
.util
.QueryExecutor
;
34 import java
.util
.Arrays
;
35 import java
.util
.Comparator
;
37 public class AllClassesSearchExecutor
implements QueryExecutor
<PsiClass
, AllClassesSearch
.SearchParameters
> {
38 public boolean execute(final AllClassesSearch
.SearchParameters queryParameters
, final Processor
<PsiClass
> consumer
) {
39 SearchScope scope
= queryParameters
.getScope();
41 if (scope
instanceof GlobalSearchScope
) {
42 return processAllClassesInGlobalScope((GlobalSearchScope
)scope
, consumer
, queryParameters
);
45 PsiElement
[] scopeRoots
= ((LocalSearchScope
)scope
).getScope();
46 for (final PsiElement scopeRoot
: scopeRoots
) {
47 if (!processScopeRootForAllClasses(scopeRoot
, consumer
)) return false;
52 private static boolean processAllClassesInGlobalScope(final GlobalSearchScope scope
, final Processor
<PsiClass
> processor
, AllClassesSearch
.SearchParameters parameters
) {
53 final PsiManager manager
= PsiManager
.getInstance(parameters
.getProject());
55 final JavaPsiFacade facade
= JavaPsiFacade
.getInstance(manager
.getProject());
56 final PsiShortNamesCache cache
= facade
.getShortNamesCache();
58 final String
[] names
= ApplicationManager
.getApplication().runReadAction(new Computable
<String
[]>() {
59 public String
[] compute() {
60 return cache
.getAllClassNames();
63 Arrays
.sort(names
, new Comparator
<String
>() {
64 public int compare(final String o1
, final String o2
) {
65 return o1
.compareToIgnoreCase(o2
);
69 for (final String name
: names
) {
70 if (!parameters
.nameMatches(name
)) continue;
72 ProgressManager
.getInstance().checkCanceled();
73 final PsiClass
[] classes
= ApplicationManager
.getApplication().runReadAction(new Computable
<PsiClass
[]>() {
74 public PsiClass
[] compute() {
75 return cache
.getClassesByName(name
, scope
);
78 for (PsiClass psiClass
: classes
) {
79 ProgressManager
.getInstance().checkCanceled();
80 if (!processor
.process(psiClass
)) {
88 private static boolean processScopeRootForAllClasses(PsiElement scopeRoot
, final Processor
<PsiClass
> processor
) {
89 if (scopeRoot
== null) return true;
90 final boolean[] stopped
= new boolean[]{false};
92 scopeRoot
.accept(new JavaRecursiveElementWalkingVisitor() {
94 public void visitElement(PsiElement element
) {
96 super.visitElement(element
);
100 @Override public void visitClass(PsiClass aClass
) {
101 stopped
[0] = !processor
.process(aClass
);
102 super.visitClass(aClass
);