Remove NotNull PSI element parameter from PropertyCodegen.generateBackingField

#KT-31131 Fixed
This commit is contained in:
Alexander Udalov
2019-04-24 12:09:05 +02:00
parent e9b50157da
commit 967a6bd80d
3 changed files with 57 additions and 67 deletions
@@ -195,7 +195,7 @@ public abstract class ClassBodyCodegen extends MemberCodegen<KtPureClassOrObject
); );
} }
else { else {
propertyCodegen.generatePrimaryConstructorProperty(p, propertyDescriptor); propertyCodegen.generatePrimaryConstructorProperty(propertyDescriptor);
} }
} }
} }
@@ -106,8 +106,9 @@ public class PropertyCodegen {
); );
} }
PropertyDescriptor propertyDescriptor = (PropertyDescriptor) variableDescriptor; if (!UnderscoreUtilKt.isSingleUnderscore(entry)) {
genDestructuringDeclaration(entry, propertyDescriptor); genDestructuringDeclaration((PropertyDescriptor) variableDescriptor);
}
} }
public void generateInPackageFacade(@NotNull DeserializedPropertyDescriptor deserializedProperty) { public void generateInPackageFacade(@NotNull DeserializedPropertyDescriptor deserializedProperty) {
@@ -125,15 +126,15 @@ public class PropertyCodegen {
kind == OwnerKind.DEFAULT_IMPLS || kind == OwnerKind.ERASED_INLINE_CLASS kind == OwnerKind.DEFAULT_IMPLS || kind == OwnerKind.ERASED_INLINE_CLASS
: "Generating property with a wrong kind (" + kind + "): " + descriptor; : "Generating property with a wrong kind (" + kind + "): " + descriptor;
genBackingFieldAndAnnotations(declaration, descriptor); genBackingFieldAndAnnotations(descriptor);
boolean isDefaultGetterAndSetter = isDefaultAccessor(getter) && isDefaultAccessor(setter); boolean isDefaultGetterAndSetter = isDefaultAccessor(getter) && isDefaultAccessor(setter);
if (isAccessorNeeded(declaration, descriptor, getter, isDefaultGetterAndSetter)) { if (isAccessorNeeded(declaration, descriptor, getter, isDefaultGetterAndSetter)) {
generateGetter(declaration, descriptor, getter); generateGetter(descriptor, getter);
} }
if (isAccessorNeeded(declaration, descriptor, setter, isDefaultGetterAndSetter)) { if (isAccessorNeeded(declaration, descriptor, setter, isDefaultGetterAndSetter)) {
generateSetter(declaration, descriptor, setter); generateSetter(descriptor, setter);
} }
} }
@@ -141,29 +142,23 @@ public class PropertyCodegen {
return accessor == null || !accessor.hasBody(); return accessor == null || !accessor.hasBody();
} }
private void genDestructuringDeclaration( private void genDestructuringDeclaration(@NotNull PropertyDescriptor descriptor) {
@NotNull KtDestructuringDeclarationEntry entry,
@NotNull PropertyDescriptor descriptor
) {
assert kind == OwnerKind.PACKAGE || kind == OwnerKind.IMPLEMENTATION || kind == OwnerKind.DEFAULT_IMPLS assert kind == OwnerKind.PACKAGE || kind == OwnerKind.IMPLEMENTATION || kind == OwnerKind.DEFAULT_IMPLS
: "Generating property with a wrong kind (" + kind + "): " + descriptor; : "Generating property with a wrong kind (" + kind + "): " + descriptor;
if (UnderscoreUtilKt.isSingleUnderscore(entry)) return; genBackingFieldAndAnnotations(descriptor);
genBackingFieldAndAnnotations(entry, descriptor); generateGetter(descriptor, null);
generateSetter(descriptor, null);
generateGetter(entry, descriptor, null);
generateSetter(entry, descriptor, null);
} }
private void genBackingFieldAndAnnotations(@Nullable KtNamedDeclaration declaration, @NotNull PropertyDescriptor descriptor) { private void genBackingFieldAndAnnotations(@NotNull PropertyDescriptor descriptor) {
// Fields and '$annotations' methods for non-private const properties are generated in the multi-file facade // Fields and '$annotations' methods for non-private const properties are generated in the multi-file facade
boolean isBackingFieldOwner = descriptor.isConst() && !Visibilities.isPrivate(descriptor.getVisibility()) boolean isBackingFieldOwner = descriptor.isConst() && !Visibilities.isPrivate(descriptor.getVisibility())
? !(context instanceof MultifileClassPartContext) ? !(context instanceof MultifileClassPartContext)
: CodegenContextUtil.isImplementationOwner(context, descriptor); : CodegenContextUtil.isImplementationOwner(context, descriptor);
assert declaration != null : "Declaration is null: " + descriptor + " (context=" + context + ")"; generateBackingField(descriptor, isBackingFieldOwner);
generateBackingField(declaration, descriptor, isBackingFieldOwner);
generateSyntheticMethodIfNeeded(descriptor, isBackingFieldOwner); generateSyntheticMethodIfNeeded(descriptor, isBackingFieldOwner);
} }
@@ -244,12 +239,12 @@ public class PropertyCodegen {
} }
} }
public void generatePrimaryConstructorProperty(@NotNull KtParameter parameter, @NotNull PropertyDescriptor descriptor) { public void generatePrimaryConstructorProperty(@NotNull PropertyDescriptor descriptor) {
genBackingFieldAndAnnotations(parameter, descriptor); genBackingFieldAndAnnotations(descriptor);
if (areAccessorsNeededForPrimaryConstructorProperty(descriptor, context.getContextKind())) { if (areAccessorsNeededForPrimaryConstructorProperty(descriptor, context.getContextKind())) {
generateGetter(parameter, descriptor, null); generateGetter(descriptor, null);
generateSetter(parameter, descriptor, null); generateSetter(descriptor, null);
} }
} }
@@ -312,17 +307,14 @@ public class PropertyCodegen {
return null; return null;
} }
private void generateBackingField( private void generateBackingField(@NotNull PropertyDescriptor descriptor, boolean isBackingFieldOwner) {
@NotNull KtNamedDeclaration p,
@NotNull PropertyDescriptor descriptor,
boolean isBackingFieldOwner
) {
if (isJvmInterface(descriptor.getContainingDeclaration()) || kind == OwnerKind.DEFAULT_IMPLS || if (isJvmInterface(descriptor.getContainingDeclaration()) || kind == OwnerKind.DEFAULT_IMPLS ||
kind == OwnerKind.ERASED_INLINE_CLASS) { kind == OwnerKind.ERASED_INLINE_CLASS) {
return; return;
} }
boolean isDelegate = p instanceof KtProperty && ((KtProperty) p).hasDelegate(); @SuppressWarnings("deprecation")
boolean isDelegate = descriptor.isDelegated();
Object defaultValue; Object defaultValue;
if (isDelegate) { if (isDelegate) {
@@ -341,7 +333,7 @@ public class PropertyCodegen {
return; return;
} }
generateBackingField(p, descriptor, isDelegate, defaultValue, isBackingFieldOwner); generateBackingField(descriptor, isDelegate, defaultValue, isBackingFieldOwner);
} }
// Annotations on properties are stored in bytecode on an empty synthetic method. This way they're still // Annotations on properties are stored in bytecode on an empty synthetic method. This way they're still
@@ -364,7 +356,6 @@ public class PropertyCodegen {
} }
private void generateBackingField( private void generateBackingField(
@NotNull KtNamedDeclaration element,
@NotNull PropertyDescriptor propertyDescriptor, @NotNull PropertyDescriptor propertyDescriptor,
boolean isDelegate, boolean isDelegate,
@Nullable Object defaultValue, @Nullable Object defaultValue,
@@ -390,8 +381,7 @@ public class PropertyCodegen {
modifiers |= ACC_SYNTHETIC; modifiers |= ACC_SYNTHETIC;
} }
KotlinType kotlinType = isDelegate ? getDelegateTypeForProperty((KtProperty) element, propertyDescriptor, bindingContext) KotlinType kotlinType = isDelegate ? getDelegateTypeForProperty(propertyDescriptor, bindingContext) : propertyDescriptor.getType();
: propertyDescriptor.getType();
Type type = typeMapper.mapType(kotlinType); Type type = typeMapper.mapType(kotlinType);
ClassBuilder builder = v; ClassBuilder builder = v;
@@ -419,7 +409,7 @@ public class PropertyCodegen {
if (isBackingFieldOwner) { if (isBackingFieldOwner) {
FieldVisitor fv = builder.newField( FieldVisitor fv = builder.newField(
JvmDeclarationOriginKt.OtherOrigin(element, propertyDescriptor), modifiers, name, type.getDescriptor(), JvmDeclarationOriginKt.OtherOrigin(propertyDescriptor), modifiers, name, type.getDescriptor(),
isDelegate ? null : typeMapper.mapFieldSignature(kotlinType, propertyDescriptor), defaultValue isDelegate ? null : typeMapper.mapFieldSignature(kotlinType, propertyDescriptor), defaultValue
); );
@@ -431,22 +421,25 @@ public class PropertyCodegen {
@NotNull @NotNull
public static KotlinType getDelegateTypeForProperty( public static KotlinType getDelegateTypeForProperty(
@NotNull KtProperty p,
@NotNull PropertyDescriptor propertyDescriptor, @NotNull PropertyDescriptor propertyDescriptor,
@NotNull BindingContext bindingContext @NotNull BindingContext bindingContext
) { ) {
KotlinType delegateType = null;
ResolvedCall<FunctionDescriptor> provideDelegateResolvedCall = ResolvedCall<FunctionDescriptor> provideDelegateResolvedCall =
bindingContext.get(BindingContext.PROVIDE_DELEGATE_RESOLVED_CALL, propertyDescriptor); bindingContext.get(BindingContext.PROVIDE_DELEGATE_RESOLVED_CALL, propertyDescriptor);
KtExpression delegateExpression = p.getDelegateExpression();
KtProperty property = (KtProperty) DescriptorToSourceUtils.descriptorToDeclaration(propertyDescriptor);
KtExpression delegateExpression = property != null ? property.getDelegateExpression() : null;
KotlinType delegateType;
if (provideDelegateResolvedCall != null) { if (provideDelegateResolvedCall != null) {
delegateType = provideDelegateResolvedCall.getResultingDescriptor().getReturnType(); delegateType = provideDelegateResolvedCall.getResultingDescriptor().getReturnType();
} }
else if (delegateExpression != null) { else if (delegateExpression != null) {
delegateType = bindingContext.getType(delegateExpression); delegateType = bindingContext.getType(delegateExpression);
} }
else {
delegateType = null;
}
if (delegateType == null) { if (delegateType == null) {
// Delegation convention is unresolved // Delegation convention is unresolved
@@ -469,51 +462,49 @@ public class PropertyCodegen {
return false; return false;
} }
private void generateGetter( private void generateGetter(@NotNull PropertyDescriptor descriptor, @Nullable KtPropertyAccessor getter) {
@Nullable KtNamedDeclaration p, @NotNull PropertyDescriptor descriptor, @Nullable KtPropertyAccessor getter generateAccessor(
) { getter,
generateAccessor(p, getter, descriptor.getGetter() != null descriptor.getGetter() != null ? descriptor.getGetter() : DescriptorFactory.createDefaultGetter(
? descriptor.getGetter() descriptor, Annotations.Companion.getEMPTY()
: DescriptorFactory.createDefaultGetter(descriptor, Annotations.Companion.getEMPTY())); )
);
} }
private void generateSetter( private void generateSetter(@NotNull PropertyDescriptor descriptor, @Nullable KtPropertyAccessor setter) {
@Nullable KtNamedDeclaration p, @NotNull PropertyDescriptor descriptor, @Nullable KtPropertyAccessor setter
) {
if (!descriptor.isVar()) return; if (!descriptor.isVar()) return;
generateAccessor(p, setter, descriptor.getSetter() != null generateAccessor(
? descriptor.getSetter() setter,
: DescriptorFactory.createDefaultSetter( descriptor.getSetter() != null ? descriptor.getSetter() : DescriptorFactory.createDefaultSetter(
descriptor, Annotations.Companion.getEMPTY(), Annotations.Companion.getEMPTY() descriptor, Annotations.Companion.getEMPTY(), Annotations.Companion.getEMPTY()
)); )
);
} }
private void generateAccessor( private void generateAccessor(@Nullable KtPropertyAccessor accessor, @NotNull PropertyAccessorDescriptor descriptor) {
@Nullable KtNamedDeclaration p,
@Nullable KtPropertyAccessor accessor,
@NotNull PropertyAccessorDescriptor accessorDescriptor
) {
if (context instanceof MultifileClassFacadeContext && if (context instanceof MultifileClassFacadeContext &&
(Visibilities.isPrivate(accessorDescriptor.getVisibility()) || (Visibilities.isPrivate(descriptor.getVisibility()) ||
AsmUtil.getVisibilityAccessFlag(accessorDescriptor) == Opcodes.ACC_PRIVATE)) { AsmUtil.getVisibilityAccessFlag(descriptor) == Opcodes.ACC_PRIVATE)) {
return; return;
} }
FunctionGenerationStrategy strategy; FunctionGenerationStrategy strategy;
if (accessor == null || !accessor.hasBody()) { if (accessor == null || !accessor.hasBody()) {
if (p instanceof KtProperty && ((KtProperty) p).hasDelegate()) { @SuppressWarnings("deprecation")
strategy = new DelegatedPropertyAccessorStrategy(state, accessorDescriptor); boolean isDelegated = descriptor.getCorrespondingProperty().isDelegated();
if (isDelegated) {
strategy = new DelegatedPropertyAccessorStrategy(state, descriptor);
} }
else { else {
strategy = new DefaultPropertyAccessorStrategy(state, accessorDescriptor); strategy = new DefaultPropertyAccessorStrategy(state, descriptor);
} }
} }
else { else {
strategy = new FunctionGenerationStrategy.FunctionDefault(state, accessor); strategy = new FunctionGenerationStrategy.FunctionDefault(state, accessor);
} }
functionCodegen.generateMethod(JvmDeclarationOriginKt.OtherOrigin(accessor != null ? accessor : p, accessorDescriptor), accessorDescriptor, strategy); functionCodegen.generateMethod(JvmDeclarationOriginKt.OtherOrigin(descriptor), descriptor, strategy);
} }
private static class DefaultPropertyAccessorStrategy extends FunctionGenerationStrategy.CodegenBased { private static class DefaultPropertyAccessorStrategy extends FunctionGenerationStrategy.CodegenBased {
@@ -68,11 +68,10 @@ internal open class KtUltraLightField(
private val kotlinType: KotlinType? by lazyPub { private val kotlinType: KotlinType? by lazyPub {
when { when {
declaration is KtProperty && declaration.hasDelegate() -> declaration is KtProperty && declaration.hasDelegate() ->
propertyDescriptor propertyDescriptor?.let {
?.let { val context = LightClassGenerationSupport.getInstance(project).analyze(declaration)
val context = LightClassGenerationSupport.getInstance(project).analyze(declaration) PropertyCodegen.getDelegateTypeForProperty(it, context)
PropertyCodegen.getDelegateTypeForProperty(declaration, it, context) }
}
declaration is KtObjectDeclaration -> declaration is KtObjectDeclaration ->
(declaration.resolve() as? ClassDescriptor)?.defaultType (declaration.resolve() as? ClassDescriptor)?.defaultType
declaration is KtEnumEntry -> { declaration is KtEnumEntry -> {