001    /*
002     * Copyright 2010-2015 JetBrains s.r.o.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.jetbrains.kotlin.asJava;
018    
019    import com.google.common.base.Function;
020    import com.google.common.collect.Collections2;
021    import com.google.common.collect.Sets;
022    import com.intellij.openapi.extensions.Extensions;
023    import com.intellij.openapi.project.Project;
024    import com.intellij.openapi.util.Condition;
025    import com.intellij.openapi.vfs.VirtualFile;
026    import com.intellij.psi.*;
027    import com.intellij.psi.search.GlobalSearchScope;
028    import com.intellij.psi.util.PsiUtilCore;
029    import com.intellij.util.SmartList;
030    import com.intellij.util.containers.ContainerUtil;
031    import org.jetbrains.annotations.NotNull;
032    import org.jetbrains.annotations.Nullable;
033    import org.jetbrains.kotlin.load.java.JvmAbi;
034    import org.jetbrains.kotlin.name.FqName;
035    import org.jetbrains.kotlin.name.FqNamesUtilKt;
036    import org.jetbrains.kotlin.psi.KtClass;
037    import org.jetbrains.kotlin.psi.KtClassOrObject;
038    import org.jetbrains.kotlin.psi.KtEnumEntry;
039    import org.jetbrains.kotlin.psi.KtFile;
040    import org.jetbrains.kotlin.resolve.jvm.KotlinFinderMarker;
041    
042    import java.util.Collection;
043    import java.util.Comparator;
044    import java.util.List;
045    import java.util.Set;
046    
047    import static org.jetbrains.kotlin.asJava.LightClassUtilsKt.toLightClass;
048    
049    public class JavaElementFinder extends PsiElementFinder implements KotlinFinderMarker {
050    
051        @NotNull
052        public static JavaElementFinder getInstance(@NotNull Project project) {
053            PsiElementFinder[] extensions = Extensions.getArea(project).getExtensionPoint(PsiElementFinder.EP_NAME).getExtensions();
054            for (PsiElementFinder extension : extensions) {
055                if (extension instanceof JavaElementFinder) {
056                    return (JavaElementFinder) extension;
057                }
058            }
059            throw new IllegalStateException(JavaElementFinder.class.getSimpleName() + " is not found for project " + project);
060        }
061    
062        private final Project project;
063        private final PsiManager psiManager;
064        private final LightClassGenerationSupport lightClassGenerationSupport;
065    
066        public JavaElementFinder(
067                @NotNull Project project,
068                @NotNull LightClassGenerationSupport lightClassGenerationSupport
069        ) {
070            this.project = project;
071            this.psiManager = PsiManager.getInstance(project);
072            this.lightClassGenerationSupport = lightClassGenerationSupport;
073        }
074    
075        @Override
076        public PsiClass findClass(@NotNull String qualifiedName, @NotNull GlobalSearchScope scope) {
077            PsiClass[] allClasses = findClasses(qualifiedName, scope);
078            return allClasses.length > 0 ? allClasses[0] : null;
079        }
080    
081        @NotNull
082        @Override
083        public PsiClass[] findClasses(@NotNull String qualifiedNameString, @NotNull GlobalSearchScope scope) {
084            if (!FqNamesUtilKt.isValidJavaFqName(qualifiedNameString)) {
085                return PsiClass.EMPTY_ARRAY;
086            }
087    
088            List<PsiClass> answer = new SmartList<PsiClass>();
089    
090            FqName qualifiedName = new FqName(qualifiedNameString);
091    
092            findClassesAndObjects(qualifiedName, scope, answer);
093    
094            answer.addAll(lightClassGenerationSupport.getFacadeClasses(qualifiedName, scope));
095            answer.addAll(lightClassGenerationSupport.getMultifilePartClasses(qualifiedName, scope));
096    
097            return sortByClasspath(answer, scope).toArray(new PsiClass[answer.size()]);
098        }
099    
100        // Finds explicitly declared classes and objects, not package classes
101        // Also DefaultImpls classes of interfaces
102        private void findClassesAndObjects(FqName qualifiedName, GlobalSearchScope scope, List<PsiClass> answer) {
103            findInterfaceDefaultImpls(qualifiedName, scope, answer);
104    
105            Collection<KtClassOrObject> classOrObjectDeclarations =
106                    lightClassGenerationSupport.findClassOrObjectDeclarations(qualifiedName, scope);
107    
108            for (KtClassOrObject declaration : classOrObjectDeclarations) {
109                if (!(declaration instanceof KtEnumEntry)) {
110                    PsiClass lightClass = toLightClass(declaration);
111                    if (lightClass != null) {
112                        answer.add(lightClass);
113                    }
114                }
115            }
116        }
117    
118        private void findInterfaceDefaultImpls(FqName qualifiedName, GlobalSearchScope scope, List<PsiClass> answer) {
119            if (qualifiedName.isRoot()) return;
120    
121            if (!qualifiedName.shortName().asString().equals(JvmAbi.DEFAULT_IMPLS_CLASS_NAME)) return;
122    
123            for (KtClassOrObject classOrObject : lightClassGenerationSupport.findClassOrObjectDeclarations(qualifiedName.parent(), scope)) {
124                //NOTE: can't filter out more interfaces right away because decompiled declarations do not have member bodies
125                if (classOrObject instanceof KtClass && ((KtClass) classOrObject).isInterface()) {
126                    PsiClass interfaceClass = toLightClass(classOrObject);
127                    if (interfaceClass != null) {
128                        PsiClass implsClass = interfaceClass.findInnerClassByName(JvmAbi.DEFAULT_IMPLS_CLASS_NAME, false);
129                        if (implsClass != null) {
130                            answer.add(implsClass);
131                        }
132                    }
133                }
134            }
135        }
136    
137        @NotNull
138        @Override
139        public Set<String> getClassNames(@NotNull PsiPackage psiPackage, @NotNull GlobalSearchScope scope) {
140            FqName packageFQN = new FqName(psiPackage.getQualifiedName());
141    
142            Collection<KtClassOrObject> declarations = lightClassGenerationSupport.findClassOrObjectDeclarationsInPackage(packageFQN, scope);
143    
144            Set<String> answer = Sets.newHashSet();
145            answer.addAll(lightClassGenerationSupport.getFacadeNames(packageFQN, scope));
146    
147            for (KtClassOrObject declaration : declarations) {
148                String name = declaration.getName();
149                if (name != null) {
150                    answer.add(name);
151                }
152            }
153    
154            return answer;
155        }
156    
157        @Override
158        public PsiPackage findPackage(@NotNull String qualifiedNameString) {
159            if (!FqNamesUtilKt.isValidJavaFqName(qualifiedNameString)) {
160                return null;
161            }
162    
163            FqName fqName = new FqName(qualifiedNameString);
164    
165            // allScope() because the contract says that the whole project
166            GlobalSearchScope allScope = GlobalSearchScope.allScope(project);
167            if (lightClassGenerationSupport.packageExists(fqName, allScope)) {
168                return new KtLightPackage(psiManager, fqName, allScope);
169            }
170    
171            return null;
172        }
173    
174        @NotNull
175        @Override
176        public PsiPackage[] getSubPackages(@NotNull PsiPackage psiPackage, @NotNull final GlobalSearchScope scope) {
177            FqName packageFQN = new FqName(psiPackage.getQualifiedName());
178    
179            Collection<FqName> subpackages = lightClassGenerationSupport.getSubPackages(packageFQN, scope);
180    
181            Collection<PsiPackage> answer = Collections2.transform(subpackages, new Function<FqName, PsiPackage>() {
182                @Override
183                public PsiPackage apply(@Nullable FqName input) {
184                    return new KtLightPackage(psiManager, input, scope);
185                }
186            });
187    
188            return answer.toArray(new PsiPackage[answer.size()]);
189        }
190    
191        @NotNull
192        @Override
193        public PsiClass[] getClasses(@NotNull PsiPackage psiPackage, @NotNull GlobalSearchScope scope) {
194            List<PsiClass> answer = new SmartList<PsiClass>();
195            FqName packageFQN = new FqName(psiPackage.getQualifiedName());
196    
197            answer.addAll(lightClassGenerationSupport.getFacadeClassesInPackage(packageFQN, scope));
198    
199            Collection<KtClassOrObject> declarations = lightClassGenerationSupport.findClassOrObjectDeclarationsInPackage(packageFQN, scope);
200            for (KtClassOrObject declaration : declarations) {
201                PsiClass aClass = toLightClass(declaration);
202                if (aClass != null) {
203                    answer.add(aClass);
204                }
205            }
206    
207            return sortByClasspath(answer, scope).toArray(new PsiClass[answer.size()]);
208        }
209    
210        @Override
211        @NotNull
212        public PsiFile[] getPackageFiles(@NotNull PsiPackage psiPackage, @NotNull GlobalSearchScope scope) {
213            FqName packageFQN = new FqName(psiPackage.getQualifiedName());
214            Collection<KtFile> result = lightClassGenerationSupport.findFilesForPackage(packageFQN, scope);
215            return result.toArray(new PsiFile[result.size()]);
216        }
217    
218        @Override
219        @Nullable
220        public Condition<PsiFile> getPackageFilesFilter(@NotNull final PsiPackage psiPackage, @NotNull GlobalSearchScope scope) {
221            return new Condition<PsiFile>() {
222                @Override
223                public boolean value(PsiFile input) {
224                    if (!(input instanceof KtFile)) {
225                        return true;
226                    }
227                    return psiPackage.getQualifiedName().equals(((KtFile) input).getPackageFqName().asString());
228                }
229            };
230        }
231    
232        @NotNull
233        public static Comparator<PsiElement> byClasspathComparator(@NotNull final GlobalSearchScope searchScope) {
234            return new Comparator<PsiElement>() {
235                @Override
236                public int compare(@NotNull PsiElement o1, @NotNull PsiElement o2) {
237                    VirtualFile f1 = PsiUtilCore.getVirtualFile(o1);
238                    VirtualFile f2 = PsiUtilCore.getVirtualFile(o2);
239                    if (f1 == f2) return 0;
240                    if (f1 == null) return -1;
241                    if (f2 == null) return 1;
242                    return searchScope.compare(f2, f1);
243                }
244            };
245        }
246    
247        private static Collection<PsiClass> sortByClasspath(@NotNull List<PsiClass> classes, @NotNull GlobalSearchScope searchScope) {
248            if (classes.size() > 1) {
249                ContainerUtil.quickSort(classes, byClasspathComparator(searchScope));
250            }
251    
252            return classes;
253        }
254    }