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