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 <T extends DeclarationDescriptor> T forceResolveAllContents(@NotNull T descriptor) {
038            LOG.debug("descriptor: " + descriptor);
039            doForceResolveAllContents(descriptor);
040            LOG.debug("<<< " + descriptor);
041            return descriptor;
042        }
043    
044        public static void forceResolveAllContents(@NotNull JetScope scope) {
045            forceResolveAllContents(scope.getAllDescriptors());
046        }
047    
048        public static void forceResolveAllContents(@NotNull Iterable<? extends DeclarationDescriptor> descriptors) {
049            for (DeclarationDescriptor descriptor : descriptors) {
050                forceResolveAllContents(descriptor);
051            }
052        }
053    
054        public static void forceResolveAllContents(@NotNull Collection<JetType> types) {
055            for (JetType type : types) {
056                forceResolveAllContents(type);
057            }
058        }
059    
060        public static void forceResolveAllContents(@NotNull TypeConstructor typeConstructor) {
061            LOG.debug("descriptor: " + typeConstructor);
062            doForceResolveAllContents(typeConstructor);
063            LOG.debug("<<< " + typeConstructor);
064        }
065    
066        public static void forceResolveAllContents(@NotNull Annotations annotations) {
067            doForceResolveAllContents(annotations);
068        }
069    
070        private static void doForceResolveAllContents(Object object) {
071            if (object instanceof LazyEntity) {
072                LazyEntity lazyEntity = (LazyEntity) object;
073                lazyEntity.forceResolveAllContents();
074            }
075            else if (object instanceof CallableDescriptor) {
076                CallableDescriptor callableDescriptor = (CallableDescriptor) object;
077                forceResolveAllContents(callableDescriptor.getReturnType());
078            }
079        }
080    
081        public static void forceResolveAllContents(@Nullable JetType type) {
082            if (type == null) return;
083    
084            forceResolveAllContents(type.getConstructor());
085            for (TypeProjection projection : type.getArguments()) {
086                forceResolveAllContents(projection.getType());
087            }
088        }
089    }