From 9ce959c4adb2d78b6e46e8163387ca8d1a97d271 Mon Sep 17 00:00:00 2001 From: Jake Wharton Date: Thu, 26 Oct 2023 09:01:35 -0400 Subject: [PATCH] Eliminate pointless covariant return in protocol widgets (#1637) This caused a method in the bytecode that was never used. For example: public final class com.example.redwood.testing.widget.redwoodlayout.ProtocolBox implements app.cash.redwood.protocol.widget.ProtocolNode { - private final app.cash.redwood.layout.widget.Box widget; + private final app.cash.redwood.layout.widget.Box _widget; private final kotlinx.serialization.json.Json json; private final app.cash.redwood.protocol.widget.ProtocolMismatchHandler mismatchHandler; private app.cash.redwood.widget.Widget$Children container; private final kotlinx.serialization.KSerializer serializer_0; private final kotlinx.serialization.KSerializer serializer_1; private final kotlinx.serialization.KSerializer serializer_2; public com.example.redwood.testing.widget.redwoodlayout.ProtocolBox(app.cash.redwood.layout.widget.Box, kotlinx.serialization.json.Json, app.cash.redwood.protocol.widget.ProtocolMismatchHandler); - public app.cash.redwood.layout.widget.Box getWidget(); - public app.cash.redwood.widget.Widget getWidget(); + public app.cash.redwood.widget.Widget getWidget(); public void apply(app.cash.redwood.protocol.PropertyChange, app.cash.redwood.protocol.EventSink); public app.cash.redwood.widget.Widget$Children children-dBpC-2Y(int); public void updateModifier(java.util.List); public void attachTo(app.cash.redwood.widget.Widget$Children); } --- .../codegen/widgetProtocolGeneration.kt | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/redwood-tooling-codegen/src/main/kotlin/app/cash/redwood/tooling/codegen/widgetProtocolGeneration.kt b/redwood-tooling-codegen/src/main/kotlin/app/cash/redwood/tooling/codegen/widgetProtocolGeneration.kt index 7c082b2637..93fdf999c4 100644 --- a/redwood-tooling-codegen/src/main/kotlin/app/cash/redwood/tooling/codegen/widgetProtocolGeneration.kt +++ b/redwood-tooling-codegen/src/main/kotlin/app/cash/redwood/tooling/codegen/widgetProtocolGeneration.kt @@ -145,25 +145,28 @@ internal fun generateProtocolNodeFactory( /* internal class ProtocolButton( - override val widget: Button, + widget: Button, private val json: Json, private val mismatchHandler: ProtocolMismatchHandler, ) : ProtocolNode { + private var _widget: Button = widget + override val widget: Widget get() = _widget + private var container: Widget.Children? = null private val serializer_0: KSerializer = json.serializersModule.serializer() private val serializer_1: KSerializer = json.serializersModule.serializer() public override fun apply(change: PropertyChange, eventSink: EventSink): Unit { when (change.tag.value) { - 1 -> widget.text(json.decodeFromJsonElement(serializer_0, change.value)) - 2 -> widget.enabled(json.decodeFromJsonElement(serializer_1, change.value)) + 1 -> _widget.text(json.decodeFromJsonElement(serializer_0, change.value)) + 2 -> _widget.enabled(json.decodeFromJsonElement(serializer_1, change.value)) 3 -> { val onClick: (() -> Unit)? = if (change.value.jsonPrimitive.boolean) { { eventSink.sendEvent(Event(change.id, EventTag(3))) } } else { null } - widget.onClick(onClick) + _widget.onClick(onClick) } else -> mismatchHandler.onUnknownProperty(WidgetTag(12), change.tag) } @@ -174,7 +177,7 @@ internal class ProtocolButton( } public override fun updateModifiers() { - widget.modifier = elements.toModifiers(json, mismatchHandler) + _widget.modifier = elements.toModifiers(json, mismatchHandler) container?.onModifierUpdated() } @@ -207,10 +210,19 @@ internal fun generateProtocolNode( .build(), ) .addProperty( - PropertySpec.builder("widget", widgetType, OVERRIDE) + PropertySpec.builder("_widget", widgetType, PRIVATE) .initializer("widget") .build(), ) + .addProperty( + PropertySpec.builder("widget", RedwoodWidget.Widget.parameterizedBy(typeVariableW), OVERRIDE) + .getter( + FunSpec.getterBuilder() + .addStatement("return _widget") + .build(), + ) + .build(), + ) .addProperty( PropertySpec.builder("json", KotlinxSerialization.Json, PRIVATE) .initializer("json") @@ -249,7 +261,7 @@ internal fun generateProtocolNode( } addStatement( - "%L -> widget.%N(json.decodeFromJsonElement(serializer_%L, change.value))", + "%L -> _widget.%N(json.decodeFromJsonElement(serializer_%L, change.value))", trait.tag, trait.name, serializerId, @@ -298,7 +310,7 @@ internal fun generateProtocolNode( addStatement("throw %T()", Stdlib.AssertionError) } endControlFlow() - addStatement("widget.%1N(%1N)", trait.name) + addStatement("_widget.%1N(%1N)", trait.name) endControlFlow() } @@ -336,7 +348,7 @@ internal fun generateProtocolNode( if (childrens.isNotEmpty()) { beginControlFlow("return when (tag.value)") for (children in childrens) { - addStatement("%L -> widget.%N", children.tag, children.name) + addStatement("%L -> _widget.%N", children.tag, children.name) } beginControlFlow("else ->") addStatement( @@ -363,7 +375,7 @@ internal fun generateProtocolNode( FunSpec.builder("updateModifier") .addModifiers(OVERRIDE) .addParameter("elements", LIST.parameterizedBy(Protocol.ModifierElement)) - .addStatement("widget.modifier = elements.%M(json, mismatchHandler)", host.toModifier) + .addStatement("_widget.modifier = elements.%M(json, mismatchHandler)", host.toModifier) .addStatement("container?.onModifierUpdated()") .build(), )