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.
21 package com
.intellij
.analysis
;
23 import com
.intellij
.openapi
.application
.ApplicationManager
;
24 import com
.intellij
.openapi
.module
.Module
;
25 import com
.intellij
.openapi
.project
.Project
;
26 import com
.intellij
.openapi
.roots
.ProjectFileIndex
;
27 import com
.intellij
.openapi
.roots
.ProjectRootManager
;
28 import com
.intellij
.openapi
.vfs
.VirtualFile
;
29 import com
.intellij
.psi
.*;
30 import com
.intellij
.psi
.search
.GlobalSearchScope
;
31 import com
.intellij
.psi
.search
.SearchScope
;
32 import com
.intellij
.psi
.search
.PackageScope
;
33 import org
.jetbrains
.annotations
.NotNull
;
35 import java
.util
.Arrays
;
36 import java
.util
.HashSet
;
39 public class JavaAnalysisScope
extends AnalysisScope
{
40 public static final int PACKAGE
= 5;
42 public JavaAnalysisScope(PsiPackage pack
, Module module
) {
43 super(pack
.getProject());
49 public JavaAnalysisScope(final PsiJavaFile psiFile
) {
53 public AnalysisScope
[] getNarrowedComplementaryScope(Project defaultProject
) {
54 final ProjectFileIndex fileIndex
= ProjectRootManager
.getInstance(defaultProject
).getFileIndex();
55 final HashSet
<Module
> modules
= new HashSet
<Module
>();
57 if (myElement
instanceof PsiJavaFile
&& !JspPsiUtil
.isInJspFile(myElement
)) {
58 PsiJavaFile psiJavaFile
= (PsiJavaFile
)myElement
;
59 final PsiClass
[] classes
= psiJavaFile
.getClasses();
60 boolean onlyPackLocalClasses
= true;
61 for (final PsiClass aClass
: classes
) {
62 if (aClass
.hasModifierProperty(PsiModifier
.PUBLIC
)) {
63 onlyPackLocalClasses
= false;
66 if (onlyPackLocalClasses
) {
67 final PsiDirectory psiDirectory
= psiJavaFile
.getContainingDirectory();
68 if (psiDirectory
!= null) {
69 return new AnalysisScope
[]{new JavaAnalysisScope(JavaDirectoryService
.getInstance().getPackage(psiDirectory
), null)};
74 else if (myType
== PACKAGE
) {
75 final PsiDirectory
[] directories
= ((PsiPackage
)myElement
).getDirectories();
76 for (PsiDirectory directory
: directories
) {
77 modules
.addAll(getAllInterestingModules(fileIndex
, directory
.getVirtualFile()));
79 return collectScopes(defaultProject
, modules
);
81 return super.getNarrowedComplementaryScope(defaultProject
);
86 public String
getShortenName() {
87 if (myType
== PACKAGE
)
88 return AnalysisScopeBundle
.message("scope.package", ((PsiPackage
)myElement
).getQualifiedName());
89 return super.getShortenName();
92 public String
getDisplayName() {
93 if (myType
== PACKAGE
) {
94 return AnalysisScopeBundle
.message("scope.package", ((PsiPackage
)myElement
).getQualifiedName());
96 return super.getDisplayName();
99 protected void initFilesSet() {
100 if (myType
== PACKAGE
) {
101 myFilesSet
= new HashSet
<VirtualFile
>();
102 accept(createFileSearcher());
105 super.initFilesSet();
108 protected void accept(final PsiElementVisitor visitor
, final boolean needReadAction
) {
109 if (myElement
instanceof PsiPackage
) {
110 final PsiPackage pack
= (PsiPackage
)myElement
;
111 final Set
<PsiDirectory
> dirs
= new HashSet
<PsiDirectory
>();
112 ApplicationManager
.getApplication().runReadAction(new Runnable() {
114 dirs
.addAll(Arrays
.asList(pack
.getDirectories(GlobalSearchScope
.projectScope(myElement
.getProject()))));
117 for (PsiDirectory dir
: dirs
) {
118 accept(dir
, visitor
, needReadAction
);
121 super.accept(visitor
, needReadAction
);
127 public SearchScope
toSearchScope() {
128 if (myType
== PACKAGE
) {
129 return new PackageScope((PsiPackage
)myElement
, true, true);
131 return super.toSearchScope();