From cf630e01dbeb6d8418381ee64e5f43ea4c304431 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Wed, 22 Jun 2016 15:55:41 +0300 Subject: [PATCH] Minor, refactor "is KProperty" checks in ReflectionTypes --- .../codegen/PropertyReferenceCodegen.kt | 2 +- .../binding/CodegenAnnotatingVisitor.java | 2 +- .../kotlin/builtins/KotlinBuiltIns.java | 3 ++ .../kotlin/builtins/ReflectionTypes.kt | 34 +++++++++++-------- .../expression/PatternTranslator.java | 3 +- 5 files changed, 26 insertions(+), 18 deletions(-) diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/PropertyReferenceCodegen.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/PropertyReferenceCodegen.kt index 5a4a1a8f74f..dc23d3bb1e5 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/PropertyReferenceCodegen.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/PropertyReferenceCodegen.kt @@ -179,7 +179,7 @@ class PropertyReferenceCodegen( value.put(OBJECT_TYPE, this) } - if (!ReflectionTypes.isKMutablePropertyType(localVariableDescriptorForReference.type)) return + if (!ReflectionTypes.isNumberedKMutablePropertyType(localVariableDescriptorForReference.type)) return val setterParameters = (getterParameters + arrayOf(OBJECT_TYPE)) generateAccessor(method("set", Type.VOID_TYPE, *setterParameters)) { value -> diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/binding/CodegenAnnotatingVisitor.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/binding/CodegenAnnotatingVisitor.java index f030d1e4197..0e42ca27cbf 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/binding/CodegenAnnotatingVisitor.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/binding/CodegenAnnotatingVisitor.java @@ -305,7 +305,7 @@ class CodegenAnnotatingVisitor extends KtVisitorVoid { supertypes = Collections.singleton( runtimeTypes.getSupertypeForPropertyReference( (PropertyDescriptor) target, - ReflectionTypes.Companion.isKMutablePropertyType(callableDescriptor.getReturnType()), + ReflectionTypes.Companion.isNumberedKMutablePropertyType(callableDescriptor.getReturnType()), receiverType != null ) ); diff --git a/core/descriptors/src/org/jetbrains/kotlin/builtins/KotlinBuiltIns.java b/core/descriptors/src/org/jetbrains/kotlin/builtins/KotlinBuiltIns.java index 4abbe3034cc..6bf34144d20 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/builtins/KotlinBuiltIns.java +++ b/core/descriptors/src/org/jetbrains/kotlin/builtins/KotlinBuiltIns.java @@ -216,6 +216,9 @@ public abstract class KotlinBuiltIns { public final FqNameUnsafe kClass = reflect("KClass"); public final FqNameUnsafe kCallable = reflect("KCallable"); + public final FqNameUnsafe kProperty0 = reflect("KProperty0"); + public final FqNameUnsafe kProperty1 = reflect("KProperty1"); + public final FqNameUnsafe kProperty2 = reflect("KProperty2"); public final FqNameUnsafe kMutableProperty0 = reflect("KMutableProperty0"); public final FqNameUnsafe kMutableProperty1 = reflect("KMutableProperty1"); public final FqNameUnsafe kMutableProperty2 = reflect("KMutableProperty2"); diff --git a/core/descriptors/src/org/jetbrains/kotlin/builtins/ReflectionTypes.kt b/core/descriptors/src/org/jetbrains/kotlin/builtins/ReflectionTypes.kt index 9eed1e040dc..0316c6b2ce3 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/builtins/ReflectionTypes.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/builtins/ReflectionTypes.kt @@ -120,28 +120,34 @@ class ReflectionTypes(module: ModuleDescriptor) { fun isCallableType(type: KotlinType): Boolean = type.isFunctionTypeOrSubtype || isKCallableType(type) - @JvmStatic fun isFunctionalKPropertyType(type: KotlinType): Boolean { - val descriptor = type.constructor.declarationDescriptor ?: return false - val segments = DescriptorUtils.getFqName(descriptor).pathSegments() - return segments.size == 3 && segments.subList(0, 2) == KOTLIN_REFLECT_FQ_NAME.pathSegments() && - segments.last().let { it.isProperty("KProperty") || it.isProperty("KMutableProperty") } - } - - private fun Name.isProperty(baseName: String) = asString().startsWith(baseName) && asString() != baseName + @JvmStatic + fun isNumberedKPropertyOrKMutablePropertyType(type: KotlinType): Boolean = + isNumberedKPropertyType(type) || isNumberedKMutablePropertyType(type) private fun isKCallableType(type: KotlinType): Boolean = hasFqName(type.constructor, KotlinBuiltIns.FQ_NAMES.kCallable) || type.constructor.supertypes.any { isKCallableType(it) } - fun isKMutablePropertyType(type: KotlinType): Boolean { - val typeConstructor = type.constructor - return hasFqName(typeConstructor, KotlinBuiltIns.FQ_NAMES.kMutableProperty0) || - hasFqName(typeConstructor, KotlinBuiltIns.FQ_NAMES.kMutableProperty1) || - hasFqName(typeConstructor, KotlinBuiltIns.FQ_NAMES.kMutableProperty2) + fun isNumberedKMutablePropertyType(type: KotlinType): Boolean { + val descriptor = type.constructor.declarationDescriptor as? ClassDescriptor ?: return false + return hasFqName(descriptor, KotlinBuiltIns.FQ_NAMES.kMutableProperty0) || + hasFqName(descriptor, KotlinBuiltIns.FQ_NAMES.kMutableProperty1) || + hasFqName(descriptor, KotlinBuiltIns.FQ_NAMES.kMutableProperty2) + } + + fun isNumberedKPropertyType(type: KotlinType): Boolean { + val descriptor = type.constructor.declarationDescriptor as? ClassDescriptor ?: return false + return hasFqName(descriptor, KotlinBuiltIns.FQ_NAMES.kProperty0) || + hasFqName(descriptor, KotlinBuiltIns.FQ_NAMES.kProperty1) || + hasFqName(descriptor, KotlinBuiltIns.FQ_NAMES.kProperty2) } private fun hasFqName(typeConstructor: TypeConstructor, fqName: FqNameUnsafe): Boolean { - val descriptor = typeConstructor.declarationDescriptor as? ClassDescriptor ?: return false + val descriptor = typeConstructor.declarationDescriptor + return descriptor is ClassDescriptor && hasFqName(descriptor, fqName) + } + + private fun hasFqName(descriptor: ClassDescriptor, fqName: FqNameUnsafe): Boolean { return descriptor.name == fqName.shortName() && DescriptorUtils.getFqName(descriptor) == fqName } diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/expression/PatternTranslator.java b/js/js.translator/src/org/jetbrains/kotlin/js/translate/expression/PatternTranslator.java index 6629a582ece..0004e7bced0 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/expression/PatternTranslator.java +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/expression/PatternTranslator.java @@ -23,7 +23,6 @@ import org.jetbrains.kotlin.KtNodeTypes; import org.jetbrains.kotlin.builtins.KotlinBuiltIns; import org.jetbrains.kotlin.builtins.PrimitiveType; import org.jetbrains.kotlin.builtins.ReflectionTypes; -import org.jetbrains.kotlin.builtins.ReflectionTypesKt; import org.jetbrains.kotlin.descriptors.CallableDescriptor; import org.jetbrains.kotlin.descriptors.ClassDescriptor; import org.jetbrains.kotlin.descriptors.DeclarationDescriptor; @@ -183,7 +182,7 @@ public final class PatternTranslator extends AbstractTranslator { private JsExpression getIsTypeCheckCallableForBuiltin(@NotNull KotlinType type) { if (isAnyOrNullableAny(type)) return namer().isAny(); - if (isFunctionTypeOrSubtype(type) && !ReflectionTypes.isFunctionalKPropertyType(type)) { + if (isFunctionTypeOrSubtype(type) && !ReflectionTypes.isNumberedKPropertyOrKMutablePropertyType(type)) { return namer().isTypeOf(program().getStringLiteral("function")); }