From 8e3f87abaffe37c176b1ba4e36a346c12b06b836 Mon Sep 17 00:00:00 2001 From: Denis Zharkov Date: Thu, 2 Oct 2014 18:22:13 +0400 Subject: [PATCH] Minor, extracted TypeUtils.isTypeParameter --- .../jet/lang/types/CastDiagnosticsUtil.java | 8 ++------ .../org/jetbrains/jet/lang/types/TypeUtils.java | 14 +++++++++++++- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/types/CastDiagnosticsUtil.java b/compiler/frontend/src/org/jetbrains/jet/lang/types/CastDiagnosticsUtil.java index ce92a892561..2a461139d1a 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/types/CastDiagnosticsUtil.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/types/CastDiagnosticsUtil.java @@ -47,7 +47,7 @@ public class CastDiagnosticsUtil { if (isRelated(lhsType, rhsType, platformToKotlinClassMap)) return true; // This is an oversimplification (which does not render the method incomplete): // we consider any type parameter capable of taking any value, which may be made more precise if we considered bounds - if (isTypeParameter(lhsType) || isTypeParameter(rhsType)) return true; + if (TypeUtils.isTypeParameter(lhsType) || TypeUtils.isTypeParameter(rhsType)) return true; if (isFinal(lhsType) || isFinal(rhsType)) return false; if (isTrait(lhsType) || isTrait(rhsType)) return true; return false; @@ -97,10 +97,6 @@ public class CastDiagnosticsUtil { return result; } - private static boolean isTypeParameter(@NotNull JetType type) { - return type.getConstructor().getDeclarationDescriptor() instanceof TypeParameterDescriptor; - } - private static boolean isFinal(@NotNull JetType type) { return !TypeUtils.canHaveSubtypes(JetTypeChecker.DEFAULT, type); } @@ -124,7 +120,7 @@ public class CastDiagnosticsUtil { if (typeChecker.isSubtypeOf(supertype, subtype)) return false; // downcasting to a type parameter is always erased - if (isTypeParameter(subtype)) return true; + if (TypeUtils.isTypeParameter(subtype)) return true; // Check that we are actually casting to a generic type // NOTE: this does not account for 'as Array>' 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 73d57b70f0d..3e4066c35fe 100644 --- a/core/descriptors/src/org/jetbrains/jet/lang/types/TypeUtils.java +++ b/core/descriptors/src/org/jetbrains/jet/lang/types/TypeUtils.java @@ -434,7 +434,7 @@ public class TypeUtils { if (TypesPackage.isFlexible(type) && isNullableType(TypesPackage.flexibility(type).getUpperBound())) { return true; } - if (type.getConstructor().getDeclarationDescriptor() instanceof TypeParameterDescriptor) { + if (isTypeParameter(type)) { return hasNullableSuperType(type); } return false; @@ -720,6 +720,18 @@ public class TypeUtils { }); } + public static boolean isTypeParameter(@NotNull JetType type) { + return getTypeParameterDescriptorOrNull(type) != null; + } + + @Nullable + public static TypeParameterDescriptor getTypeParameterDescriptorOrNull(@NotNull JetType type) { + if (type.getConstructor().getDeclarationDescriptor() instanceof TypeParameterDescriptor) { + return (TypeParameterDescriptor) type.getConstructor().getDeclarationDescriptor(); + } + return null; + } + private static abstract class AbstractTypeWithKnownNullability extends AbstractJetType { private final JetType delegate;