Skip to content

Commit

Permalink
Add constructorJvmOverloads option to KotlinGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan O'Neill committed Oct 6, 2023
1 parent 126c5f4 commit 67f5eae
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ class KotlinGenerator private constructor(
private val profile: Profile,
private val emitAndroid: Boolean,
private val javaInterOp: Boolean,
private val constructorJvmOverloads: Boolean,
private val emitDeclaredOptions: Boolean,
private val emitAppliedOptions: Boolean,
private val rpcCallStyle: RpcCallStyle,
Expand Down Expand Up @@ -1062,6 +1063,10 @@ class KotlinGenerator private constructor(
val nameAllocator = nameAllocator(message)
val byteClass = ProtoType.BYTES.typeName

if (constructorJvmOverloads) {
constructorBuilder.addAnnotation(JvmOverloads::class)
}

val parametersAndProperties = parametersAndProperties(message, nameAllocator)
for ((parameter, property) in parametersAndProperties) {
constructorBuilder.addParameter(parameter)
Expand Down Expand Up @@ -2859,6 +2864,7 @@ class KotlinGenerator private constructor(
profile: Profile = Profile(),
emitAndroid: Boolean = false,
javaInterop: Boolean = false,
constructorJvmOverloads: Boolean = false,
emitDeclaredOptions: Boolean = true,
emitAppliedOptions: Boolean = true,
rpcCallStyle: RpcCallStyle = RpcCallStyle.SUSPENDING,
Expand Down Expand Up @@ -2908,6 +2914,7 @@ class KotlinGenerator private constructor(
memberToKotlinName = memberToKotlinName,
emitAndroid = emitAndroid,
javaInterOp = javaInterop || buildersOnly,
constructorJvmOverloads = constructorJvmOverloads,
emitDeclaredOptions = emitDeclaredOptions,
emitAppliedOptions = emitAppliedOptions,
rpcCallStyle = rpcCallStyle,
Expand Down Expand Up @@ -2981,4 +2988,4 @@ class KotlinGenerator private constructor(

private fun PropertySpec.Builder.jvmField(): PropertySpec.Builder = addAnnotation(
ClassName("com.squareup.wire.internal", "JvmField"),
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -1953,13 +1953,39 @@ class KotlinGeneratorTest {
)
}
val code = KotlinWithProfilesGenerator(schema)
.generateKotlin("SomeMessage", buildersOnly = true)
.generateKotlin("SomeMessage", buildersOnly = false)
assertThat(code).contains("SomeMessage private constructor(")
assertThat(code).contains("InnerMessage private constructor(")
assertThat(code).doesNotContain("SomeMessage public constructor(")
assertThat(code).doesNotContain("InnerMessage public constructor(")
}

@Test
fun constructorJvmOverloads() {
val schema = buildSchema {
add(
"message.proto".toPath(),
"""
|syntax = "proto2";
|message SomeMessage {
| optional string a = 1;
| optional string b = 2;
| message InnerMessage {
| optional string c = 3;
| optional string d = 8;
| }
|}
|
""".trimMargin(),
)
}

val code = KotlinWithProfilesGenerator(schema)
.generateKotlin("SomeMessage", constructorJvmOverloads = true)
assertThat(code).contains("public class SomeMessage @JvmOverloads constructor(")
assertThat(code).contains("public class InnerMessage @JvmOverloads constructor(")
}

@Test
fun javaInteropAndBuildersOnly() {
val schema = buildSchema {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@ internal class KotlinWithProfilesGenerator(private val schema: Schema) {
boxOneOfsMinSize: Int = 5_000,
buildersOnly: Boolean = false,
javaInterop: Boolean = false,
constructorJvmOverloads: Boolean = false,
): String {
val kotlinGenerator = KotlinGenerator(
schema,
profile = profile(profileName),
boxOneOfsMinSize = boxOneOfsMinSize,
buildersOnly = buildersOnly,
javaInterop = javaInterop,
constructorJvmOverloads = constructorJvmOverloads,
)
val type = schema.getType(typeName)!!
val typeSpec = kotlinGenerator.generateType(type)
Expand Down

0 comments on commit 67f5eae

Please sign in to comment.