Skip to content

Commit

Permalink
Add FunSpec#jvmModifiers overload to fix chaining
Browse files Browse the repository at this point in the history
Resolves square#638
  • Loading branch information
ZacSweers committed Mar 27, 2019
1 parent a313063 commit 7dfa776
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/main/java/com/squareup/kotlinpoet/FunSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -286,7 +287,19 @@ class FunSpec private constructor(
this.modifiers += modifiers
}

fun jvmModifiers(modifiers: Iterable<Modifier>) {
@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<Modifier>) {
jvmModifiers(modifiers)
}

@JvmName("jvmModifiersNew")
fun jvmModifiers(modifiers: Iterable<Modifier>) = apply {
var visibility = KModifier.INTERNAL
for (modifier in modifiers) {
when (modifier) {
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/com/squareup/kotlinpoet/FunSpecTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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())
}
}

0 comments on commit 7dfa776

Please sign in to comment.