001 /*
002 * Copyright 2010-2014 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.jet.lang.descriptors.impl;
018
019 import org.jetbrains.annotations.NotNull;
020 import org.jetbrains.annotations.Nullable;
021 import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
022 import org.jetbrains.jet.lang.descriptors.PackageViewDescriptor;
023 import org.jetbrains.jet.lang.resolve.name.FqName;
024 import org.jetbrains.jet.lang.resolve.name.Name;
025 import org.jetbrains.jet.lang.resolve.scopes.JetScopeImpl;
026 import org.jetbrains.jet.utils.Printer;
027 import org.jetbrains.jet.utils.UtilsPackage;
028
029 import java.util.ArrayList;
030 import java.util.Collection;
031 import java.util.List;
032
033 public class SubpackagesScope extends JetScopeImpl {
034 private final PackageViewDescriptor packageView;
035
036 public SubpackagesScope(PackageViewDescriptor packageView) {
037 this.packageView = packageView;
038
039 }
040
041 @NotNull
042 @Override
043 public DeclarationDescriptor getContainingDeclaration() {
044 return packageView;
045 }
046
047 @Nullable
048 @Override
049 public PackageViewDescriptor getPackage(@NotNull Name name) {
050 return name.isSpecial() ? null : packageView.getModule().getPackage(packageView.getFqName().child(name));
051 }
052
053 @NotNull
054 @Override
055 public Collection<DeclarationDescriptor> getAllDescriptors() {
056 Collection<FqName> subFqNames = packageView.getModule().getPackageFragmentProvider().getSubPackagesOf(packageView.getFqName());
057 List<DeclarationDescriptor> result = new ArrayList<DeclarationDescriptor>(subFqNames.size());
058 for (FqName subFqName : subFqNames) {
059 UtilsPackage.addIfNotNull(result, getPackage(subFqName.shortName()));
060 }
061 return result;
062 }
063
064 @Override
065 public void printScopeStructure(@NotNull Printer p) {
066 p.println(getClass().getSimpleName(), " {");
067 p.pushIndent();
068
069 p.println("thisDescriptor = ", packageView);
070
071 p.popIndent();
072 p.println("}");
073 }
074 }