001    /*
002     * Copyright 2010-2015 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.kotlin.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.kotlin.descriptors.*;
023    import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor;
024    import org.jetbrains.kotlin.descriptors.annotations.Annotations;
025    import org.jetbrains.kotlin.resolve.scopes.JetScope;
026    import org.jetbrains.kotlin.types.JetType;
027    import org.jetbrains.kotlin.types.TypeConstructor;
028    import org.jetbrains.kotlin.types.TypeProjection;
029    import org.jetbrains.kotlin.types.TypesPackage;
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            doForceResolveAllContents(descriptor);
040            return 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            doForceResolveAllContents(typeConstructor);
061        }
062    
063        public static void forceResolveAllContents(@NotNull Annotations annotations) {
064            doForceResolveAllContents(annotations);
065            for (AnnotationDescriptor annotation : annotations) {
066                doForceResolveAllContents(annotation);
067            }
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                ReceiverParameterDescriptor parameter = callableDescriptor.getExtensionReceiverParameter();
078                if (parameter != null) {
079                    forceResolveAllContents(parameter.getType());
080                }
081                for (ValueParameterDescriptor parameterDescriptor : callableDescriptor.getValueParameters()) {
082                    forceResolveAllContents(parameterDescriptor);
083                }
084                for (TypeParameterDescriptor typeParameterDescriptor : callableDescriptor.getTypeParameters()) {
085                    forceResolveAllContents(typeParameterDescriptor.getUpperBounds());
086                }
087                forceResolveAllContents(callableDescriptor.getReturnType());
088            }
089        }
090    
091        @Nullable
092        public static JetType forceResolveAllContents(@Nullable JetType type) {
093            if (type == null) return null;
094    
095            if (TypesPackage.isFlexible(type)) {
096                forceResolveAllContents(TypesPackage.flexibility(type).getLowerBound());
097                forceResolveAllContents(TypesPackage.flexibility(type).getUpperBound());
098            }
099            else {
100                forceResolveAllContents(type.getConstructor());
101                for (TypeProjection projection : type.getArguments()) {
102                    forceResolveAllContents(projection.getType());
103                }
104            }
105            return type;
106        }
107    }