From 7946e6ef7fd26b88d07f0186c4926071f6ca0a85 Mon Sep 17 00:00:00 2001 From: Rahul Ravikumar Date: Wed, 18 Dec 2024 04:03:49 -0800 Subject: [PATCH] Address comments in the PR. * `newBuilder()` throws an `AssertionError` instead. Thanks @oldergod. * Updated the `KotlinGeneratorTest` to use the renamed option in the `KotlinGenerator`. * Update goldens to include the new error message. --- .../main/kotlin/squareup/wire/mutable/MutableHeader.kt | 2 +- .../main/kotlin/squareup/wire/mutable/MutablePacket.kt | 2 +- .../kotlin/squareup/wire/mutable/MutablePayload.kt | 2 +- .../java/com/squareup/wire/kotlin/KotlinGenerator.kt | 10 ++++++++-- .../com/squareup/wire/kotlin/KotlinGeneratorTest.kt | 5 ++++- .../wire/kotlin/KotlinWithProfilesGenerator.kt | 4 ++-- 6 files changed, 17 insertions(+), 8 deletions(-) diff --git a/wire-golden-files/src/main/kotlin/squareup/wire/mutable/MutableHeader.kt b/wire-golden-files/src/main/kotlin/squareup/wire/mutable/MutableHeader.kt index 82eddb82a2..eaa49a9058 100644 --- a/wire-golden-files/src/main/kotlin/squareup/wire/mutable/MutableHeader.kt +++ b/wire-golden-files/src/main/kotlin/squareup/wire/mutable/MutableHeader.kt @@ -42,7 +42,7 @@ public class MutableHeader( message = "Shouldn't be used in Kotlin", level = DeprecationLevel.HIDDEN, ) - override fun newBuilder(): Nothing = throw AssertionError("Builders are deprecated and only available in a javaInterop build; see https://square.github.io/wire/wire_compiler/#kotlin") + override fun newBuilder(): Nothing = throw AssertionError("newBuilder() is unsupported for Mutable message types") override fun equals(other: Any?): Boolean { if (other !is MutableHeader) return false diff --git a/wire-golden-files/src/main/kotlin/squareup/wire/mutable/MutablePacket.kt b/wire-golden-files/src/main/kotlin/squareup/wire/mutable/MutablePacket.kt index 0c5df72def..cfd8903338 100644 --- a/wire-golden-files/src/main/kotlin/squareup/wire/mutable/MutablePacket.kt +++ b/wire-golden-files/src/main/kotlin/squareup/wire/mutable/MutablePacket.kt @@ -49,7 +49,7 @@ public class MutablePacket( message = "Shouldn't be used in Kotlin", level = DeprecationLevel.HIDDEN, ) - override fun newBuilder(): Nothing = throw AssertionError("Builders are deprecated and only available in a javaInterop build; see https://square.github.io/wire/wire_compiler/#kotlin") + override fun newBuilder(): Nothing = throw AssertionError("newBuilder() is unsupported for Mutable message types") override fun equals(other: Any?): Boolean { if (other !is MutablePacket) return false diff --git a/wire-golden-files/src/main/kotlin/squareup/wire/mutable/MutablePayload.kt b/wire-golden-files/src/main/kotlin/squareup/wire/mutable/MutablePayload.kt index 355f1db1b6..a883271531 100644 --- a/wire-golden-files/src/main/kotlin/squareup/wire/mutable/MutablePayload.kt +++ b/wire-golden-files/src/main/kotlin/squareup/wire/mutable/MutablePayload.kt @@ -42,7 +42,7 @@ public class MutablePayload( message = "Shouldn't be used in Kotlin", level = DeprecationLevel.HIDDEN, ) - override fun newBuilder(): Nothing = throw AssertionError("Builders are deprecated and only available in a javaInterop build; see https://square.github.io/wire/wire_compiler/#kotlin") + override fun newBuilder(): Nothing = throw AssertionError("newBuilder() is unsupported for Mutable message types") override fun equals(other: Any?): Boolean { if (other !is MutablePayload) return false diff --git a/wire-kotlin-generator/src/main/java/com/squareup/wire/kotlin/KotlinGenerator.kt b/wire-kotlin-generator/src/main/java/com/squareup/wire/kotlin/KotlinGenerator.kt index 5a1137f414..b105e3394b 100644 --- a/wire-kotlin-generator/src/main/java/com/squareup/wire/kotlin/KotlinGenerator.kt +++ b/wire-kotlin-generator/src/main/java/com/squareup/wire/kotlin/KotlinGenerator.kt @@ -644,6 +644,12 @@ class KotlinGenerator private constructor( .addModifiers(OVERRIDE) if (mutableTypes || !javaInterOp) { + val errorMessage = if (mutableTypes) { + "newBuilder() is unsupported for Mutable message types" + } else { + "Builders are deprecated and only available in a javaInterop build; see https://square.github.io/wire/wire_compiler/#kotlin" + } + return funBuilder .addAnnotation( AnnotationSpec.builder(Deprecated::class) @@ -652,7 +658,7 @@ class KotlinGenerator private constructor( .build(), ) .returns(NOTHING) - .addStatement("throw %T(%S)", ClassName("kotlin", "AssertionError"), "Builders are deprecated and only available in a javaInterop build; see https://square.github.io/wire/wire_compiler/#kotlin") + .addStatement("throw %T(%S)", ClassName("kotlin", "AssertionError"), errorMessage) .build() } @@ -702,7 +708,7 @@ class KotlinGenerator private constructor( val body = buildCodeBlock { if (!mutableTypes) { - // This is true iff the message is not mutable + // We cannot rely on referential equality if the message is mutable. addStatement("if (%N === this) return·true", otherName) } addStatement("if (%N !is %T) return·false", otherName, kotlinType) diff --git a/wire-kotlin-generator/src/test/java/com/squareup/wire/kotlin/KotlinGeneratorTest.kt b/wire-kotlin-generator/src/test/java/com/squareup/wire/kotlin/KotlinGeneratorTest.kt index 0b7f989bb2..d19eaf2d8b 100644 --- a/wire-kotlin-generator/src/test/java/com/squareup/wire/kotlin/KotlinGeneratorTest.kt +++ b/wire-kotlin-generator/src/test/java/com/squareup/wire/kotlin/KotlinGeneratorTest.kt @@ -2327,7 +2327,7 @@ class KotlinGeneratorTest { } val code = KotlinWithProfilesGenerator(schema).generateKotlin( typeName = "Packet", - generateMutableMessages = true, + mutableTypes = true, ) assertThat(code).contains("class MutablePacket") assertThat(code).contains("public var header_: MutableHeader? = null") @@ -2335,6 +2335,9 @@ class KotlinGeneratorTest { assertThat(code).contains("MutableHeader#ADAPTER") // should refer to adapters of Mutable message types. assertThat(code).contains("MutablePayload#ADAPTER") assertThat(code).contains("var result = 0") // hashCode() is no longer calling super.hashCode(). + assertThat(code).contains( + "throw AssertionError(\"newBuilder() is unsupported for Mutable message types\")", + ) assertThat(code).contains( "throw UnsupportedOperationException(\"redact() is unsupported for Mutable message types\")", ) diff --git a/wire-kotlin-generator/src/test/java/com/squareup/wire/kotlin/KotlinWithProfilesGenerator.kt b/wire-kotlin-generator/src/test/java/com/squareup/wire/kotlin/KotlinWithProfilesGenerator.kt index 7ebeb16bd3..0810e12caf 100644 --- a/wire-kotlin-generator/src/test/java/com/squareup/wire/kotlin/KotlinWithProfilesGenerator.kt +++ b/wire-kotlin-generator/src/test/java/com/squareup/wire/kotlin/KotlinWithProfilesGenerator.kt @@ -54,7 +54,7 @@ internal class KotlinWithProfilesGenerator(private val schema: Schema) { buildersOnly: Boolean = false, javaInterop: Boolean = false, enumMode: EnumMode = EnumMode.ENUM_CLASS, - generateMutableMessages: Boolean = false, + mutableTypes: Boolean = false, ): String { val kotlinGenerator = KotlinGenerator( schema, @@ -63,7 +63,7 @@ internal class KotlinWithProfilesGenerator(private val schema: Schema) { buildersOnly = buildersOnly, javaInterop = javaInterop, enumMode = enumMode, - generateMutableMessages = generateMutableMessages, + mutableTypes = mutableTypes, ) val type = schema.getType(typeName)!! val typeSpec = kotlinGenerator.generateType(type)