From 7dfa77627fc414a75a65f0f58756fa8c8f0a75e2 Mon Sep 17 00:00:00 2001 From: Zac Sweers Date: Tue, 26 Mar 2019 20:38:59 -0700 Subject: [PATCH] Add FunSpec#jvmModifiers overload to fix chaining Resolves #638 --- src/main/java/com/squareup/kotlinpoet/FunSpec.kt | 15 ++++++++++++++- .../java/com/squareup/kotlinpoet/FunSpecTest.kt | 13 +++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/squareup/kotlinpoet/FunSpec.kt b/src/main/java/com/squareup/kotlinpoet/FunSpec.kt index 04a8cc630b..3c703b6f2d 100644 --- a/src/main/java/com/squareup/kotlinpoet/FunSpec.kt +++ b/src/main/java/com/squareup/kotlinpoet/FunSpec.kt @@ -28,6 +28,7 @@ import javax.lang.model.type.DeclaredType import javax.lang.model.type.ExecutableType import javax.lang.model.type.TypeVariable import javax.lang.model.util.Types +import kotlin.DeprecationLevel.HIDDEN import kotlin.reflect.KClass /** A generated function declaration. */ @@ -286,7 +287,19 @@ class FunSpec private constructor( this.modifiers += modifiers } - fun jvmModifiers(modifiers: Iterable) { + @JvmName("jvmModifiers") + @Deprecated( + message = "This API was missing the Builder return type and breaks chaining. " + + "https://github.com/square/kotlinpoet/issues/638", + replaceWith = ReplaceWith("jvmModifiers(modifiers)"), + level = HIDDEN + ) + fun jvmModifersOld(modifiers: Iterable) { + jvmModifiers(modifiers) + } + + @JvmName("jvmModifiersNew") + fun jvmModifiers(modifiers: Iterable) = apply { var visibility = KModifier.INTERNAL for (modifier in modifiers) { when (modifier) { diff --git a/src/test/java/com/squareup/kotlinpoet/FunSpecTest.kt b/src/test/java/com/squareup/kotlinpoet/FunSpecTest.kt index e64dff1e5e..c9f9de2806 100644 --- a/src/test/java/com/squareup/kotlinpoet/FunSpecTest.kt +++ b/src/test/java/com/squareup/kotlinpoet/FunSpecTest.kt @@ -671,6 +671,7 @@ class FunSpecTest { @Test fun jvmFinalModifier() { val builder = FunSpec.builder("finalMethod") builder.jvmModifiers(listOf(Modifier.FINAL)) + .returns(Unit::class) // Ensure chaining works https://github.com/square/kotlinpoet/issues/638 assertThat(builder.build().toString()).isEqualTo(""" |internal final fun finalMethod() { @@ -688,4 +689,16 @@ class FunSpecTest { |} |""".trimMargin()) } + + /** Ensure chaining jvmModifiers() works - https://github.com/square/kotlinpoet/issues/638 */ + @Test fun newJvmModifierChaining() { + val builder = FunSpec.builder("finalMethod") + builder.jvmModifiers(listOf(Modifier.FINAL)) + .returns(Unit::class) + + assertThat(builder.build().toString()).isEqualTo(""" + |internal final fun finalMethod() { + |} + |""".trimMargin()) + } }