Simplify backing field generation logic in PropertyCodegen

Inline several methods, remove extra parameters, fix warnings
This commit is contained in:
Alexander Udalov
2018-08-22 15:43:51 +02:00
parent 753191e668
commit 9d1baaeb56
@@ -161,10 +161,9 @@ public class PropertyCodegen {
? !(context instanceof MultifileClassPartContext) ? !(context instanceof MultifileClassPartContext)
: CodegenContextUtil.isImplClassOwner(context); : CodegenContextUtil.isImplClassOwner(context);
Annotations propertyAnnotations = descriptor.getAnnotations();
assert declaration != null : "Declaration is null: " + descriptor + " (context=" + context + ")"; assert declaration != null : "Declaration is null: " + descriptor + " (context=" + context + ")";
generateBackingField(declaration, descriptor, descriptor.getBackingField(), descriptor.getDelegateField(), isBackingFieldOwner); generateBackingField(declaration, descriptor, isBackingFieldOwner);
generateSyntheticMethodIfNeeded(descriptor, propertyAnnotations, isBackingFieldOwner); generateSyntheticMethodIfNeeded(descriptor, isBackingFieldOwner);
} }
/** /**
@@ -303,8 +302,6 @@ public class PropertyCodegen {
private void generateBackingField( private void generateBackingField(
@NotNull KtNamedDeclaration p, @NotNull KtNamedDeclaration p,
@NotNull PropertyDescriptor descriptor, @NotNull PropertyDescriptor descriptor,
@Nullable FieldDescriptor backingField,
@Nullable FieldDescriptor delegateField,
boolean isBackingFieldOwner boolean isBackingFieldOwner
) { ) {
if (isJvmInterface(descriptor.getContainingDeclaration()) || kind == OwnerKind.DEFAULT_IMPLS || if (isJvmInterface(descriptor.getContainingDeclaration()) || kind == OwnerKind.DEFAULT_IMPLS ||
@@ -312,21 +309,32 @@ public class PropertyCodegen {
return; return;
} }
if (p instanceof KtProperty && ((KtProperty) p).hasDelegate()) { boolean isDelegate = p instanceof KtProperty && ((KtProperty) p).hasDelegate();
generatePropertyDelegateAccess((KtProperty) p, descriptor, delegateField, isBackingFieldOwner);
Object defaultValue;
if (isDelegate) {
defaultValue = null;
} }
else if (Boolean.TRUE.equals(bindingContext.get(BindingContext.BACKING_FIELD_REQUIRED, descriptor))) { else if (Boolean.TRUE.equals(bindingContext.get(BindingContext.BACKING_FIELD_REQUIRED, descriptor))) {
generateBackingFieldAccess(p, descriptor, backingField, isBackingFieldOwner); if (shouldWriteFieldInitializer(descriptor)) {
ConstantValue<?> initializer = descriptor.getCompileTimeInitializer();
defaultValue = initializer == null ? null : initializer.getValue();
}
else {
defaultValue = null;
}
} }
else {
return;
}
generateBackingField(p, 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
// accessible via reflection, and 'deprecated' and 'synthetic' flags prevent this method from being called accidentally // accessible via reflection, and 'deprecated' and 'synthetic' flags prevent this method from being called accidentally
private void generateSyntheticMethodIfNeeded( private void generateSyntheticMethodIfNeeded(@NotNull PropertyDescriptor descriptor, boolean isBackingFieldOwner) {
@NotNull PropertyDescriptor descriptor, Annotations annotations = descriptor.getAnnotations();
@NotNull Annotations annotations,
boolean isBackingFieldOwner
) {
if (annotations.getAllAnnotations().isEmpty()) return; if (annotations.getAllAnnotations().isEmpty()) return;
Method signature = getSyntheticMethodSignature(descriptor); Method signature = getSyntheticMethodSignature(descriptor);
@@ -354,11 +362,11 @@ public class PropertyCodegen {
@NotNull KtNamedDeclaration element, @NotNull KtNamedDeclaration element,
@NotNull PropertyDescriptor propertyDescriptor, @NotNull PropertyDescriptor propertyDescriptor,
boolean isDelegate, boolean isDelegate,
@NotNull KotlinType kotlinType,
@Nullable Object defaultValue, @Nullable Object defaultValue,
@Nullable FieldDescriptor annotatedField,
boolean isBackingFieldOwner boolean isBackingFieldOwner
) { ) {
FieldDescriptor annotatedField = isDelegate ? propertyDescriptor.getDelegateField() : propertyDescriptor.getBackingField();
int modifiers = getDeprecatedAccessFlag(propertyDescriptor); int modifiers = getDeprecatedAccessFlag(propertyDescriptor);
for (AnnotationCodegen.JvmFlagAnnotation flagAnnotation : AnnotationCodegen.FIELD_FLAGS) { for (AnnotationCodegen.JvmFlagAnnotation flagAnnotation : AnnotationCodegen.FIELD_FLAGS) {
@@ -379,6 +387,8 @@ public class PropertyCodegen {
modifiers |= ACC_SYNTHETIC; modifiers |= ACC_SYNTHETIC;
} }
KotlinType kotlinType =
isDelegate ? getDelegateTypeForProperty((KtProperty) element, propertyDescriptor) : propertyDescriptor.getType();
Type type = typeMapper.mapType(kotlinType); Type type = typeMapper.mapType(kotlinType);
ClassBuilder builder = v; ClassBuilder builder = v;
@@ -416,17 +426,6 @@ public class PropertyCodegen {
} }
} }
private void generatePropertyDelegateAccess(
@NotNull KtProperty p,
@NotNull PropertyDescriptor propertyDescriptor,
@Nullable FieldDescriptor delegateField,
boolean isBackingFieldOwner
) {
KotlinType delegateType = getDelegateTypeForProperty(p, propertyDescriptor);
generateBackingField(p, propertyDescriptor, true, delegateType, null, delegateField, isBackingFieldOwner);
}
@NotNull @NotNull
private KotlinType getDelegateTypeForProperty(@NotNull KtProperty p, @NotNull PropertyDescriptor propertyDescriptor) { private KotlinType getDelegateTypeForProperty(@NotNull KtProperty p, @NotNull PropertyDescriptor propertyDescriptor) {
KotlinType delegateType = null; KotlinType delegateType = null;
@@ -449,27 +448,11 @@ public class PropertyCodegen {
return delegateType; return delegateType;
} }
private void generateBackingFieldAccess(
@NotNull KtNamedDeclaration p,
@NotNull PropertyDescriptor propertyDescriptor,
@Nullable FieldDescriptor backingField,
boolean isBackingFieldOwner
) {
Object value = null;
if (shouldWriteFieldInitializer(propertyDescriptor)) {
ConstantValue<?> initializer = propertyDescriptor.getCompileTimeInitializer();
if (initializer != null) {
value = initializer.getValue();
}
}
generateBackingField(p, propertyDescriptor, false, propertyDescriptor.getType(), value, backingField, isBackingFieldOwner);
}
private boolean shouldWriteFieldInitializer(@NotNull PropertyDescriptor descriptor) { private boolean shouldWriteFieldInitializer(@NotNull PropertyDescriptor descriptor) {
if (!descriptor.isConst() && state.getLanguageVersionSettings().supportsFeature(LanguageFeature.NoConstantValueAttributeForNonConstVals)) if (!descriptor.isConst() &&
state.getLanguageVersionSettings().supportsFeature(LanguageFeature.NoConstantValueAttributeForNonConstVals)) {
return false; return false;
}
//final field of primitive or String type //final field of primitive or String type
if (!descriptor.isVar()) { if (!descriptor.isVar()) {
@@ -479,16 +462,16 @@ public class PropertyCodegen {
return false; return false;
} }
public void generateGetter(@Nullable KtNamedDeclaration p, @NotNull PropertyDescriptor descriptor, @Nullable KtPropertyAccessor getter) { private void generateGetter(
@Nullable KtNamedDeclaration p, @NotNull PropertyDescriptor descriptor, @Nullable KtPropertyAccessor getter
) {
generateAccessor(p, getter, descriptor.getGetter() != null generateAccessor(p, getter, descriptor.getGetter() != null
? descriptor.getGetter() ? descriptor.getGetter()
: DescriptorFactory.createDefaultGetter(descriptor, Annotations.Companion.getEMPTY())); : DescriptorFactory.createDefaultGetter(descriptor, Annotations.Companion.getEMPTY()));
} }
public void generateSetter( private void generateSetter(
@Nullable KtNamedDeclaration p, @Nullable KtNamedDeclaration p, @NotNull PropertyDescriptor descriptor, @Nullable KtPropertyAccessor setter
@NotNull PropertyDescriptor descriptor,
@Nullable KtPropertyAccessor setter
) { ) {
if (!descriptor.isVar()) return; if (!descriptor.isVar()) return;
@@ -568,7 +551,7 @@ public class PropertyCodegen {
public static StackValue invokeDelegatedPropertyConventionMethod( public static StackValue invokeDelegatedPropertyConventionMethod(
@NotNull ExpressionCodegen codegen, @NotNull ExpressionCodegen codegen,
@NotNull ResolvedCall<FunctionDescriptor> resolvedCall, @NotNull ResolvedCall<FunctionDescriptor> resolvedCall,
@Nullable StackValue receiver, @NotNull StackValue receiver,
@NotNull PropertyDescriptor propertyDescriptor @NotNull PropertyDescriptor propertyDescriptor
) { ) {
codegen.tempVariables.put( codegen.tempVariables.put(