001    /*
002     * Copyright 2010-2013 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.jet.lang.resolve.java.kotlinSignature;
018    
019    import com.google.common.collect.Maps;
020    import org.jetbrains.annotations.NotNull;
021    import org.jetbrains.annotations.Nullable;
022    import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
023    import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor;
024    import org.jetbrains.jet.lang.descriptors.impl.TypeParameterDescriptorImpl;
025    import org.jetbrains.jet.lang.types.TypeConstructor;
026    import org.jetbrains.jet.lang.types.TypeProjection;
027    import org.jetbrains.jet.lang.types.TypeSubstitutor;
028    
029    import java.util.List;
030    import java.util.Map;
031    
032    public class SignaturesUtil {
033        public static Map<TypeParameterDescriptor, TypeParameterDescriptorImpl> recreateTypeParametersAndReturnMapping(
034                @NotNull List<TypeParameterDescriptor> originalParameters,
035                @Nullable DeclarationDescriptor newOwner
036        ) {
037            Map<TypeParameterDescriptor, TypeParameterDescriptorImpl> result = Maps.newLinkedHashMap(); // save order of type parameters
038            for (TypeParameterDescriptor typeParameter : originalParameters) {
039                result.put(typeParameter,
040                           TypeParameterDescriptorImpl.createForFurtherModification(
041                                   newOwner == null ? typeParameter.getContainingDeclaration() : newOwner,
042                                   typeParameter.getAnnotations(),
043                                   typeParameter.isReified(),
044                                   typeParameter.getVariance(),
045                                   typeParameter.getName(),
046                                   typeParameter.getIndex()));
047            }
048            return result;
049        }
050    
051        public static TypeSubstitutor createSubstitutorForTypeParameters(
052                @NotNull Map<TypeParameterDescriptor, TypeParameterDescriptorImpl> originalToAltTypeParameters
053        ) {
054            Map<TypeConstructor, TypeProjection> typeSubstitutionContext = Maps.newHashMap();
055            for (Map.Entry<TypeParameterDescriptor, TypeParameterDescriptorImpl> originalToAltTypeParameter : originalToAltTypeParameters
056                    .entrySet()) {
057                typeSubstitutionContext.put(originalToAltTypeParameter.getKey().getTypeConstructor(),
058                                            new TypeProjection(originalToAltTypeParameter.getValue().getDefaultType()));
059            }
060            return TypeSubstitutor.create(typeSubstitutionContext);
061        }
062    
063        private SignaturesUtil() {
064        }
065    }