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.resolve.scopes;
018    
019    import com.google.common.base.Predicate;
020    import com.google.common.collect.Collections2;
021    import org.jetbrains.annotations.NotNull;
022    import org.jetbrains.annotations.Nullable;
023    import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
024    import org.jetbrains.jet.lang.descriptors.ClassifierDescriptor;
025    import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
026    import org.jetbrains.jet.lang.descriptors.ReceiverParameterDescriptor;
027    import org.jetbrains.jet.lang.resolve.name.LabelName;
028    import org.jetbrains.jet.lang.resolve.name.Name;
029    
030    import java.util.Collection;
031    import java.util.Collections;
032    import java.util.List;
033    
034    public class InnerClassesScopeWrapper extends AbstractScopeAdapter {
035        private final JetScope actualScope;
036    
037        public InnerClassesScopeWrapper(JetScope actualScope) {
038            this.actualScope = actualScope;
039        }
040    
041        @NotNull
042        @Override
043        protected JetScope getWorkerScope() {
044            return actualScope;
045        }
046    
047        @Override
048        public ClassifierDescriptor getClassifier(@NotNull Name name) {
049            ClassifierDescriptor classifier = actualScope.getClassifier(name);
050            if (isClass(classifier)) return classifier;
051            return null;
052        }
053    
054        @NotNull
055        @Override
056        public Collection<DeclarationDescriptor> getDeclarationsByLabel(LabelName labelName) {
057            Collection<DeclarationDescriptor> declarationsByLabel = actualScope.getDeclarationsByLabel(labelName);
058            return Collections2.filter(declarationsByLabel, new Predicate<DeclarationDescriptor>() {
059                @Override
060                public boolean apply(@Nullable DeclarationDescriptor descriptor) {
061                    return isClass(descriptor);
062                }
063            });
064        }
065    
066        @NotNull
067        @Override
068        public Collection<DeclarationDescriptor> getAllDescriptors() {
069            Collection<DeclarationDescriptor> allDescriptors = actualScope.getAllDescriptors();
070            return Collections2.filter(allDescriptors, new Predicate<DeclarationDescriptor>() {
071                @Override
072                public boolean apply(@Nullable DeclarationDescriptor descriptor) {
073                    return isClass(descriptor);
074                }
075            });
076        }
077    
078        @NotNull
079        @Override
080        public List<ReceiverParameterDescriptor> getImplicitReceiversHierarchy() {
081            return Collections.emptyList();
082        }
083    
084        @Override
085        public String toString() {
086            return "Classes from " + actualScope;
087        }
088    
089        private static boolean isClass(DeclarationDescriptor descriptor) {
090            return descriptor instanceof ClassDescriptor && !((ClassDescriptor) descriptor).getKind().isObject();
091        }
092    }