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