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.AnnotationWithTarget;
024    import org.jetbrains.kotlin.descriptors.annotations.Annotations;
025    import org.jetbrains.kotlin.resolve.scopes.KtScope;
026    import org.jetbrains.kotlin.types.*;
027    
028    import java.util.Collection;
029    
030    public class ForceResolveUtil {
031        private static final Logger LOG = Logger.getInstance(ForceResolveUtil.class);
032    
033        private ForceResolveUtil() {}
034    
035        public static <T> T forceResolveAllContents(@NotNull T descriptor) {
036            doForceResolveAllContents(descriptor);
037            return descriptor;
038        }
039    
040        public static void forceResolveAllContents(@NotNull KtScope scope) {
041            forceResolveAllContents(scope.getAllDescriptors());
042        }
043    
044        public static void forceResolveAllContents(@NotNull Iterable<? extends DeclarationDescriptor> descriptors) {
045            for (DeclarationDescriptor descriptor : descriptors) {
046                forceResolveAllContents(descriptor);
047            }
048        }
049    
050        public static void forceResolveAllContents(@NotNull Collection<KotlinType> types) {
051            for (KotlinType type : types) {
052                forceResolveAllContents(type);
053            }
054        }
055    
056        public static void forceResolveAllContents(@NotNull TypeConstructor typeConstructor) {
057            doForceResolveAllContents(typeConstructor);
058        }
059    
060        public static void forceResolveAllContents(@NotNull Annotations annotations) {
061            doForceResolveAllContents(annotations);
062            for (AnnotationWithTarget annotationWithTarget : annotations.getAllAnnotations()) {
063                doForceResolveAllContents(annotationWithTarget.getAnnotation());
064            }
065        }
066    
067        private static void doForceResolveAllContents(Object object) {
068            if (object instanceof LazyEntity) {
069                LazyEntity lazyEntity = (LazyEntity) object;
070                lazyEntity.forceResolveAllContents();
071            }
072            else if (object instanceof CallableDescriptor) {
073                CallableDescriptor callableDescriptor = (CallableDescriptor) object;
074                ReceiverParameterDescriptor parameter = callableDescriptor.getExtensionReceiverParameter();
075                if (parameter != null) {
076                    forceResolveAllContents(parameter.getType());
077                }
078                for (ValueParameterDescriptor parameterDescriptor : callableDescriptor.getValueParameters()) {
079                    forceResolveAllContents(parameterDescriptor);
080                }
081                for (TypeParameterDescriptor typeParameterDescriptor : callableDescriptor.getTypeParameters()) {
082                    forceResolveAllContents(typeParameterDescriptor.getUpperBounds());
083                }
084                forceResolveAllContents(callableDescriptor.getReturnType());
085                forceResolveAllContents(callableDescriptor.getAnnotations());
086            }
087        }
088    
089        @Nullable
090        public static KotlinType forceResolveAllContents(@Nullable KotlinType type) {
091            if (type == null) return null;
092    
093            forceResolveAllContents(type.getAnnotations());
094            if (FlexibleTypesKt.isFlexible(type)) {
095                forceResolveAllContents(FlexibleTypesKt.flexibility(type).getLowerBound());
096                forceResolveAllContents(FlexibleTypesKt.flexibility(type).getUpperBound());
097            }
098            else {
099                forceResolveAllContents(type.getConstructor());
100                for (TypeProjection projection : type.getArguments()) {
101                    if (!projection.isStarProjection()) {
102                        forceResolveAllContents(projection.getType());
103                    }
104                }
105            }
106            return type;
107        }
108    }