Skip to content

Commit b791061

Browse files
author
Egor Andreevici
committed
Add support for anonymous functions
1 parent a1749ce commit b791061

File tree

5 files changed

+42
-11
lines changed

5 files changed

+42
-11
lines changed

src/main/java/com/squareup/kotlinpoet/CodeBlock.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ class CodeBlock private constructor(
333333
is CharSequence -> o.toString()
334334
is ParameterSpec -> o.name
335335
is PropertySpec -> o.name
336-
is FunSpec -> o.name
336+
is FunSpec -> o.name!!
337337
is TypeSpec -> o.name!!
338338
else -> throw IllegalArgumentException("expected name but was " + o)
339339
}

src/main/java/com/squareup/kotlinpoet/FunSpec.kt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ class FunSpec private constructor(builder: Builder) {
7171
codeWriter.emitAnnotations(annotations, false)
7272
codeWriter.emitModifiers(modifiers, implicitModifiers)
7373

74-
if (!isConstructor && !name.isAccessor) {
74+
if (name == null) {
75+
codeWriter.emit("fun")
76+
} else if (!isConstructor && !name.isAccessor) {
7577
codeWriter.emit("fun ")
7678
}
7779

@@ -117,7 +119,9 @@ class FunSpec private constructor(builder: Builder) {
117119
codeWriter.emitCode("%T.", receiverType)
118120
}
119121
}
120-
codeWriter.emitCode("%L", escapeIfNecessary(name))
122+
if (name != null) {
123+
codeWriter.emitCode("%L", escapeIfNecessary(name))
124+
}
121125
}
122126

123127
parameters.emit(codeWriter) { param ->
@@ -181,7 +185,7 @@ class FunSpec private constructor(builder: Builder) {
181185
return builder
182186
}
183187

184-
class Builder internal constructor(internal val name: String) {
188+
class Builder internal constructor(internal val name: String?) {
185189
internal val kdoc = CodeBlock.builder()
186190
internal var receiverType: TypeName? = null
187191
internal var returnType: TypeName? = null
@@ -366,8 +370,8 @@ class FunSpec private constructor(builder: Builder) {
366370
internal const val GETTER = "get()"
367371
internal const val SETTER = "set()"
368372

369-
internal val String.isConstructor get() = this == CONSTRUCTOR
370-
internal val String.isAccessor get() = this.isOneOf(GETTER, SETTER)
373+
internal val String?.isConstructor get() = this == CONSTRUCTOR
374+
internal val String?.isAccessor get() = this != null && this.isOneOf(GETTER, SETTER)
371375

372376
private val EXPRESSION_BODY_PREFIX = CodeBlock.of("return ")
373377

@@ -379,6 +383,8 @@ class FunSpec private constructor(builder: Builder) {
379383

380384
@JvmStatic fun setterBuilder() = Builder(SETTER)
381385

386+
@JvmStatic fun anonymousFunctionBuilder() = Builder(null)
387+
382388
/**
383389
* Returns a new fun spec builder that overrides `method`.
384390

src/main/java/com/squareup/kotlinpoet/ParameterSpec.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ class ParameterSpec private constructor(builder: ParameterSpec.Builder) {
3434
codeWriter.emitAnnotations(annotations, true)
3535
codeWriter.emitModifiers(modifiers)
3636
if (name.isNotEmpty()) codeWriter.emitCode("%L", escapeIfNecessary(name))
37-
if (name.isNotEmpty() && includeType) codeWriter.emit(": ")
38-
if (includeType) codeWriter.emitCode("%T", type)
37+
val emitType = type != null && includeType
38+
if (name.isNotEmpty() && emitType) codeWriter.emit(": ")
39+
if (emitType) codeWriter.emitCode("%T", type)
3940
emitDefaultValue(codeWriter)
4041
}
4142

@@ -56,7 +57,7 @@ class ParameterSpec private constructor(builder: ParameterSpec.Builder) {
5657

5758
override fun toString() = buildString { emit(CodeWriter(this)) }
5859

59-
fun toBuilder(name: String = this.name, type: TypeName = this.type): Builder {
60+
fun toBuilder(name: String = this.name, type: TypeName? = this.type): Builder {
6061
val builder = Builder(name, type)
6162
builder.annotations += annotations
6263
builder.modifiers += modifiers
@@ -66,7 +67,7 @@ class ParameterSpec private constructor(builder: ParameterSpec.Builder) {
6667

6768
class Builder internal constructor(
6869
internal val name: String,
69-
internal val type: TypeName
70+
internal val type: TypeName?
7071
) {
7172
internal var defaultValue: CodeBlock? = null
7273

@@ -152,6 +153,8 @@ class ParameterSpec private constructor(builder: ParameterSpec.Builder) {
152153
@JvmStatic fun unnamed(type: Type) = unnamed(type.asTypeName())
153154

154155
@JvmStatic fun unnamed(type: TypeName) = Builder("", type).build()
156+
157+
@JvmStatic fun untyped(name: String) = Builder(name, null).build()
155158
}
156159
}
157160

src/main/java/com/squareup/kotlinpoet/Util.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ internal fun requireNoneOf(modifiers: Set<KModifier>, vararg forbidden: KModifie
5555
}
5656
}
5757

58-
internal fun <T> T.isOneOf(t1: T, t2: T, t3: T? = null, t4: T? = null, t5: T? = null, t6: T? = null) =
58+
internal fun <T : Any> T.isOneOf(t1: T, t2: T, t3: T? = null, t4: T? = null, t5: T? = null, t6: T? = null) =
5959
this == t1 || this == t2 || this == t3 || this == t4 || this == t5 || this == t6
6060

6161
internal fun <T> Collection<T>.containsAnyOf(vararg t: T) = t.any(this::contains)

src/test/java/com/squareup/kotlinpoet/FunSpecTest.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,4 +479,26 @@ class FunSpecTest {
479479

480480
assertThat(builder.build().parameters).containsExactly(seasoning)
481481
}
482+
483+
@Test fun anonymousFunction() {
484+
val funSpec = FunSpec.anonymousFunctionBuilder()
485+
.addParameter("x", Int::class)
486+
.addParameter("y", Int::class)
487+
.returns(Int::class)
488+
.addStatement("return x + y")
489+
.build()
490+
assertThat(funSpec.toString()).isEqualTo("""
491+
|fun(x: kotlin.Int, y: kotlin.Int): kotlin.Int = x + y
492+
|""".trimMargin())
493+
}
494+
495+
@Test fun anonymousFunctionWithUntypedParameters() {
496+
val funSpec = FunSpec.anonymousFunctionBuilder()
497+
.addParameter(ParameterSpec.untyped("item"))
498+
.addStatement("return item > 0")
499+
.build()
500+
assertThat(funSpec.toString()).isEqualTo("""
501+
|fun(item) = item > 0
502+
|""".trimMargin())
503+
}
482504
}

0 commit comments

Comments
 (0)