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.java.scope;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.annotations.Nullable;
021    import org.jetbrains.jet.lang.descriptors.*;
022    import org.jetbrains.jet.lang.resolve.java.resolver.JavaMemberResolver;
023    import org.jetbrains.jet.lang.resolve.java.resolver.ProgressChecker;
024    import org.jetbrains.jet.lang.resolve.name.Name;
025    import org.jetbrains.jet.lang.resolve.scopes.JetScopeImpl;
026    
027    import java.util.*;
028    
029    public abstract class JavaBaseScope extends JetScopeImpl {
030        @NotNull
031        protected final JavaMemberResolver memberResolver;
032        @NotNull
033        protected final MembersProvider membersProvider;
034        @NotNull
035        private final Map<Name, Set<FunctionDescriptor>> functionDescriptors = new HashMap<Name, Set<FunctionDescriptor>>();
036        @NotNull
037        private final Map<Name, Set<VariableDescriptor>> propertyDescriptors = new HashMap<Name, Set<VariableDescriptor>>();
038        @Nullable
039        private Collection<DeclarationDescriptor> allDescriptors = null;
040        @Nullable
041        private Set<ClassDescriptor> objectDescriptors = null;
042        @NotNull
043        protected final ClassOrNamespaceDescriptor descriptor;
044    
045        private Collection<ClassDescriptor> innerClasses = null;
046    
047    
048        protected JavaBaseScope(
049                @NotNull ClassOrNamespaceDescriptor descriptor,
050                @NotNull JavaMemberResolver memberResolver,
051                @NotNull MembersProvider membersProvider
052        ) {
053            this.memberResolver = memberResolver;
054            this.membersProvider = membersProvider;
055            this.descriptor = descriptor;
056        }
057    
058        @NotNull
059        @Override
060        public DeclarationDescriptor getContainingDeclaration() {
061            return descriptor;
062        }
063    
064        @NotNull
065        @Override
066        public Collection<VariableDescriptor> getProperties(@NotNull Name name) {
067            Set<VariableDescriptor> cached = propertyDescriptors.get(name);
068            if (cached != null) return cached;
069    
070            if (allDescriptorsComputed()) {
071                return Collections.emptySet();
072            }
073    
074            Set<VariableDescriptor> computedDescriptors = computePropertyDescriptors(name);
075            propertyDescriptors.put(name, computedDescriptors);
076            return computedDescriptors;
077        }
078    
079        @NotNull
080        private Set<VariableDescriptor> computePropertyDescriptors(@NotNull Name name) {
081            NamedMembers members = membersProvider.get(name);
082            if (members == null) {
083                return Collections.emptySet();
084            }
085            return memberResolver.resolveFieldGroup(members, descriptor);
086        }
087    
088        @NotNull
089        @Override
090        public Collection<FunctionDescriptor> getFunctions(@NotNull Name name) {
091            Set<FunctionDescriptor> cached = functionDescriptors.get(name);
092            if (cached != null) return cached;
093    
094            if (allDescriptorsComputed()) {
095                return Collections.emptySet();
096            }
097    
098            Set<FunctionDescriptor> computedDescriptors = computeFunctionDescriptor(name);
099            functionDescriptors.put(name, computedDescriptors);
100            return computedDescriptors;
101        }
102    
103        @NotNull
104        protected abstract Set<FunctionDescriptor> computeFunctionDescriptor(@NotNull Name name);
105    
106        @NotNull
107        @Override
108        public Collection<DeclarationDescriptor> getAllDescriptors() {
109            if (allDescriptorsComputed()) {
110                return allDescriptors;
111            }
112    
113            allDescriptors = computeAllDescriptors();
114    
115            return allDescriptors;
116        }
117    
118        private boolean allDescriptorsComputed() {
119            return allDescriptors != null;
120        }
121    
122        @NotNull
123        protected Collection<DeclarationDescriptor> computeAllDescriptors() {
124            Collection<DeclarationDescriptor> result = new HashSet<DeclarationDescriptor>();
125            result.addAll(computeFieldAndFunctionDescriptors());
126            result.addAll(filterObjects(getInnerClasses(), false));
127            return result;
128        }
129    
130        @NotNull
131        @Override
132        public Set<ClassDescriptor> getObjectDescriptors() {
133            if (objectDescriptors == null) {
134                objectDescriptors = new HashSet<ClassDescriptor>(filterObjects(getInnerClasses(), true));
135            }
136            return objectDescriptors;
137        }
138    
139        @NotNull
140        protected abstract Collection<ClassDescriptor> computeInnerClasses();
141    
142        @NotNull
143        private Collection<DeclarationDescriptor> computeFieldAndFunctionDescriptors() {
144            Collection<DeclarationDescriptor> result = new ArrayList<DeclarationDescriptor>();
145            for (NamedMembers members : membersProvider.allMembers()) {
146                Name name = members.getName();
147                ProgressChecker.getInstance().checkCanceled();
148                result.addAll(getFunctions(name));
149                ProgressChecker.getInstance().checkCanceled();
150                result.addAll(getProperties(name));
151            }
152            return result;
153        }
154    
155        @NotNull
156        protected Collection<ClassDescriptor> getInnerClasses() {
157            if (innerClasses == null) {
158                innerClasses = computeInnerClasses();
159            }
160            return innerClasses;
161        }
162    
163        @NotNull
164        private static Collection<ClassDescriptor> filterObjects(@NotNull Collection<ClassDescriptor> classes, boolean objects) {
165            List<ClassDescriptor> result = new ArrayList<ClassDescriptor>();
166            for (ClassDescriptor descriptor : classes) {
167                if (descriptor.getKind().isObject() == objects) {
168                    result.add(descriptor);
169                }
170            }
171            return result;
172        }
173    }