Proper enhancement for SAM adapters

This commit is contained in:
Andrey Breslav
2015-04-23 18:23:08 +03:00
committed by Denis Zharkov
parent 9644eeb047
commit a8b5698145
3 changed files with 54 additions and 31 deletions
@@ -47,30 +47,8 @@ fun <D : CallableMemberDescriptor> 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
@@ -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);
}
}
@@ -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<ValueParameterDescriptor> 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;
}
}