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
017package org.jetbrains.jet.lang.resolve.java.kotlinSignature;
018
019import com.google.common.collect.Maps;
020import org.jetbrains.annotations.NotNull;
021import org.jetbrains.annotations.Nullable;
022import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
023import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor;
024import org.jetbrains.jet.lang.descriptors.impl.TypeParameterDescriptorImpl;
025import org.jetbrains.jet.lang.types.TypeConstructor;
026import org.jetbrains.jet.lang.types.TypeProjection;
027import org.jetbrains.jet.lang.types.TypeSubstitutor;
028
029import java.util.List;
030import java.util.Map;
031
032public 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}