Release original Java descriptors while enhancing
It's both more correct and helps to release memory retained by descriptor before enhancement
This commit is contained in:
+27
@@ -17,6 +17,9 @@
|
|||||||
package org.jetbrains.kotlin.load.java.sam;
|
package org.jetbrains.kotlin.load.java.sam;
|
||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import org.jetbrains.kotlin.descriptors.*;
|
||||||
|
import org.jetbrains.kotlin.descriptors.annotations.Annotations;
|
||||||
import org.jetbrains.kotlin.load.java.descriptors.JavaConstructorDescriptor;
|
import org.jetbrains.kotlin.load.java.descriptors.JavaConstructorDescriptor;
|
||||||
import org.jetbrains.kotlin.load.java.descriptors.SamAdapterDescriptor;
|
import org.jetbrains.kotlin.load.java.descriptors.SamAdapterDescriptor;
|
||||||
|
|
||||||
@@ -31,6 +34,30 @@ import org.jetbrains.kotlin.load.java.descriptors.SamAdapterDescriptor;
|
|||||||
setHasSynthesizedParameterNames(declaration.hasSynthesizedParameterNames());
|
setHasSynthesizedParameterNames(declaration.hasSynthesizedParameterNames());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private SamAdapterConstructorDescriptor(
|
||||||
|
@NotNull ClassDescriptor containingDeclaration,
|
||||||
|
@Nullable JavaConstructorDescriptor original,
|
||||||
|
@NotNull Annotations annotations,
|
||||||
|
boolean isPrimary,
|
||||||
|
@NotNull Kind kind,
|
||||||
|
@NotNull SourceElement source,
|
||||||
|
@NotNull JavaConstructorDescriptor declaration
|
||||||
|
) {
|
||||||
|
super(containingDeclaration, original, annotations, isPrimary, kind, source);
|
||||||
|
this.declaration = declaration;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
protected JavaConstructorDescriptor createDescriptor(
|
||||||
|
@NotNull ClassDescriptor newOwner,
|
||||||
|
@Nullable JavaConstructorDescriptor original,
|
||||||
|
@NotNull Kind kind,
|
||||||
|
@NotNull SourceElement sourceElement
|
||||||
|
) {
|
||||||
|
return new SamAdapterConstructorDescriptor(newOwner, original, getAnnotations(), isPrimary, kind, sourceElement, declaration);
|
||||||
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public JavaConstructorDescriptor getOriginForSam() {
|
public JavaConstructorDescriptor getOriginForSam() {
|
||||||
|
|||||||
+1
-4
@@ -22,7 +22,6 @@ import org.jetbrains.kotlin.descriptors.annotations.Annotations
|
|||||||
import org.jetbrains.kotlin.descriptors.impl.SimpleFunctionDescriptorImpl
|
import org.jetbrains.kotlin.descriptors.impl.SimpleFunctionDescriptorImpl
|
||||||
import org.jetbrains.kotlin.incremental.components.LookupLocation
|
import org.jetbrains.kotlin.incremental.components.LookupLocation
|
||||||
import org.jetbrains.kotlin.load.java.sam.SingleAbstractMethodUtils
|
import org.jetbrains.kotlin.load.java.sam.SingleAbstractMethodUtils
|
||||||
import org.jetbrains.kotlin.load.java.typeEnhancement.enhanceSignature
|
|
||||||
import org.jetbrains.kotlin.name.Name
|
import org.jetbrains.kotlin.name.Name
|
||||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||||
import org.jetbrains.kotlin.resolve.descriptorUtil.parentsWithSelf
|
import org.jetbrains.kotlin.resolve.descriptorUtil.parentsWithSelf
|
||||||
@@ -47,9 +46,7 @@ class SamAdapterFunctionsScope(storageManager: StorageManager) : SyntheticScope
|
|||||||
if (!function.hasJavaOriginInHierarchy()) return null //TODO: should we go into base at all?
|
if (!function.hasJavaOriginInHierarchy()) return null //TODO: should we go into base at all?
|
||||||
if (!SingleAbstractMethodUtils.isSamAdapterNecessary(function)) return null
|
if (!SingleAbstractMethodUtils.isSamAdapterNecessary(function)) return null
|
||||||
if (function.returnType == null) return null
|
if (function.returnType == null) return null
|
||||||
//TODO: it's a temporary hack while original returns a function with platform types
|
return MyFunctionDescriptor.create(function)
|
||||||
val enhancedFunction = function.enhanceSignature()
|
|
||||||
return MyFunctionDescriptor.create(enhancedFunction)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getSyntheticExtensionFunctions(receiverTypes: Collection<KotlinType>, name: Name, location: LookupLocation): Collection<FunctionDescriptor> {
|
override fun getSyntheticExtensionFunctions(receiverTypes: Collection<KotlinType>, name: Name, location: LookupLocation): Collection<FunctionDescriptor> {
|
||||||
|
|||||||
+16
-4
@@ -91,14 +91,26 @@ public class JavaConstructorDescriptor extends ConstructorDescriptorImpl impleme
|
|||||||
|
|
||||||
assert newName == null : "Attempt to rename constructor: " + this;
|
assert newName == null : "Attempt to rename constructor: " + this;
|
||||||
|
|
||||||
JavaConstructorDescriptor result = new JavaConstructorDescriptor(
|
JavaConstructorDescriptor result = createDescriptor((ClassDescriptor) newOwner, (JavaConstructorDescriptor) original, kind,
|
||||||
(ClassDescriptor) newOwner, this, getAnnotations(), isPrimary, kind, getSourceToUseForCopy(preserveSource, original)
|
getSourceToUseForCopy(preserveSource, original));
|
||||||
);
|
|
||||||
result.setHasStableParameterNames(hasStableParameterNames());
|
result.setHasStableParameterNames(hasStableParameterNames());
|
||||||
result.setHasSynthesizedParameterNames(hasSynthesizedParameterNames());
|
result.setHasSynthesizedParameterNames(hasSynthesizedParameterNames());
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
protected JavaConstructorDescriptor createDescriptor(
|
||||||
|
@NotNull ClassDescriptor newOwner,
|
||||||
|
@Nullable JavaConstructorDescriptor original,
|
||||||
|
@NotNull Kind kind,
|
||||||
|
@NotNull SourceElement sourceElement
|
||||||
|
) {
|
||||||
|
return new JavaConstructorDescriptor(
|
||||||
|
newOwner, original, getAnnotations(), isPrimary, kind,
|
||||||
|
sourceElement
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@NotNull
|
@NotNull
|
||||||
public JavaConstructorDescriptor enhance(
|
public JavaConstructorDescriptor enhance(
|
||||||
@@ -106,7 +118,7 @@ public class JavaConstructorDescriptor extends ConstructorDescriptorImpl impleme
|
|||||||
@NotNull List<KotlinType> enhancedValueParametersTypes,
|
@NotNull List<KotlinType> enhancedValueParametersTypes,
|
||||||
@NotNull KotlinType enhancedReturnType
|
@NotNull KotlinType enhancedReturnType
|
||||||
) {
|
) {
|
||||||
JavaConstructorDescriptor enhanced = createSubstitutedCopy(getContainingDeclaration(), getOriginal(), getKind(), null, false);
|
JavaConstructorDescriptor enhanced = createSubstitutedCopy(getContainingDeclaration(), /* original = */ null, getKind(), null, false);
|
||||||
// We do not use doSubstitute here as in JavaMethodDescriptor.enhance because type parameters of constructor belongs to class
|
// We do not use doSubstitute here as in JavaMethodDescriptor.enhance because type parameters of constructor belongs to class
|
||||||
enhanced.initialize(
|
enhanced.initialize(
|
||||||
enhancedReceiverType,
|
enhancedReceiverType,
|
||||||
|
|||||||
+2
-4
@@ -138,15 +138,13 @@ public class JavaMethodDescriptor extends SimpleFunctionDescriptorImpl implement
|
|||||||
List<ValueParameterDescriptor> enhancedValueParameters =
|
List<ValueParameterDescriptor> enhancedValueParameters =
|
||||||
UtilKt.copyValueParameters(enhancedValueParametersTypes, getValueParameters(), this);
|
UtilKt.copyValueParameters(enhancedValueParametersTypes, getValueParameters(), this);
|
||||||
|
|
||||||
// We use `doSubstitute` here because it does exactly what we need:
|
|
||||||
// 1. creates full copy of descriptor
|
|
||||||
// 2. copies method's type parameters (with new containing declaration) and properly substitute to them in value parameters, return type and etc.
|
|
||||||
JavaMethodDescriptor enhancedMethod =
|
JavaMethodDescriptor enhancedMethod =
|
||||||
(JavaMethodDescriptor) newCopyBuilder()
|
(JavaMethodDescriptor) newCopyBuilder()
|
||||||
.setValueParameters(enhancedValueParameters)
|
.setValueParameters(enhancedValueParameters)
|
||||||
.setReturnType(enhancedReturnType)
|
.setReturnType(enhancedReturnType)
|
||||||
.setExtensionReceiverType(enhancedReceiverType)
|
.setExtensionReceiverType(enhancedReceiverType)
|
||||||
.setOriginal(getOriginal())
|
.setDropOriginalInContainingParts()
|
||||||
|
.setPreserveSourceElement()
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
assert enhancedMethod != null : "null after substitution while enhancing " + toString();
|
assert enhancedMethod != null : "null after substitution while enhancing " + toString();
|
||||||
|
|||||||
+1
-1
@@ -26,7 +26,7 @@ fun <D : CallableMemberDescriptor> enhanceSignatures(platformSignatures: Collect
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <D : CallableMemberDescriptor> D.enhanceSignature(): D {
|
private fun <D : CallableMemberDescriptor> D.enhanceSignature(): D {
|
||||||
// TODO type parameters
|
// TODO type parameters
|
||||||
// TODO use new type parameters while enhancing other types
|
// TODO use new type parameters while enhancing other types
|
||||||
// TODO Propagation into generic type arguments
|
// TODO Propagation into generic type arguments
|
||||||
|
|||||||
+28
-9
@@ -291,8 +291,10 @@ public abstract class FunctionDescriptorImpl extends DeclarationDescriptorNonRoo
|
|||||||
protected @Nullable KotlinType newExtensionReceiverParameterType;
|
protected @Nullable KotlinType newExtensionReceiverParameterType;
|
||||||
protected @NotNull KotlinType newReturnType;
|
protected @NotNull KotlinType newReturnType;
|
||||||
protected @Nullable Name name;
|
protected @Nullable Name name;
|
||||||
protected boolean copyOverrides;
|
protected boolean copyOverrides = true;
|
||||||
protected boolean signatureChange;
|
protected boolean signatureChange = false;
|
||||||
|
protected boolean preserveSourceElement = false;
|
||||||
|
protected boolean dropOriginalInContainingParts = false;
|
||||||
private boolean isHiddenToOvercomeSignatureClash;
|
private boolean isHiddenToOvercomeSignatureClash;
|
||||||
private List<TypeParameterDescriptor> newTypeParameters = null;
|
private List<TypeParameterDescriptor> newTypeParameters = null;
|
||||||
|
|
||||||
@@ -316,8 +318,6 @@ public abstract class FunctionDescriptorImpl extends DeclarationDescriptorNonRoo
|
|||||||
this.newExtensionReceiverParameterType = newExtensionReceiverParameterType;
|
this.newExtensionReceiverParameterType = newExtensionReceiverParameterType;
|
||||||
this.newReturnType = newReturnType;
|
this.newReturnType = newReturnType;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.copyOverrides = true;
|
|
||||||
this.signatureChange = false;
|
|
||||||
this.isHiddenToOvercomeSignatureClash = isHiddenToOvercomeSignatureClash();
|
this.isHiddenToOvercomeSignatureClash = isHiddenToOvercomeSignatureClash();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -391,12 +391,25 @@ public abstract class FunctionDescriptorImpl extends DeclarationDescriptorNonRoo
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public CopyConfiguration setPreserveSourceElement() {
|
||||||
|
this.preserveSourceElement = true;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public CopyConfiguration setDropOriginalInContainingParts() {
|
||||||
|
this.dropOriginalInContainingParts = true;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public CopyConfiguration setHidden() {
|
public CopyConfiguration setHidden() {
|
||||||
isHiddenToOvercomeSignatureClash = true;
|
isHiddenToOvercomeSignatureClash = true;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
public FunctionDescriptor build() {
|
public FunctionDescriptor build() {
|
||||||
return doSubstitute(this);
|
return doSubstitute(this);
|
||||||
}
|
}
|
||||||
@@ -429,7 +442,7 @@ public abstract class FunctionDescriptorImpl extends DeclarationDescriptorNonRoo
|
|||||||
protected FunctionDescriptor doSubstitute(@NotNull CopyConfiguration configuration) {
|
protected FunctionDescriptor doSubstitute(@NotNull CopyConfiguration configuration) {
|
||||||
FunctionDescriptorImpl substitutedDescriptor = createSubstitutedCopy(
|
FunctionDescriptorImpl substitutedDescriptor = createSubstitutedCopy(
|
||||||
configuration.newOwner, configuration.original, configuration.kind, configuration.name,
|
configuration.newOwner, configuration.original, configuration.kind, configuration.name,
|
||||||
/* preserveSource = */ configuration.signatureChange);
|
configuration.preserveSourceElement);
|
||||||
|
|
||||||
List<TypeParameterDescriptor> substitutedTypeParameters;
|
List<TypeParameterDescriptor> substitutedTypeParameters;
|
||||||
TypeSubstitutor substitutor;
|
TypeSubstitutor substitutor;
|
||||||
@@ -474,7 +487,7 @@ public abstract class FunctionDescriptorImpl extends DeclarationDescriptorNonRoo
|
|||||||
}
|
}
|
||||||
|
|
||||||
List<ValueParameterDescriptor> substitutedValueParameters = getSubstitutedValueParameters(
|
List<ValueParameterDescriptor> substitutedValueParameters = getSubstitutedValueParameters(
|
||||||
substitutedDescriptor, configuration.newValueParameterDescriptors, substitutor
|
substitutedDescriptor, configuration.newValueParameterDescriptors, substitutor, configuration.dropOriginalInContainingParts
|
||||||
);
|
);
|
||||||
if (substitutedValueParameters == null) {
|
if (substitutedValueParameters == null) {
|
||||||
return null;
|
return null;
|
||||||
@@ -511,7 +524,12 @@ public abstract class FunctionDescriptorImpl extends DeclarationDescriptorNonRoo
|
|||||||
|
|
||||||
if (configuration.copyOverrides) {
|
if (configuration.copyOverrides) {
|
||||||
for (FunctionDescriptor overriddenFunction : overriddenFunctions) {
|
for (FunctionDescriptor overriddenFunction : overriddenFunctions) {
|
||||||
substitutedDescriptor.addOverriddenDescriptor(overriddenFunction.substitute(substitutor));
|
if (configuration.originalSubstitutor.isEmpty()) {
|
||||||
|
substitutedDescriptor.addOverriddenDescriptor(overriddenFunction);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
substitutedDescriptor.addOverriddenDescriptor(overriddenFunction.substitute(substitutor));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -543,7 +561,8 @@ public abstract class FunctionDescriptorImpl extends DeclarationDescriptorNonRoo
|
|||||||
public static List<ValueParameterDescriptor> getSubstitutedValueParameters(
|
public static List<ValueParameterDescriptor> getSubstitutedValueParameters(
|
||||||
FunctionDescriptor substitutedDescriptor,
|
FunctionDescriptor substitutedDescriptor,
|
||||||
@NotNull List<ValueParameterDescriptor> unsubstitutedValueParameters,
|
@NotNull List<ValueParameterDescriptor> unsubstitutedValueParameters,
|
||||||
@NotNull TypeSubstitutor substitutor
|
@NotNull TypeSubstitutor substitutor,
|
||||||
|
boolean dropOriginal
|
||||||
) {
|
) {
|
||||||
List<ValueParameterDescriptor> result = new ArrayList<ValueParameterDescriptor>(unsubstitutedValueParameters.size());
|
List<ValueParameterDescriptor> result = new ArrayList<ValueParameterDescriptor>(unsubstitutedValueParameters.size());
|
||||||
for (ValueParameterDescriptor unsubstitutedValueParameter : unsubstitutedValueParameters) {
|
for (ValueParameterDescriptor unsubstitutedValueParameter : unsubstitutedValueParameters) {
|
||||||
@@ -556,7 +575,7 @@ public abstract class FunctionDescriptorImpl extends DeclarationDescriptorNonRoo
|
|||||||
result.add(
|
result.add(
|
||||||
new ValueParameterDescriptorImpl(
|
new ValueParameterDescriptorImpl(
|
||||||
substitutedDescriptor,
|
substitutedDescriptor,
|
||||||
unsubstitutedValueParameter,
|
dropOriginal ? null : unsubstitutedValueParameter,
|
||||||
unsubstitutedValueParameter.getIndex(),
|
unsubstitutedValueParameter.getIndex(),
|
||||||
unsubstitutedValueParameter.getAnnotations(),
|
unsubstitutedValueParameter.getAnnotations(),
|
||||||
unsubstitutedValueParameter.getName(),
|
unsubstitutedValueParameter.getName(),
|
||||||
|
|||||||
+1
-1
@@ -272,7 +272,7 @@ public class PropertyDescriptorImpl extends VariableDescriptorWithInitializerImp
|
|||||||
);
|
);
|
||||||
if (newSetter != null) {
|
if (newSetter != null) {
|
||||||
List<ValueParameterDescriptor> substitutedValueParameters = FunctionDescriptorImpl.getSubstitutedValueParameters(
|
List<ValueParameterDescriptor> substitutedValueParameters = FunctionDescriptorImpl.getSubstitutedValueParameters(
|
||||||
newSetter, setter.getValueParameters(), substitutor
|
newSetter, setter.getValueParameters(), substitutor, /* dropOriginal = */ false
|
||||||
);
|
);
|
||||||
if (substitutedValueParameters == null) {
|
if (substitutedValueParameters == null) {
|
||||||
// The setter is projected out, e.g. in this case:
|
// The setter is projected out, e.g. in this case:
|
||||||
|
|||||||
+4
-2
@@ -113,14 +113,16 @@ public class SimpleFunctionDescriptorImpl extends FunctionDescriptorImpl impleme
|
|||||||
@Override
|
@Override
|
||||||
public SimpleFunctionDescriptor createRenamedCopy(@NotNull Name name) {
|
public SimpleFunctionDescriptor createRenamedCopy(@NotNull Name name) {
|
||||||
//noinspection ConstantConditions
|
//noinspection ConstantConditions
|
||||||
return (SimpleFunctionDescriptor) newCopyBuilder().setName(name).setSignatureChange().build();
|
return (SimpleFunctionDescriptor) newCopyBuilder().setName(name).setSignatureChange().setPreserveSourceElement().build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public SimpleFunctionDescriptor createCopyWithNewValueParameters(@NotNull List<ValueParameterDescriptor> valueParameters) {
|
public SimpleFunctionDescriptor createCopyWithNewValueParameters(@NotNull List<ValueParameterDescriptor> valueParameters) {
|
||||||
//noinspection ConstantConditions
|
//noinspection ConstantConditions
|
||||||
return (SimpleFunctionDescriptor) newCopyBuilder().setValueParameters(valueParameters).setSignatureChange().build();
|
return (SimpleFunctionDescriptor) newCopyBuilder()
|
||||||
|
.setValueParameters(valueParameters)
|
||||||
|
.setSignatureChange().setPreserveSourceElement().build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
|
|||||||
Reference in New Issue
Block a user