Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FunSpec#jvmModifiers overload to fix chaining #653

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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. " +
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The message here should target library users, not maintainers, something like "Deprecated. Use jvmModifiersNew()." would do.

"https://github.com/square/kotlinpoet/issues/638",
replaceWith = ReplaceWith("jvmModifiers(modifiers)"),
level = HIDDEN
)
fun jvmModifersOld(modifiers: Iterable<Modifier>) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: Modifers -> Modifiers

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())
}
}