Skip to content

Commit

Permalink
Merge pull request #1260 from roihershberg/support_external_getter
Browse files Browse the repository at this point in the history
Add support for external property getter
  • Loading branch information
Egorand committed Jun 10, 2022
2 parents 35da0d5 + 75898ae commit c17e163
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
9 changes: 7 additions & 2 deletions kotlinpoet/src/main/java/com/squareup/kotlinpoet/FunSpec.kt
Expand Up @@ -55,13 +55,18 @@ 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 {
require(body.isEmpty() || !builder.modifiers.containsAnyOf(ABSTRACT, EXPECT)) {
"abstract or expect function ${builder.name} cannot have code"
}
if (name == SETTER) {
if (name == GETTER) {
require(!isExternalGetter || body.isEmpty()) {
"external getter cannot have code"
}
} else if (name == SETTER) {
require(parameters.size <= 1) {
"$name can have at most one parameter"
}
Expand Down Expand Up @@ -155,7 +160,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,43 @@ 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 externalGetterCannotHaveBody() {
assertThrows<IllegalArgumentException> {
PropertySpec.builder("foo", String::class)
.getter(
FunSpec.getterBuilder()
.addModifiers(EXTERNAL)
.addStatement("return %S", "foo")
.build()
)
.build()
}.hasMessageThat().isEqualTo("external getter cannot have code")
}

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

0 comments on commit c17e163

Please sign in to comment.