Skip to content

Commit

Permalink
Add support for external property getter
Browse files Browse the repository at this point in the history
  • Loading branch information
roihershberg committed Jun 9, 2022
1 parent 35da0d5 commit f53205e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
3 changes: 2 additions & 1 deletion kotlinpoet/src/main/java/com/squareup/kotlinpoet/FunSpec.kt
Expand Up @@ -55,6 +55,7 @@ public class FunSpec private constructor(
public val delegateConstructorArguments: List<CodeBlock> =
builder.delegateConstructorArguments.toImmutableList()
public val body: CodeBlock = builder.body.build()
private val isExternalGetter = name == GETTER && builder.modifiers.contains(EXTERNAL)
private val isEmptySetter = name == SETTER && parameters.isEmpty()

init {
Expand Down Expand Up @@ -155,7 +156,7 @@ public class FunSpec private constructor(
codeWriter.emitCode("%N", this)
}

if (!isEmptySetter) {
if (!isEmptySetter && !isExternalGetter) {
parameters.emit(codeWriter) { param ->
param.emit(codeWriter, includeType = name != SETTER)
}
Expand Down
Expand Up @@ -18,6 +18,7 @@ package com.squareup.kotlinpoet
import com.google.common.truth.Truth.assertThat
import com.squareup.kotlinpoet.FunSpec.Companion.GETTER
import com.squareup.kotlinpoet.FunSpec.Companion.SETTER
import com.squareup.kotlinpoet.KModifier.EXTERNAL
import com.squareup.kotlinpoet.KModifier.PRIVATE
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import java.io.Serializable
Expand Down Expand Up @@ -74,6 +75,30 @@ class PropertySpecTest {
}.hasMessageThat().isEqualTo("parameterless setter cannot have code")
}

@Test fun externalGetterAndSetter() {
val prop = PropertySpec.builder("foo", String::class)
.mutable()
.getter(
FunSpec.getterBuilder()
.addModifiers(EXTERNAL)
.build()
)
.setter(
FunSpec.setterBuilder()
.addModifiers(EXTERNAL)
.build()
)
.build()

assertThat(prop.toString()).isEqualTo(
"""
|var foo: kotlin.String
| external get
| external set
|""".trimMargin()
)
}

@Test fun inlineSingleAccessorVal() {
val prop = PropertySpec.builder("foo", String::class)
.getter(
Expand Down

0 comments on commit f53205e

Please sign in to comment.