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.AnnotationWithTarget;
025    import org.jetbrains.kotlin.descriptors.annotations.Annotations;
026    import org.jetbrains.kotlin.resolve.scopes.JetScope;
027    import org.jetbrains.kotlin.types.JetType;
028    import org.jetbrains.kotlin.types.TypeConstructor;
029    import org.jetbrains.kotlin.types.TypeProjection;
030    import org.jetbrains.kotlin.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> T forceResolveAllContents(@NotNull T descriptor) {
040            doForceResolveAllContents(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            doForceResolveAllContents(typeConstructor);
062        }
063    
064        public static void forceResolveAllContents(@NotNull Annotations annotations) {
065            doForceResolveAllContents(annotations);
066            for (AnnotationWithTarget annotationWithTarget : annotations.getAllAnnotations()) {
067                doForceResolveAllContents(annotationWithTarget.getAnnotation());
068            }
069        }
070    
071        private static void doForceResolveAllContents(Object object) {
072            if (object instanceof LazyEntity) {
073                LazyEntity lazyEntity = (LazyEntity) object;
074                lazyEntity.forceResolveAllContents();
075            }
076            else if (object instanceof CallableDescriptor) {
077                CallableDescriptor callableDescriptor = (CallableDescriptor) object;
078                ReceiverParameterDescriptor parameter = callableDescriptor.getExtensionReceiverParameter();
079                if (parameter != null) {
080                    forceResolveAllContents(parameter.getType());
081                }
082                for (ValueParameterDescriptor parameterDescriptor : callableDescriptor.getValueParameters()) {
083                    forceResolveAllContents(parameterDescriptor);
084                }
085                for (TypeParameterDescriptor typeParameterDescriptor : callableDescriptor.getTypeParameters()) {
086                    forceResolveAllContents(typeParameterDescriptor.getUpperBounds());
087                }
088                forceResolveAllContents(callableDescriptor.getReturnType());
089                forceResolveAllContents(callableDescriptor.getAnnotations());
090            }
091        }
092    
093        @Nullable
094        public static JetType forceResolveAllContents(@Nullable JetType type) {
095            if (type == null) return null;
096    
097            forceResolveAllContents(type.getAnnotations());
098            if (TypesPackage.isFlexible(type)) {
099                forceResolveAllContents(TypesPackage.flexibility(type).getLowerBound());
100                forceResolveAllContents(TypesPackage.flexibility(type).getUpperBound());
101            }
102            else {
103                forceResolveAllContents(type.getConstructor());
104                for (TypeProjection projection : type.getArguments()) {
105                    if (!projection.isStarProjection()) {
106                        forceResolveAllContents(projection.getType());
107                    }
108                }
109            }
110            return type;
111        }
112    }