From a8b5698145e67d2b6eeb37ec976b1298a644297e Mon Sep 17 00:00:00 2001 From: Andrey Breslav Date: Thu, 23 Apr 2015 18:23:08 +0300 Subject: [PATCH] Proper enhancement for SAM adapters --- .../java/components/signatureEnhancement.kt | 24 +------------ .../sam/SamAdapterFunctionDescriptor.java | 27 +++++++++++++-- .../descriptors/JavaMethodDescriptor.java | 34 +++++++++++++++---- 3 files changed, 54 insertions(+), 31 deletions(-) diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/components/signatureEnhancement.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/components/signatureEnhancement.kt index 1b01e6993c3..6272f43a9b5 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/components/signatureEnhancement.kt +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/components/signatureEnhancement.kt @@ -47,30 +47,8 @@ fun D.enhance(): D { val enhancedReturnType = parts { it.getReturnType()!!.toReturnTypePart() }.enhance() if (this is JavaMethodDescriptor) { - val enhancedFunction = JavaMethodDescriptor.createJavaMethod( - getContainingDeclaration()!!, - getAnnotations(), - getName(), - getSource() - ) - enhancedFunction.initialize( - enhancedReceiverType, - getDispatchReceiverParameter(), - getTypeParameters(), - enhancedValueParameters, - enhancedReturnType, - getModality(), - getVisibility() - ) - enhancedFunction.setHasStableParameterNames(hasStableParameterNames()) - enhancedFunction.setHasSynthesizedParameterNames(hasSynthesizedParameterNames()) - - for (overridden in getOverriddenDescriptors()) { - enhancedFunction.addOverriddenDescriptor(overridden) - } - @suppress("UNCHECKED_CAST") - return enhancedFunction as D + return this.enhance(enhancedReceiverType, enhancedValueParameters, enhancedReturnType) as D } return this diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/sam/SamAdapterFunctionDescriptor.java b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/sam/SamAdapterFunctionDescriptor.java index db1a506d2e8..20c5158fa42 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/sam/SamAdapterFunctionDescriptor.java +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/sam/SamAdapterFunctionDescriptor.java @@ -17,6 +17,10 @@ package org.jetbrains.kotlin.load.java.sam; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.kotlin.descriptors.DeclarationDescriptor; +import org.jetbrains.kotlin.descriptors.FunctionDescriptor; +import org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor; import org.jetbrains.kotlin.load.java.descriptors.JavaMethodDescriptor; import org.jetbrains.kotlin.load.java.descriptors.SamAdapterDescriptor; @@ -24,8 +28,17 @@ import org.jetbrains.kotlin.load.java.descriptors.SamAdapterDescriptor; private final JavaMethodDescriptor declaration; public SamAdapterFunctionDescriptor(@NotNull JavaMethodDescriptor declaration) { - super(declaration.getContainingDeclaration(), null, declaration.getAnnotations(), - declaration.getName(), Kind.SYNTHESIZED, declaration.getSource()); + this(declaration.getContainingDeclaration(), null, Kind.SYNTHESIZED, declaration); + } + + private SamAdapterFunctionDescriptor( + @NotNull DeclarationDescriptor containingDeclaration, + @Nullable SimpleFunctionDescriptor original, + @NotNull Kind kind, + @NotNull JavaMethodDescriptor declaration + ) { + super(containingDeclaration, original, declaration.getAnnotations(), + declaration.getName(), kind, declaration.getSource()); this.declaration = declaration; setHasStableParameterNames(declaration.hasStableParameterNames()); setHasSynthesizedParameterNames(declaration.hasSynthesizedParameterNames()); @@ -36,4 +49,14 @@ import org.jetbrains.kotlin.load.java.descriptors.SamAdapterDescriptor; public JavaMethodDescriptor getOriginForSam() { return declaration; } + + @NotNull + @Override + protected JavaMethodDescriptor createSubstitutedCopy( + @NotNull DeclarationDescriptor newOwner, + @Nullable FunctionDescriptor original, + @NotNull Kind kind + ) { + return new SamAdapterFunctionDescriptor(newOwner, (SimpleFunctionDescriptor) original, kind, declaration); + } } diff --git a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/descriptors/JavaMethodDescriptor.java b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/descriptors/JavaMethodDescriptor.java index 92bb1269271..99cff0089bc 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/descriptors/JavaMethodDescriptor.java +++ b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/descriptors/JavaMethodDescriptor.java @@ -18,14 +18,13 @@ package org.jetbrains.kotlin.load.java.descriptors; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.jetbrains.kotlin.descriptors.DeclarationDescriptor; -import org.jetbrains.kotlin.descriptors.FunctionDescriptor; -import org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor; -import org.jetbrains.kotlin.descriptors.SourceElement; +import org.jetbrains.kotlin.descriptors.*; import org.jetbrains.kotlin.descriptors.annotations.Annotations; -import org.jetbrains.kotlin.descriptors.impl.FunctionDescriptorImpl; import org.jetbrains.kotlin.descriptors.impl.SimpleFunctionDescriptorImpl; import org.jetbrains.kotlin.name.Name; +import org.jetbrains.kotlin.types.JetType; + +import java.util.List; public class JavaMethodDescriptor extends SimpleFunctionDescriptorImpl implements JavaCallableMemberDescriptor { private Boolean hasStableParameterNames = null; @@ -74,7 +73,7 @@ public class JavaMethodDescriptor extends SimpleFunctionDescriptorImpl implement @NotNull @Override - protected FunctionDescriptorImpl createSubstitutedCopy( + protected JavaMethodDescriptor createSubstitutedCopy( @NotNull DeclarationDescriptor newOwner, @Nullable FunctionDescriptor original, @NotNull Kind kind @@ -91,4 +90,27 @@ public class JavaMethodDescriptor extends SimpleFunctionDescriptorImpl implement result.setHasSynthesizedParameterNames(hasSynthesizedParameterNames()); return result; } + + @NotNull + public JavaMethodDescriptor enhance( + @Nullable JetType enhancedReceiverType, + @NotNull List enhancedValueParameters, + @NotNull JetType enhancedReturnType + ) { + JavaMethodDescriptor enhancedMethod = createSubstitutedCopy(getContainingDeclaration(), getOriginal(), getKind()); + enhancedMethod.initialize( + enhancedReceiverType, + getDispatchReceiverParameter(), + getTypeParameters(), + enhancedValueParameters, + enhancedReturnType, + getModality(), + getVisibility() + ); + for (FunctionDescriptor overridden : getOverriddenDescriptors()) { + enhancedMethod.addOverriddenDescriptor(overridden); + } + + return enhancedMethod; + } }