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    
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.DeclarationDescriptorVisitor;
023    import org.jetbrains.jet.lang.descriptors.ModuleDescriptor;
024    import org.jetbrains.jet.lang.descriptors.PackageFragmentDescriptor;
025    import org.jetbrains.jet.lang.descriptors.annotations.Annotations;
026    import org.jetbrains.jet.lang.resolve.name.FqName;
027    import org.jetbrains.jet.lang.resolve.scopes.JetScope;
028    import org.jetbrains.jet.lang.resolve.scopes.RedeclarationHandler;
029    import org.jetbrains.jet.lang.resolve.scopes.WritableScope;
030    import org.jetbrains.jet.lang.resolve.scopes.WritableScopeImpl;
031    import org.jetbrains.jet.lang.types.TypeSubstitutor;
032    
033    public class MutablePackageFragmentDescriptor extends DeclarationDescriptorImpl implements PackageFragmentDescriptor {
034    
035        private final ModuleDescriptor module;
036        private final FqName fqName;
037        private final WritableScope scope;
038        private final PackageLikeBuilder builder;
039    
040        public MutablePackageFragmentDescriptor(@NotNull ModuleDescriptor module, @NotNull FqName fqName) {
041            super(Annotations.EMPTY, fqName.shortNameOrSpecial());
042            this.module = module;
043            this.fqName = fqName;
044    
045            scope = new WritableScopeImpl(JetScope.EMPTY, this, RedeclarationHandler.DO_NOTHING, "Members of " + fqName + " in " + module);
046            scope.changeLockLevel(WritableScope.LockLevel.BOTH);
047            builder = new ScopeBasedPackageLikeBuilder(this, scope);
048        }
049    
050        @NotNull
051        @Override
052        public ModuleDescriptor getContainingDeclaration() {
053            return module;
054        }
055    
056        @NotNull
057        @Override
058        public FqName getFqName() {
059            return fqName;
060        }
061    
062        @NotNull
063        @Override
064        public WritableScope getMemberScope() {
065            return scope;
066        }
067    
068        @Nullable
069        @Override
070        public DeclarationDescriptor substitute(@NotNull TypeSubstitutor substitutor) {
071            return this;
072        }
073    
074        @Override
075        public <R, D> R accept(DeclarationDescriptorVisitor<R, D> visitor, D data) {
076            return visitor.visitPackageFragmentDescriptor(this, data);
077        }
078    
079        @NotNull
080        public PackageLikeBuilder getBuilder() {
081            return builder;
082        }
083    }