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.types;
018    
019    import org.jetbrains.annotations.Mutable;
020    import org.jetbrains.annotations.NotNull;
021    import org.jetbrains.annotations.ReadOnly;
022    import org.jetbrains.kotlin.descriptors.DeclarationDescriptor;
023    import org.jetbrains.kotlin.descriptors.SourceElement;
024    import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor;
025    import org.jetbrains.kotlin.descriptors.impl.TypeParameterDescriptorImpl;
026    
027    import java.util.HashMap;
028    import java.util.List;
029    import java.util.Map;
030    
031    public class DescriptorSubstitutor {
032        private DescriptorSubstitutor() {
033        }
034    
035        @NotNull
036        public static TypeSubstitutor substituteTypeParameters(
037                @ReadOnly @NotNull List<TypeParameterDescriptor> typeParameters,
038                @NotNull TypeSubstitution originalSubstitution,
039                @NotNull DeclarationDescriptor newContainingDeclaration,
040                @NotNull @Mutable List<TypeParameterDescriptor> result
041        ) {
042            Map<TypeConstructor, TypeProjection> mutableSubstitution = new HashMap<TypeConstructor, TypeProjection>();
043    
044            Map<TypeParameterDescriptor, TypeParameterDescriptorImpl> substitutedMap = new HashMap<TypeParameterDescriptor, TypeParameterDescriptorImpl>();
045            int index = 0;
046            for (TypeParameterDescriptor descriptor : typeParameters) {
047                TypeParameterDescriptorImpl substituted = TypeParameterDescriptorImpl.createForFurtherModification(
048                        newContainingDeclaration,
049                        descriptor.getAnnotations(),
050                        descriptor.isReified(),
051                        descriptor.getVariance(),
052                        descriptor.getName(),
053                        index++,
054                        SourceElement.NO_SOURCE
055                );
056    
057                mutableSubstitution.put(descriptor.getTypeConstructor(), new TypeProjectionImpl(substituted.getDefaultType()));
058    
059                substitutedMap.put(descriptor, substituted);
060                result.add(substituted);
061            }
062    
063            TypeSubstitutor substitutor = TypeSubstitutor.createChainedSubstitutor(
064                    originalSubstitution, TypeConstructorSubstitution.createByConstructorsMap(mutableSubstitution)
065            );
066    
067            for (TypeParameterDescriptor descriptor : typeParameters) {
068                TypeParameterDescriptorImpl substituted = substitutedMap.get(descriptor);
069                for (KotlinType upperBound : descriptor.getUpperBounds()) {
070                    KotlinType substitutedBound = substitutor.substitute(upperBound, Variance.IN_VARIANCE);
071                    assert substitutedBound != null : "Upper bound failed to substitute: " + descriptor;
072                    substituted.addUpperBound(substitutedBound);
073                }
074                substituted.setInitialized();
075            }
076    
077            return substitutor;
078        }
079    }