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 2 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(separator = ", ", prefix = " : "))
Egorand marked this conversation as resolved.
Show resolved Hide resolved
}
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 Down Expand Up @@ -4416,6 +4416,47 @@ 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()
.primaryConstructor(
FunSpec.constructorBuilder()
.build(),
)
TrevorSStone marked this conversation as resolved.
Show resolved Hide resolved
.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.asTypeName()),
TrevorSStone marked this conversation as resolved.
Show resolved Hide resolved
)
.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> {
Expand Down