Extract method

This commit is contained in:
Andrey Breslav
2013-09-03 15:59:43 +04:00
parent fb5ab4c7c5
commit 685bea6446
@@ -23,6 +23,7 @@ import com.google.common.collect.Maps;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.intellij.openapi.util.Pair; import com.intellij.openapi.util.Pair;
import com.intellij.util.Processor; import com.intellij.util.Processor;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.*; import org.jetbrains.jet.lang.descriptors.*;
@@ -456,19 +457,29 @@ public class TypeUtils {
} }
@NotNull @NotNull
public static JetType substituteParameters(@NotNull ClassDescriptor clazz, @NotNull List<JetType> actualTypeParameters) { public static JetType substituteParameters(@NotNull ClassDescriptor clazz, @NotNull List<JetType> typeArguments) {
List<TypeParameterDescriptor> clazzTypeParameters = clazz.getTypeConstructor().getParameters(); List<TypeProjection> projections = ContainerUtil.map(typeArguments, new com.intellij.util.Function<JetType, TypeProjection>() {
@Override
public TypeProjection fun(JetType type) {
return new TypeProjection(type);
}
});
if (clazzTypeParameters.size() != actualTypeParameters.size()) { return substituteProjectionsForParameters(clazz, projections);
throw new IllegalArgumentException("type parameter counts do not match: " + clazz + ", " + actualTypeParameters); }
@NotNull
public static JetType substituteProjectionsForParameters(@NotNull ClassDescriptor clazz, @NotNull List<TypeProjection> projections) {
List<TypeParameterDescriptor> clazzTypeParameters = clazz.getTypeConstructor().getParameters();
if (clazzTypeParameters.size() != projections.size()) {
throw new IllegalArgumentException("type parameter counts do not match: " + clazz + ", " + projections);
} }
Map<TypeConstructor, TypeProjection> substitutions = Maps.newHashMap(); Map<TypeConstructor, TypeProjection> substitutions = Maps.newHashMap();
for (int i = 0; i < clazzTypeParameters.size(); ++i) { for (int i = 0; i < clazzTypeParameters.size(); ++i) {
TypeConstructor typeConstructor = clazzTypeParameters.get(i).getTypeConstructor(); TypeConstructor typeConstructor = clazzTypeParameters.get(i).getTypeConstructor();
TypeProjection typeProjection = new TypeProjection(actualTypeParameters.get(i)); substitutions.put(typeConstructor, projections.get(i));
substitutions.put(typeConstructor, typeProjection);
} }
return TypeSubstitutor.create(substitutions).substitute(clazz.getDefaultType(), Variance.INVARIANT); return TypeSubstitutor.create(substitutions).substitute(clazz.getDefaultType(), Variance.INVARIANT);