From a6846b39673108a63271014e0f5564d40b60eddb Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Tue, 13 Oct 2015 13:00:51 +0300 Subject: [PATCH] Insert KProperty<*> when creating property delegate methods in IDE --- .../kotlin/resolve/DelegatedPropertyResolver.java | 2 +- .../jetbrains/kotlin/builtins/KotlinBuiltIns.java | 15 +++++++++------ .../jetbrains/kotlin/builtins/ReflectionTypes.kt | 14 ++++++++++---- .../org/jetbrains/kotlin/util/OperatorChecker.kt | 9 ++------- ...reatePropertyDelegateAccessorsActionFactory.kt | 6 ++++-- .../createFunction/delegateAccessors/val.kt.after | 4 +++- .../createFunction/delegateAccessors/var.kt.after | 6 ++++-- .../delegateAccessors/varMissingGet.kt | 4 +++- .../delegateAccessors/varMissingGet.kt.after | 8 +++++--- .../delegateAccessors/varMissingSet.kt | 4 +++- .../delegateAccessors/varMissingSet.kt.after | 8 +++++--- 11 files changed, 49 insertions(+), 31 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/DelegatedPropertyResolver.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/DelegatedPropertyResolver.java index ad473a0a1dd..b7463d6ae3d 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/DelegatedPropertyResolver.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/DelegatedPropertyResolver.java @@ -128,7 +128,7 @@ public class DelegatedPropertyResolver { @NotNull private static JetExpression createExpressionForProperty(@NotNull JetPsiFactory psiFactory) { - return psiFactory.createExpression("null as kotlin.reflect.KProperty<*>"); + return psiFactory.createExpression("null as " + KotlinBuiltIns.FQ_NAMES.kProperty.asSingleFqName().asString() + "<*>"); } public void resolveDelegatedPropertyPDMethod( diff --git a/core/descriptors/src/org/jetbrains/kotlin/builtins/KotlinBuiltIns.java b/core/descriptors/src/org/jetbrains/kotlin/builtins/KotlinBuiltIns.java index 563e1360a0c..12fb0d5d3be 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/builtins/KotlinBuiltIns.java +++ b/core/descriptors/src/org/jetbrains/kotlin/builtins/KotlinBuiltIns.java @@ -26,6 +26,7 @@ import org.jetbrains.kotlin.descriptors.annotations.*; import org.jetbrains.kotlin.descriptors.impl.ModuleDescriptorImpl; import org.jetbrains.kotlin.descriptors.impl.ValueParameterDescriptorImpl; import org.jetbrains.kotlin.incremental.components.NoLookupLocation; +import org.jetbrains.kotlin.name.ClassId; import org.jetbrains.kotlin.name.FqName; import org.jetbrains.kotlin.name.FqNameUnsafe; import org.jetbrains.kotlin.name.Name; @@ -157,7 +158,9 @@ public abstract class KotlinBuiltIns { public final FqName mutableSet = fqName("MutableSet"); public final FqName mutableMap = fqName("MutableMap"); - public final FqNameUnsafe kClass = new FqName("kotlin.reflect.KClass").toUnsafe(); + public final FqNameUnsafe kClass = reflect("KClass"); + public final FqNameUnsafe kCallable = reflect("KCallable"); + public final ClassId kProperty = ClassId.topLevel(reflect("KProperty").toSafe()); public final Map fqNameToPrimitiveType; public final Map arrayClassFqNameToPrimitiveType; @@ -180,6 +183,11 @@ public abstract class KotlinBuiltIns { return BUILT_INS_PACKAGE_FQ_NAME.child(Name.identifier(simpleName)); } + @NotNull + private static FqNameUnsafe reflect(@NotNull String simpleName) { + return ReflectionTypesKt.getKOTLIN_REFLECT_FQ_NAME().child(Name.identifier(simpleName)).toUnsafe(); + } + @NotNull private static FqName annotationName(@NotNull String simpleName) { return ANNOTATION_PACKAGE_FQ_NAME.child(Name.identifier(simpleName)); @@ -697,11 +705,6 @@ public abstract class KotlinBuiltIns { return getAnnotation().getDefaultType(); } - @NotNull - public ClassDescriptor getPropertyMetadata() { - return getBuiltInClassByName("PropertyMetadata"); - } - @NotNull public AnnotationDescriptor createExtensionAnnotation() { return new AnnotationDescriptorImpl(getBuiltInClassByName(FQ_NAMES.extension.shortName()).getDefaultType(), diff --git a/core/descriptors/src/org/jetbrains/kotlin/builtins/ReflectionTypes.kt b/core/descriptors/src/org/jetbrains/kotlin/builtins/ReflectionTypes.kt index 7c21bcf99a2..f8928eab600 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/builtins/ReflectionTypes.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/builtins/ReflectionTypes.kt @@ -22,10 +22,10 @@ import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor import org.jetbrains.kotlin.descriptors.annotations.Annotations import org.jetbrains.kotlin.incremental.components.NoLookupLocation import org.jetbrains.kotlin.name.FqName -import org.jetbrains.kotlin.name.FqNameUnsafe import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.resolve.DescriptorUtils import org.jetbrains.kotlin.resolve.scopes.JetScope +import org.jetbrains.kotlin.serialization.deserialization.findClassAcrossModuleDependencies import org.jetbrains.kotlin.types.* import java.util.* @@ -115,8 +115,6 @@ public class ReflectionTypes(private val module: ModuleDescriptor) { return containingPackage != null && containingPackage.fqName == KOTLIN_REFLECT_FQ_NAME } - private val K_CALLABLE_FQ_NAME = FqNameUnsafe("kotlin.reflect.KCallable") - public fun isCallableType(type: JetType): Boolean = KotlinBuiltIns.isFunctionOrExtensionFunctionType(type) || isKCallableType(type) @@ -126,7 +124,15 @@ public class ReflectionTypes(private val module: ModuleDescriptor) { private fun isExactKCallableType(type: JetType): Boolean { val descriptor = type.constructor.declarationDescriptor - return descriptor is ClassDescriptor && DescriptorUtils.getFqName(descriptor) == K_CALLABLE_FQ_NAME + return descriptor is ClassDescriptor && DescriptorUtils.getFqName(descriptor) == KotlinBuiltIns.FQ_NAMES.kCallable + } + + public fun createKPropertyStarType(module: ModuleDescriptor): JetType? { + val kPropertyClass = module.findClassAcrossModuleDependencies(KotlinBuiltIns.FQ_NAMES.kProperty) ?: return null + return JetTypeImpl.create( + Annotations.EMPTY, kPropertyClass, false, + listOf(StarProjectionImpl(kPropertyClass.typeConstructor.parameters.single())) + ) } } } diff --git a/core/descriptors/src/org/jetbrains/kotlin/util/OperatorChecker.kt b/core/descriptors/src/org/jetbrains/kotlin/util/OperatorChecker.kt index 95402d05329..5bcba37dd16 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/util/OperatorChecker.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/util/OperatorChecker.kt @@ -17,17 +17,15 @@ package org.jetbrains.kotlin.util import org.jetbrains.kotlin.builtins.KotlinBuiltIns +import org.jetbrains.kotlin.builtins.ReflectionTypes import org.jetbrains.kotlin.descriptors.ClassDescriptor import org.jetbrains.kotlin.descriptors.DeclarationDescriptor import org.jetbrains.kotlin.descriptors.FunctionDescriptor import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor -import org.jetbrains.kotlin.name.ClassId -import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns import org.jetbrains.kotlin.resolve.descriptorUtil.hasDefaultValue import org.jetbrains.kotlin.resolve.descriptorUtil.isExtension import org.jetbrains.kotlin.resolve.descriptorUtil.module -import org.jetbrains.kotlin.serialization.deserialization.findClassAcrossModuleDependencies import org.jetbrains.kotlin.types.typeUtil.* import org.jetbrains.kotlin.util.OperatorNameConventions.GET import org.jetbrains.kotlin.util.OperatorNameConventions.SET @@ -103,10 +101,7 @@ object OperatorChecks { } private val ValueParameterDescriptor.isKProperty: Boolean - get() { - val kProperty = module.findClassAcrossModuleDependencies(ClassId.topLevel(FqName("kotlin.reflect.KProperty"))) - return kProperty!!.defaultType.isSubtypeOf(type.makeNotNullable()) - } + get() = ReflectionTypes.createKPropertyStarType(module)?.isSubtypeOf(type.makeNotNullable()) ?: false private val FunctionDescriptor.isMember: Boolean get() = containingDeclaration is ClassDescriptor diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/createFromUsage/createCallable/CreatePropertyDelegateAccessorsActionFactory.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/createFromUsage/createCallable/CreatePropertyDelegateAccessorsActionFactory.kt index b3976bee822..56898e6f168 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/createFromUsage/createCallable/CreatePropertyDelegateAccessorsActionFactory.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/createFromUsage/createCallable/CreatePropertyDelegateAccessorsActionFactory.kt @@ -17,7 +17,7 @@ package org.jetbrains.kotlin.idea.quickfix.createFromUsage.createCallable import com.intellij.util.SmartList -import org.jetbrains.kotlin.builtins.KotlinBuiltIns +import org.jetbrains.kotlin.builtins.ReflectionTypes import org.jetbrains.kotlin.descriptors.PropertyAccessorDescriptor import org.jetbrains.kotlin.descriptors.PropertyDescriptor import org.jetbrains.kotlin.diagnostics.Diagnostic @@ -31,6 +31,7 @@ import org.jetbrains.kotlin.psi.JetProperty import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns +import org.jetbrains.kotlin.resolve.descriptorUtil.module import org.jetbrains.kotlin.types.Variance object CreatePropertyDelegateAccessorsActionFactory : CreateCallableMemberFromUsageFactory() { @@ -54,7 +55,8 @@ object CreatePropertyDelegateAccessorsActionFactory : CreateCallableMemberFromUs val accessorReceiverType = TypeInfo(element, Variance.IN_VARIANCE) val builtIns = propertyDescriptor.builtIns val thisRefParam = ParameterInfo(TypeInfo(propertyReceiver?.type ?: builtIns.nullableNothingType, Variance.IN_VARIANCE)) - val metadataParam = ParameterInfo(TypeInfo(builtIns.propertyMetadata.defaultType, Variance.IN_VARIANCE)) + val kPropertyStarType = ReflectionTypes.createKPropertyStarType(propertyDescriptor.module) ?: return emptyList() + val metadataParam = ParameterInfo(TypeInfo(kPropertyStarType, Variance.IN_VARIANCE), "property") val callableInfos = SmartList() diff --git a/idea/testData/quickfix/createFromUsage/createFunction/delegateAccessors/val.kt.after b/idea/testData/quickfix/createFromUsage/createFunction/delegateAccessors/val.kt.after index 5beaafcd4e0..d73b6966c24 100644 --- a/idea/testData/quickfix/createFromUsage/createFunction/delegateAccessors/val.kt.after +++ b/idea/testData/quickfix/createFromUsage/createFunction/delegateAccessors/val.kt.after @@ -1,6 +1,8 @@ +import kotlin.reflect.KProperty + // "Create member function 'getValue'" "true" class F { - fun getValue(x: X, propertyMetadata: PropertyMetadata): Int { + fun getValue(x: X, property: KProperty<*>): Int { throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates. } diff --git a/idea/testData/quickfix/createFromUsage/createFunction/delegateAccessors/var.kt.after b/idea/testData/quickfix/createFromUsage/createFunction/delegateAccessors/var.kt.after index 9ad6d0ef495..70b7b305e68 100644 --- a/idea/testData/quickfix/createFromUsage/createFunction/delegateAccessors/var.kt.after +++ b/idea/testData/quickfix/createFromUsage/createFunction/delegateAccessors/var.kt.after @@ -1,10 +1,12 @@ +import kotlin.reflect.KProperty + // "Create member function 'getValue', function 'setValue'" "true" class F { - fun getValue(x: X, propertyMetadata: PropertyMetadata): Int { + fun getValue(x: X, property: KProperty<*>): Int { throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates. } - fun setValue(x: X, propertyMetadata: PropertyMetadata, i: Int) { + fun setValue(x: X, property: KProperty<*>, i: Int) { throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates. } diff --git a/idea/testData/quickfix/createFromUsage/createFunction/delegateAccessors/varMissingGet.kt b/idea/testData/quickfix/createFromUsage/createFunction/delegateAccessors/varMissingGet.kt index 0c677772264..3688d4714d6 100644 --- a/idea/testData/quickfix/createFromUsage/createFunction/delegateAccessors/varMissingGet.kt +++ b/idea/testData/quickfix/createFromUsage/createFunction/delegateAccessors/varMissingGet.kt @@ -1,6 +1,8 @@ // "Create member function 'getValue'" "true" +import kotlin.reflect.KProperty + class F { - fun setValue(x: X, propertyMetadata: PropertyMetadata, i: Int) { } + fun setValue(x: X, property: KProperty<*>, i: Int) { } } class X { diff --git a/idea/testData/quickfix/createFromUsage/createFunction/delegateAccessors/varMissingGet.kt.after b/idea/testData/quickfix/createFromUsage/createFunction/delegateAccessors/varMissingGet.kt.after index a8661a71626..1b440dffa54 100644 --- a/idea/testData/quickfix/createFromUsage/createFunction/delegateAccessors/varMissingGet.kt.after +++ b/idea/testData/quickfix/createFromUsage/createFunction/delegateAccessors/varMissingGet.kt.after @@ -1,8 +1,10 @@ // "Create member function 'getValue'" "true" -class F { - fun setValue(x: X, propertyMetadata: PropertyMetadata, i: Int) { } +import kotlin.reflect.KProperty - fun getValue(x: X, propertyMetadata: PropertyMetadata): Int { +class F { + fun setValue(x: X, property: KProperty<*>, i: Int) { } + + fun getValue(x: X, property: KProperty<*>): Int { throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates. } } diff --git a/idea/testData/quickfix/createFromUsage/createFunction/delegateAccessors/varMissingSet.kt b/idea/testData/quickfix/createFromUsage/createFunction/delegateAccessors/varMissingSet.kt index 8e62eaf3802..c7cb7ac972c 100644 --- a/idea/testData/quickfix/createFromUsage/createFunction/delegateAccessors/varMissingSet.kt +++ b/idea/testData/quickfix/createFromUsage/createFunction/delegateAccessors/varMissingSet.kt @@ -1,6 +1,8 @@ // "Create member function 'setValue'" "true" +import kotlin.reflect.KProperty + class F { - fun getValue(x: X, propertyMetadata: PropertyMetadata): Int = 1 + fun getValue(x: X, property: KProperty<*>): Int = 1 } class X { diff --git a/idea/testData/quickfix/createFromUsage/createFunction/delegateAccessors/varMissingSet.kt.after b/idea/testData/quickfix/createFromUsage/createFunction/delegateAccessors/varMissingSet.kt.after index 142f0dfc9b4..84682282c15 100644 --- a/idea/testData/quickfix/createFromUsage/createFunction/delegateAccessors/varMissingSet.kt.after +++ b/idea/testData/quickfix/createFromUsage/createFunction/delegateAccessors/varMissingSet.kt.after @@ -1,8 +1,10 @@ // "Create member function 'setValue'" "true" -class F { - fun getValue(x: X, propertyMetadata: PropertyMetadata): Int = 1 +import kotlin.reflect.KProperty - fun setValue(x: X, propertyMetadata: PropertyMetadata, i: Int) { +class F { + fun getValue(x: X, property: KProperty<*>): Int = 1 + + fun setValue(x: X, property: KProperty<*>, i: Int) { throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates. } }