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.jvm; 018 019 import org.jetbrains.annotations.NotNull; 020 import org.jetbrains.annotations.Nullable; 021 import org.jetbrains.kotlin.descriptors.DeclarationDescriptor; 022 import org.jetbrains.kotlin.descriptors.SourceElement; 023 import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor; 024 import org.jetbrains.kotlin.descriptors.impl.TypeParameterDescriptorImpl; 025 import org.jetbrains.kotlin.load.java.structure.*; 026 import org.jetbrains.kotlin.types.TypeConstructor; 027 import org.jetbrains.kotlin.types.TypeProjection; 028 import org.jetbrains.kotlin.types.TypeProjectionImpl; 029 import org.jetbrains.kotlin.types.TypeSubstitutor; 030 031 import java.util.*; 032 033 public class JavaResolverUtils { 034 private JavaResolverUtils() { 035 } 036 037 public static Map<TypeParameterDescriptor, TypeParameterDescriptorImpl> recreateTypeParametersAndReturnMapping( 038 @NotNull List<TypeParameterDescriptor> originalParameters, 039 @Nullable DeclarationDescriptor newOwner 040 ) { 041 // LinkedHashMap to save the order of type parameters 042 Map<TypeParameterDescriptor, TypeParameterDescriptorImpl> result = 043 new LinkedHashMap<TypeParameterDescriptor, TypeParameterDescriptorImpl>(); 044 for (TypeParameterDescriptor typeParameter : originalParameters) { 045 result.put(typeParameter, 046 TypeParameterDescriptorImpl.createForFurtherModification( 047 newOwner == null ? typeParameter.getContainingDeclaration() : newOwner, 048 typeParameter.getAnnotations(), 049 typeParameter.isReified(), 050 typeParameter.getVariance(), 051 typeParameter.getName(), 052 typeParameter.getIndex(), 053 SourceElement.NO_SOURCE 054 ) 055 ); 056 } 057 return result; 058 } 059 060 @NotNull 061 public static TypeSubstitutor createSubstitutorForTypeParameters( 062 @NotNull Map<TypeParameterDescriptor, TypeParameterDescriptorImpl> originalToAltTypeParameters 063 ) { 064 Map<TypeConstructor, TypeProjection> typeSubstitutionContext = new HashMap<TypeConstructor, TypeProjection>(); 065 for (Map.Entry<TypeParameterDescriptor, TypeParameterDescriptorImpl> originalToAltTypeParameter : originalToAltTypeParameters.entrySet()) { 066 typeSubstitutionContext.put(originalToAltTypeParameter.getKey().getTypeConstructor(), 067 new TypeProjectionImpl(originalToAltTypeParameter.getValue().getDefaultType())); 068 } 069 // TODO: Use IndexedParametersSubstitution here instead of map creation 070 return TypeSubstitutor.create(typeSubstitutionContext); 071 } 072 }