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.resolve.lazy;
018    
019    import com.intellij.openapi.diagnostic.Logger;
020    import org.jetbrains.annotations.NotNull;
021    import org.jetbrains.annotations.Nullable;
022    import org.jetbrains.jet.lang.descriptors.CallableDescriptor;
023    import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
024    import org.jetbrains.jet.lang.descriptors.annotations.Annotations;
025    import org.jetbrains.jet.lang.resolve.scopes.JetScope;
026    import org.jetbrains.jet.lang.types.JetType;
027    import org.jetbrains.jet.lang.types.TypeConstructor;
028    import org.jetbrains.jet.lang.types.TypeProjection;
029    
030    import java.util.Collection;
031    
032    public class ForceResolveUtil {
033        private static final Logger LOG = Logger.getInstance(ForceResolveUtil.class);
034    
035        private ForceResolveUtil() {}
036    
037        public static void forceResolveAllContents(@NotNull DeclarationDescriptor descriptor) {
038            LOG.debug("descriptor: " + descriptor);
039            doForceResolveAllContents(descriptor);
040            LOG.debug("<<< " + descriptor);
041        }
042    
043        public static void forceResolveAllContents(@NotNull JetScope scope) {
044            forceResolveAllContents(scope.getAllDescriptors());
045        }
046    
047        public static void forceResolveAllContents(@NotNull Iterable<? extends DeclarationDescriptor> descriptors) {
048            for (DeclarationDescriptor descriptor : descriptors) {
049                forceResolveAllContents(descriptor);
050            }
051        }
052    
053        public static void forceResolveAllContents(@NotNull Collection<JetType> types) {
054            for (JetType type : types) {
055                forceResolveAllContents(type);
056            }
057        }
058    
059        public static void forceResolveAllContents(@NotNull TypeConstructor typeConstructor) {
060            LOG.debug("descriptor: " + typeConstructor);
061            doForceResolveAllContents(typeConstructor);
062            LOG.debug("<<< " + typeConstructor);
063        }
064    
065        public static void forceResolveAllContents(@NotNull Annotations annotations) {
066            doForceResolveAllContents(annotations);
067        }
068    
069        private static void doForceResolveAllContents(Object object) {
070            if (object instanceof LazyEntity) {
071                LazyEntity lazyEntity = (LazyEntity) object;
072                lazyEntity.forceResolveAllContents();
073            }
074            else if (object instanceof CallableDescriptor) {
075                CallableDescriptor callableDescriptor = (CallableDescriptor) object;
076                forceResolveAllContents(callableDescriptor.getReturnType());
077            }
078        }
079    
080        public static void forceResolveAllContents(@Nullable JetType type) {
081            if (type == null) return;
082    
083            forceResolveAllContents(type.getConstructor());
084            for (TypeProjection projection : type.getArguments()) {
085                forceResolveAllContents(projection.getType());
086            }
087        }
088    }