From 1e611f4df1be2d3e336dfbbd69e180f1f7fa9402 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Fri, 28 Mar 2014 21:55:31 +0400 Subject: [PATCH] Minor refactoring in TypeUtils Move out unneeded things in runtime, fix warnings --- .../expressions/TypeReconstructionUtil.java | 16 ++++- .../SamAdapterOverridabilityCondition.java | 26 +++++++- .../jetbrains/jet/lang/types/TypeUtils.java | 63 ++++--------------- .../quickfix/AddStarProjectionsFix.java | 6 +- .../quickfix/ChangeToStarProjectionFix.java | 4 +- 5 files changed, 57 insertions(+), 58 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/TypeReconstructionUtil.java b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/TypeReconstructionUtil.java index 5f4467b7501..c33404c8f41 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/TypeReconstructionUtil.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/TypeReconstructionUtil.java @@ -67,6 +67,20 @@ public class TypeReconstructionUtil { assert declarationDescriptor != null : "No declaration descriptor for type constructor " + constructor; String name = declarationDescriptor.getName().asString(); - return TypeUtils.getTypeNameAndStarProjectionsString(name, size); + return getTypeNameAndStarProjectionsString(name, size); + } + + @NotNull + public static String getTypeNameAndStarProjectionsString(@NotNull String name, int size) { + StringBuilder builder = new StringBuilder(name); + builder.append("<"); + for (int i = 0; i < size; i++) { + builder.append("*"); + if (i == size - 1) break; + builder.append(", "); + } + builder.append(">"); + + return builder.toString(); } } diff --git a/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/java/sam/SamAdapterOverridabilityCondition.java b/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/java/sam/SamAdapterOverridabilityCondition.java index a83433138fe..a2a7701a258 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/java/sam/SamAdapterOverridabilityCondition.java +++ b/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/java/sam/SamAdapterOverridabilityCondition.java @@ -1,3 +1,19 @@ +/* + * Copyright 2010-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.jetbrains.jet.lang.resolve.java.sam; import org.jetbrains.annotations.NotNull; @@ -34,13 +50,21 @@ public class SamAdapterOverridabilityCondition implements ExternalOverridability for (ValueParameterDescriptor param1 : parameters1) { ValueParameterDescriptor param2 = parameters2.get(param1.getIndex()); - if (!TypeUtils.equalClasses(param2.getType(), param1.getType())) { + if (!equalClasses(param2.getType(), param1.getType())) { return false; } } return true; } + private static boolean equalClasses(@NotNull JetType type1, @NotNull JetType type2) { + DeclarationDescriptor declarationDescriptor1 = type1.getConstructor().getDeclarationDescriptor(); + if (declarationDescriptor1 == null) return false; // No class, classes are not equal + DeclarationDescriptor declarationDescriptor2 = type2.getConstructor().getDeclarationDescriptor(); + if (declarationDescriptor2 == null) return false; // Class of type1 is not null + return declarationDescriptor1.getOriginal().equals(declarationDescriptor2.getOriginal()); + } + // if function is or overrides declaration, returns null; otherwise, return original of sam adapter with substituted type parameters @Nullable private static SimpleFunctionDescriptor getOriginalOfSamAdapterFunction(@NotNull SimpleFunctionDescriptor callable) { diff --git a/core/descriptors/src/org/jetbrains/jet/lang/types/TypeUtils.java b/core/descriptors/src/org/jetbrains/jet/lang/types/TypeUtils.java index 3a13bef729e..4b76340dd03 100644 --- a/core/descriptors/src/org/jetbrains/jet/lang/types/TypeUtils.java +++ b/core/descriptors/src/org/jetbrains/jet/lang/types/TypeUtils.java @@ -23,7 +23,8 @@ import com.google.common.collect.Maps; import com.google.common.collect.Sets; import com.intellij.openapi.util.Pair; import com.intellij.util.Processor; -import com.intellij.util.containers.ContainerUtil; +import kotlin.Function1; +import kotlin.KotlinPackage; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.lang.descriptors.ClassDescriptor; @@ -133,6 +134,14 @@ public class TypeUtils { return nullable ? new NullableType(type) : new NotNullType(type); } + @NotNull + public static JetType makeNullableIfNeeded(@NotNull JetType type, boolean nullable) { + if (nullable) { + return makeNullable(type); + } + return type; + } + public static boolean isIntersectionEmpty(@NotNull JetType typeA, @NotNull JetType typeB) { return intersect(JetTypeChecker.INSTANCE, Sets.newLinkedHashSet(Lists.newArrayList(typeA, typeB))) == null; } @@ -342,13 +351,6 @@ public class TypeUtils { return false; } - public static JetType makeNullableIfNeeded(JetType type, boolean nullable) { - if (nullable) { - return makeNullable(type); - } - return type; - } - @NotNull public static JetType makeUnsubstitutedType(ClassDescriptor classDescriptor, JetScope unsubstitutedMemberScope) { if (ErrorUtils.isError(classDescriptor)) { @@ -374,15 +376,6 @@ public class TypeUtils { return result; } - @NotNull - public static List getDefaultTypes(List parameters) { - List result = Lists.newArrayList(); - for (TypeParameterDescriptor parameterDescriptor : parameters) { - result.add(parameterDescriptor.getDefaultType()); - } - return result; - } - private static void collectImmediateSupertypes(@NotNull JetType type, @NotNull Collection result) { TypeSubstitutor substitutor = TypeSubstitutor.create(type); for (JetType supertype : type.getConstructor().getSupertypes()) { @@ -438,14 +431,6 @@ public class TypeUtils { return false; } - public static boolean equalClasses(@NotNull JetType type1, @NotNull JetType type2) { - DeclarationDescriptor declarationDescriptor1 = type1.getConstructor().getDeclarationDescriptor(); - if (declarationDescriptor1 == null) return false; // No class, classes are not equal - DeclarationDescriptor declarationDescriptor2 = type2.getConstructor().getDeclarationDescriptor(); - if (declarationDescriptor2 == null) return false; // Class of type1 is not null - return declarationDescriptor1.getOriginal().equals(declarationDescriptor2.getOriginal()); - } - @Nullable public static ClassDescriptor getClassDescriptor(@NotNull JetType type) { DeclarationDescriptor declarationDescriptor = type.getConstructor().getDeclarationDescriptor(); @@ -457,9 +442,9 @@ public class TypeUtils { @NotNull public static JetType substituteParameters(@NotNull ClassDescriptor clazz, @NotNull List typeArguments) { - List projections = ContainerUtil.map(typeArguments, new com.intellij.util.Function() { + List projections = KotlinPackage.map(typeArguments, new Function1() { @Override - public TypeProjection fun(JetType type) { + public TypeProjection invoke(JetType type) { return new TypeProjectionImpl(type); } }); @@ -488,16 +473,6 @@ public class TypeUtils { return JetTypeChecker.INSTANCE.isSubtypeOf(a, b) && JetTypeChecker.INSTANCE.isSubtypeOf(b, a); } - public static boolean typeConstructorUsedInType(@NotNull TypeConstructor key, @NotNull JetType value) { - if (value.getConstructor() == key) return true; - for (TypeProjection projection : value.getArguments()) { - if (typeConstructorUsedInType(key, projection.getType())) { - return true; - } - } - return false; - } - public static boolean dependsOnTypeParameters(@NotNull JetType type, @NotNull Collection typeParameters) { return dependsOnTypeConstructors(type, Collections2 .transform(typeParameters, new Function() { @@ -533,20 +508,6 @@ public class TypeUtils { return false; } - @NotNull - public static String getTypeNameAndStarProjectionsString(@NotNull String name, int size) { - StringBuilder builder = new StringBuilder(name); - builder.append("<"); - for (int i = 0; i < size; i++) { - builder.append("*"); - if (i == size - 1) break; - builder.append(", "); - } - builder.append(">"); - - return builder.toString(); - } - @NotNull public static TypeProjection makeStarProjection(@NotNull TypeParameterDescriptor parameterDescriptor) { return new TypeProjectionImpl(parameterDescriptor.getVariance() == Variance.OUT_VARIANCE diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/AddStarProjectionsFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/AddStarProjectionsFix.java index 961840f5199..f271ef8bf12 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/AddStarProjectionsFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/AddStarProjectionsFix.java @@ -28,7 +28,7 @@ import org.jetbrains.jet.lang.diagnostics.DiagnosticWithParameters1; import org.jetbrains.jet.lang.diagnostics.DiagnosticWithParameters2; import org.jetbrains.jet.lang.diagnostics.Errors; import org.jetbrains.jet.lang.psi.*; -import org.jetbrains.jet.lang.types.TypeUtils; +import org.jetbrains.jet.lang.types.expressions.TypeReconstructionUtil; import org.jetbrains.jet.plugin.JetBundle; public abstract class AddStarProjectionsFix extends JetIntentionAction { @@ -43,7 +43,7 @@ public abstract class AddStarProjectionsFix extends JetIntentionAction { @@ -34,7 +34,7 @@ public class ChangeToStarProjectionFix extends JetIntentionAction