001/*
002 * Copyright 2010-2013 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
017package org.jetbrains.jet.lang.resolve.java.scope;
018
019import com.google.common.collect.Sets;
020import com.intellij.openapi.progress.ProgressIndicatorProvider;
021import com.intellij.psi.PsiClass;
022import com.intellij.psi.PsiModifier;
023import com.intellij.psi.PsiPackage;
024import org.jetbrains.annotations.NotNull;
025import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
026import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
027import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
028import org.jetbrains.jet.lang.resolve.java.*;
029import org.jetbrains.jet.lang.resolve.name.FqName;
030
031import java.util.Collection;
032
033public final class ScopeUtils {
034    private ScopeUtils() {
035    }
036
037    @NotNull
038    public static Collection<DeclarationDescriptor> computeAllPackageDeclarations(
039            PsiPackage psiPackage,
040            JavaSemanticServices javaSemanticServices
041    ) {
042        Collection<DeclarationDescriptor> result = Sets.newHashSet();
043        JavaDescriptorResolver descriptorResolver = javaSemanticServices.getDescriptorResolver();
044
045        for (PsiPackage psiSubPackage : psiPackage.getSubPackages()) {
046            NamespaceDescriptor childNs = descriptorResolver.resolveNamespace(
047                    new FqName(psiSubPackage.getQualifiedName()), DescriptorSearchRule.IGNORE_IF_FOUND_IN_KOTLIN);
048            if (childNs != null) {
049                result.add(childNs);
050            }
051        }
052
053        for (PsiClass psiClass : javaSemanticServices.getPsiClassFinder().findPsiClasses(psiPackage)) {
054            if (PackageClassUtils.isPackageClass(psiClass)) {
055                continue;
056            }
057
058            if (psiClass instanceof JetJavaMirrorMarker) {
059                continue;
060            }
061
062            if (psiClass.hasModifierProperty(PsiModifier.PUBLIC)) {
063                ProgressIndicatorProvider.checkCanceled();
064                FqName fqName = new FqName(psiClass.getQualifiedName());
065                ClassDescriptor classDescriptor = descriptorResolver.resolveClass(fqName, DescriptorSearchRule.IGNORE_IF_FOUND_IN_KOTLIN);
066                if (classDescriptor != null) {
067                    result.add(classDescriptor);
068                }
069
070                NamespaceDescriptor namespaceDescriptor = descriptorResolver.resolveNamespace(
071                        fqName, DescriptorSearchRule.IGNORE_IF_FOUND_IN_KOTLIN);
072                if (namespaceDescriptor != null) {
073                    result.add(namespaceDescriptor);
074                }
075            }
076        }
077        return result;
078    }
079
080}