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                substituted.setInitialized();
057    
058                mutableSubstitution.put(descriptor.getTypeConstructor(), new TypeProjectionImpl(substituted.getDefaultType()));
059    
060                substitutedMap.put(descriptor, substituted);
061                result.add(substituted);
062            }
063    
064            TypeSubstitutor substitutor = TypeSubstitutor.createChainedSubstitutor(
065                    originalSubstitution, TypeConstructorSubstitution.createByConstructorsMap(mutableSubstitution)
066            );
067    
068            for (TypeParameterDescriptor descriptor : typeParameters) {
069                TypeParameterDescriptorImpl substituted = substitutedMap.get(descriptor);
070                for (JetType upperBound : descriptor.getUpperBounds()) {
071                    substituted.getUpperBounds().add(substitutor.substitute(upperBound, Variance.INVARIANT));
072                }
073            }
074    
075            return substitutor;
076        }
077    }