Skip to content

Commit

Permalink
Merge pull request #2678 from square/kotlin-build-followups
Browse files Browse the repository at this point in the history
Cleanup Kotlin build function
  • Loading branch information
oldergod authored Oct 16, 2023
2 parents 936caaf + 64cbd6a commit fa69a5d
Show file tree
Hide file tree
Showing 67 changed files with 181 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.squareup.wire.`internal`.FloatArrayList
import com.squareup.wire.`internal`.IntArrayList
import com.squareup.wire.`internal`.JvmField
import com.squareup.wire.`internal`.JvmStatic
import com.squareup.wire.`internal`.JvmSynthetic
import com.squareup.wire.`internal`.LongArrayList
import com.squareup.wire.`internal`.checkElementsNotNull
import com.squareup.wire.`internal`.decodePrimitive_double
Expand Down Expand Up @@ -4498,6 +4499,7 @@ public class AllTypes private constructor(

private const val serialVersionUID: Long = 0L

@JvmSynthetic
public inline fun build(body: Builder.() -> Unit): AllTypes = Builder().apply(body).build()
}

Expand Down Expand Up @@ -4633,6 +4635,7 @@ public class AllTypes private constructor(

private const val serialVersionUID: Long = 0L

@JvmSynthetic
public inline fun build(body: Builder.() -> Unit): NestedMessage =
Builder().apply(body).build()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.squareup.wire.ReverseProtoWriter
import com.squareup.wire.Syntax.PROTO_2
import com.squareup.wire.`internal`.JvmField
import com.squareup.wire.`internal`.JvmStatic
import com.squareup.wire.`internal`.JvmSynthetic
import kotlin.Any
import kotlin.Boolean
import kotlin.Int
Expand Down Expand Up @@ -152,6 +153,7 @@ public class BoxedOneOfs(

private const val serialVersionUID: Long = 0L

@JvmSynthetic
public inline fun build(body: Builder.() -> Unit): BoxedOneOfs = Builder().apply(body).build()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.squareup.wire.ReverseProtoWriter
import com.squareup.wire.Syntax.PROTO_2
import com.squareup.wire.WireField
import com.squareup.wire.`internal`.JvmField
import com.squareup.wire.`internal`.JvmSynthetic
import kotlin.Any
import kotlin.Boolean
import kotlin.Int
Expand Down Expand Up @@ -125,6 +126,7 @@ public class SomeMessage private constructor(

private const val serialVersionUID: Long = 0L

@JvmSynthetic
public inline fun build(body: Builder.() -> Unit): SomeMessage = Builder().apply(body).build()
}

Expand Down Expand Up @@ -234,6 +236,7 @@ public class SomeMessage private constructor(

private const val serialVersionUID: Long = 0L

@JvmSynthetic
public inline fun build(body: Builder.() -> Unit): NestedMessage =
Builder().apply(body).build()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.squareup.wire.ReverseProtoWriter
import com.squareup.wire.Syntax.PROTO_2
import com.squareup.wire.WireField
import com.squareup.wire.`internal`.JvmField
import com.squareup.wire.`internal`.JvmSynthetic
import com.squareup.wire.`internal`.checkElementsNotNull
import com.squareup.wire.`internal`.immutableCopyOf
import com.squareup.wire.`internal`.sanitize
Expand Down Expand Up @@ -261,6 +262,7 @@ public class Dinosaur(

private const val serialVersionUID: Long = 0L

@JvmSynthetic
public inline fun build(body: Builder.() -> Unit): Dinosaur = Builder().apply(body).build()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.squareup.wire.WireEnumConstant
import com.squareup.wire.WireField
import com.squareup.wire.`internal`.JvmField
import com.squareup.wire.`internal`.JvmStatic
import com.squareup.wire.`internal`.JvmSynthetic
import com.squareup.wire.`internal`.checkElementsNotNull
import com.squareup.wire.`internal`.immutableCopyOf
import com.squareup.wire.`internal`.missingRequiredFields
Expand Down Expand Up @@ -269,6 +270,7 @@ public class KeywordKotlin(

private const val serialVersionUID: Long = 0L

@JvmSynthetic
public inline fun build(body: Builder.() -> Unit): KeywordKotlin = Builder().apply(body).build()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ class KotlinGenerator private constructor(

addDefaultFields(type, companionBuilder, nameAllocator)
addAdapter(type, companionBuilder)
if (buildersOnly || javaInterOp) addBuildClosure(type, companionBuilder, builderClassName)
if (buildersOnly || javaInterOp) addBuildFunction(type, companionBuilder, builderClassName)

val classBuilder = TypeSpec.classBuilder(className)
.apply {
Expand Down Expand Up @@ -1971,9 +1971,23 @@ class KotlinGenerator private constructor(
}
}

private fun addBuildClosure(type: MessageType, companionBuilder: TypeSpec.Builder, builderClassName: ClassName) {
val buildFn = FunSpec.builder("build")
/**
* Adds a closure into the [companionBuilder] allowing the creation of an instance via the Kotlin
* DSL.
*
* Example
* ```
* companion object {
* @JvmSynthetic
* public inline fun build(body: Builder.() -> Unit): AllTypes = Builder().apply(body).build()
* }
* ```
*/
private fun addBuildFunction(type: MessageType, companionBuilder: TypeSpec.Builder, builderClassName: ClassName) {
val buildFunction = FunSpec.builder("build")
.addModifiers(INLINE)
// We hide it to Java callers.
.addAnnotation(ClassName("com.squareup.wire.internal", "JvmSynthetic"))
.addParameter(
"body",
LambdaTypeName.get(
Expand All @@ -1985,7 +1999,7 @@ class KotlinGenerator private constructor(
.returns(generatedTypeName(type))
.build()

companionBuilder.addFunction(buildFn)
companionBuilder.addFunction(buildFunction)
}

private fun redactFun(message: MessageType): FunSpec {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1979,11 +1979,14 @@ class KotlinGeneratorTest {
.generateKotlin("SomeMessage", buildersOnly = false, javaInterop = false)
assertThat(code).doesNotContain("inline fun build(body: Builder.() -> Unit): SomeMessage")
val buildersOnlyCode = KotlinWithProfilesGenerator(schema)
.generateKotlin("SomeMessage", buildersOnly = true)
assertThat(buildersOnlyCode).contains("inline fun build(body: Builder.() -> Unit): SomeMessage")
.generateKotlin("SomeMessage", buildersOnly = true, javaInterop = false)
assertThat(buildersOnlyCode).contains("@JvmSynthetic\n public inline fun build(body: Builder.() -> Unit): SomeMessage")
val javaInteropCode = KotlinWithProfilesGenerator(schema)
.generateKotlin("SomeMessage", javaInterop = true)
assertThat(javaInteropCode).contains("inline fun build(body: Builder.() -> Unit): SomeMessage")
.generateKotlin("SomeMessage", buildersOnly = false, javaInterop = true)
assertThat(javaInteropCode).contains("@JvmSynthetic\n public inline fun build(body: Builder.() -> Unit): SomeMessage")
val bothCode = KotlinWithProfilesGenerator(schema)
.generateKotlin("SomeMessage", buildersOnly = true, javaInterop = true)
assertThat(bothCode).contains("@JvmSynthetic\n public inline fun build(body: Builder.() -> Unit): SomeMessage")
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.squareup.wire.ReverseProtoWriter
import com.squareup.wire.Syntax.PROTO_2
import com.squareup.wire.WireField
import com.squareup.wire.`internal`.JvmField
import com.squareup.wire.`internal`.JvmSynthetic
import com.squareup.wire.`internal`.checkElementsNotNull
import com.squareup.wire.`internal`.immutableCopyOf
import com.squareup.wire.`internal`.sanitize
Expand Down Expand Up @@ -261,6 +262,7 @@ public class Dinosaur(

private const val serialVersionUID: Long = 0L

@JvmSynthetic
public inline fun build(body: Builder.() -> Unit): Dinosaur = Builder().apply(body).build()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.squareup.wire.WireEnum
import com.squareup.wire.WireField
import com.squareup.wire.`internal`.JvmField
import com.squareup.wire.`internal`.JvmStatic
import com.squareup.wire.`internal`.JvmSynthetic
import com.squareup.wire.`internal`.checkElementsNotNull
import com.squareup.wire.`internal`.immutableCopyOf
import com.squareup.wire.`internal`.missingRequiredFields
Expand Down Expand Up @@ -251,6 +252,7 @@ public class Person(

private const val serialVersionUID: Long = 0L

@JvmSynthetic
public inline fun build(body: Builder.() -> Unit): Person = Builder().apply(body).build()
}

Expand Down Expand Up @@ -443,6 +445,7 @@ public class Person(

private const val serialVersionUID: Long = 0L

@JvmSynthetic
public inline fun build(body: Builder.() -> Unit): PhoneNumber = Builder().apply(body).build()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.squareup.wire.WireEnumConstant
import com.squareup.wire.WireField
import com.squareup.wire.`internal`.JvmField
import com.squareup.wire.`internal`.JvmStatic
import com.squareup.wire.`internal`.JvmSynthetic
import com.squareup.wire.`internal`.checkElementsNotNull
import com.squareup.wire.`internal`.immutableCopyOf
import com.squareup.wire.`internal`.missingRequiredFields
Expand Down Expand Up @@ -269,6 +270,7 @@ public class KeywordKotlin(

private const val serialVersionUID: Long = 0L

@JvmSynthetic
public inline fun build(body: Builder.() -> Unit): KeywordKotlin = Builder().apply(body).build()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ expect annotation class JvmDefaultWithCompatibility()

expect annotation class JvmField()

expect annotation class JvmSynthetic()

expect annotation class JvmStatic()

expect abstract class ObjectStreamException : IOException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ actual interface Serializable
/** This annotation is an empty placeholder. */
actual annotation class JvmField

/** This annotation is an empty placeholder. */
actual annotation class JvmSynthetic

/** This annotation is an empty placeholder. */
actual annotation class JvmStatic

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ actual typealias JvmDefaultWithCompatibility = kotlin.jvm.JvmDefaultWithCompatib

actual typealias JvmField = kotlin.jvm.JvmField

actual typealias JvmSynthetic = kotlin.jvm.JvmSynthetic

actual typealias JvmStatic = kotlin.jvm.JvmStatic

actual typealias ObjectStreamException = java.io.ObjectStreamException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ actual interface Serializable
/** This annotation is an empty placeholder. */
actual annotation class JvmField

/** This annotation is an empty placeholder. */
actual annotation class JvmSynthetic

/** This annotation is an empty placeholder. */
actual annotation class JvmStatic

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.squareup.wire.ReverseProtoWriter
import com.squareup.wire.Syntax.PROTO_2
import com.squareup.wire.WireField
import com.squareup.wire.`internal`.JvmField
import com.squareup.wire.`internal`.JvmSynthetic
import com.squareup.wire.`internal`.sanitize
import kotlin.Any
import kotlin.Boolean
Expand Down Expand Up @@ -150,6 +151,7 @@ public class NotRedacted private constructor(

private const val serialVersionUID: Long = 0L

@JvmSynthetic
public inline fun build(body: Builder.() -> Unit): NotRedacted = Builder().apply(body).build()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.squareup.wire.ReverseProtoWriter
import com.squareup.wire.Syntax.PROTO_2
import com.squareup.wire.WireField
import com.squareup.wire.`internal`.JvmField
import com.squareup.wire.`internal`.JvmSynthetic
import com.squareup.wire.`internal`.sanitize
import kotlin.Any
import kotlin.Boolean
Expand Down Expand Up @@ -176,6 +177,7 @@ public class RedactedChild private constructor(

private const val serialVersionUID: Long = 0L

@JvmSynthetic
public inline fun build(body: Builder.() -> Unit): RedactedChild = Builder().apply(body).build()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.squareup.wire.ReverseProtoWriter
import com.squareup.wire.Syntax.PROTO_2
import com.squareup.wire.WireField
import com.squareup.wire.`internal`.JvmField
import com.squareup.wire.`internal`.JvmSynthetic
import kotlin.Any
import kotlin.Boolean
import kotlin.Int
Expand Down Expand Up @@ -126,6 +127,7 @@ public class RedactedCycleA private constructor(

private const val serialVersionUID: Long = 0L

@JvmSynthetic
public inline fun build(body: Builder.() -> Unit): RedactedCycleA =
Builder().apply(body).build()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.squareup.wire.ReverseProtoWriter
import com.squareup.wire.Syntax.PROTO_2
import com.squareup.wire.WireField
import com.squareup.wire.`internal`.JvmField
import com.squareup.wire.`internal`.JvmSynthetic
import kotlin.Any
import kotlin.Boolean
import kotlin.Int
Expand Down Expand Up @@ -126,6 +127,7 @@ public class RedactedCycleB private constructor(

private const val serialVersionUID: Long = 0L

@JvmSynthetic
public inline fun build(body: Builder.() -> Unit): RedactedCycleB =
Builder().apply(body).build()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.squareup.wire.ReverseProtoWriter
import com.squareup.wire.Syntax.PROTO_2
import com.squareup.wire.WireField
import com.squareup.wire.`internal`.JvmField
import com.squareup.wire.`internal`.JvmSynthetic
import com.squareup.wire.`internal`.sanitize
import kotlin.Any
import kotlin.Boolean
Expand Down Expand Up @@ -152,6 +153,7 @@ public class RedactedExtension private constructor(

private const val serialVersionUID: Long = 0L

@JvmSynthetic
public inline fun build(body: Builder.() -> Unit): RedactedExtension =
Builder().apply(body).build()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.squareup.wire.ReverseProtoWriter
import com.squareup.wire.Syntax.PROTO_2
import com.squareup.wire.WireField
import com.squareup.wire.`internal`.JvmField
import com.squareup.wire.`internal`.JvmSynthetic
import com.squareup.wire.`internal`.sanitize
import kotlin.Any
import kotlin.Boolean
Expand Down Expand Up @@ -204,6 +205,7 @@ public class RedactedFields private constructor(

private const val serialVersionUID: Long = 0L

@JvmSynthetic
public inline fun build(body: Builder.() -> Unit): RedactedFields =
Builder().apply(body).build()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.squareup.wire.ReverseProtoWriter
import com.squareup.wire.Syntax.PROTO_2
import com.squareup.wire.WireField
import com.squareup.wire.`internal`.JvmField
import com.squareup.wire.`internal`.JvmSynthetic
import com.squareup.wire.`internal`.checkElementsNotNull
import com.squareup.wire.`internal`.immutableCopyOf
import com.squareup.wire.`internal`.redactElements
Expand Down Expand Up @@ -170,6 +171,7 @@ public class RedactedRepeated private constructor(

private const val serialVersionUID: Long = 0L

@JvmSynthetic
public inline fun build(body: Builder.() -> Unit): RedactedRepeated =
Builder().apply(body).build()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.squareup.wire.ReverseProtoWriter
import com.squareup.wire.Syntax.PROTO_2
import com.squareup.wire.WireField
import com.squareup.wire.`internal`.JvmField
import com.squareup.wire.`internal`.JvmSynthetic
import kotlin.Any
import kotlin.Boolean
import kotlin.Int
Expand Down Expand Up @@ -126,6 +127,7 @@ public class RedactedRequired private constructor(

private const val serialVersionUID: Long = 0L

@JvmSynthetic
public inline fun build(body: Builder.() -> Unit): RedactedRequired =
Builder().apply(body).build()
}
Expand Down
Loading

0 comments on commit fa69a5d

Please sign in to comment.