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 support for delegates to Anonymous classes #2034

Merged
Merged
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
2 changes: 2 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Change Log

## Unreleased

* Fix: Support delegates on anonymous classes. (#2034)

## Version 2.0.0

Thanks to [@brokenhappy][brokenhappy], [@tajobe][tajobe], [@niyajali][niyajali],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,23 +142,24 @@ public class TypeSpec private constructor(
codeWriter.emitCode("object")
val supertype = if (superclass != ANY) {
if (!areNestedExternal && !modifiers.contains(EXPECT)) {
listOf(CodeBlock.of(" %T(%L)", superclass, superclassConstructorParametersBlock))
listOf(CodeBlock.of("%T(%L)", superclass, superclassConstructorParametersBlock))
} else {
listOf(CodeBlock.of(" %T", superclass))
listOf(CodeBlock.of("%T", superclass))
}
} else {
listOf()
}

val allSuperTypes = supertype + if (superinterfaces.isNotEmpty()) {
superinterfaces.keys.map { CodeBlock.of(" %T", it) }
} else {
emptyList()
val allSuperTypes = supertype + superinterfaces.entries.map { (type, init) ->
if (init == null) {
CodeBlock.of("%T", type)
} else {
CodeBlock.of("%T by %L", type, init)
}
}

if (allSuperTypes.isNotEmpty()) {
codeWriter.emitCode(" :")
codeWriter.emitCode(allSuperTypes.joinToCode(","))
codeWriter.emitCode(allSuperTypes.joinToCode(prefix = " : "))
}
if (hasNoBody) {
codeWriter.emit(" {\n}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4367,7 +4367,7 @@ class TypeSpecTest {
val type = TypeSpec.objectBuilder("Guac")
.addSuperinterface(
Consumer::class.parameterizedBy(String::class),
CodeBlock.of("({ println(it) })"),
CodeBlock.of("Consumer({ println(it) })"),
)
.build()

Expand All @@ -4377,7 +4377,7 @@ class TypeSpecTest {
|import java.util.function.Consumer
|import kotlin.String
|
|public object Guac : Consumer<String> by ({ println(it) })
|public object Guac : Consumer<String> by Consumer({ println(it) })
|
""".trimMargin()

Expand All @@ -4396,7 +4396,7 @@ class TypeSpecTest {
)
.addSuperinterface(
Runnable::class,
CodeBlock.of("Runnable ({ %T.debug(\"Hello world\") })", Logger::class.asTypeName()),
CodeBlock.of("Runnable ({ %T.debug(\"Hello world\") })", Logger::class),
)
.build()

Expand All @@ -4417,6 +4417,44 @@ class TypeSpecTest {
assertThat(toString(type)).isEqualTo(expect)
}

// https://github.com/square/kotlinpoet/issues/2033
@Test fun testDelegateOnAnonymousObject() {
val type = TypeSpec.anonymousClassBuilder()
.addSuperinterface(
Consumer::class.parameterizedBy(String::class),
CodeBlock.of("java.util.function.Consumer({ println(it) })"),
)
.build()

val expect = """
|object : java.util.function.Consumer<kotlin.String> by java.util.function.Consumer({ println(it) }) {
|}
""".trimMargin()

assertThat(type.toString()).isEqualTo(expect)
}

// https://github.com/square/kotlinpoet/issues/2033
@Test fun testMultipleDelegatesOnAnonymousObject() {
val type = TypeSpec.anonymousClassBuilder()
.addSuperinterface(
Function::class.parameterizedBy(String::class, Int::class),
CodeBlock.of("kotlin.Function ({ text -> text.toIntOrNull() ?: 0 })"),
)
.addSuperinterface(
Runnable::class,
CodeBlock.of("java.lang.Runnable ({ %T.debug(\"Hello world\") })", Logger::class),
)
.build()

val expect = """
|object : kotlin.Function<kotlin.String, kotlin.Int> by kotlin.Function ({ text -> text.toIntOrNull() ?: 0 }), java.lang.Runnable by java.lang.Runnable ({ java.util.logging.Logger.debug("Hello world") }) {
|}
""".trimMargin()

assertThat(type.toString()).isEqualTo(expect)
}

@Test fun testNoSuchParameterDelegate() {
assertThrows<IllegalArgumentException> {
TypeSpec.classBuilder("Taco")
Expand Down
Loading