konrad-kaminski / spring-kotlin-coroutine

Kotlin coroutine support for Spring.
448 stars 69 forks source link

Demo - Utils.kt means companion objects not allowed in Spring Component? #15

Closed bdueck closed 6 years ago

bdueck commented 6 years ago

Hi - first off, great job on putting together coroutine support for Spring.

I've run into a small problem as I've been exploring the demo.

Specifically, adding a companion object to a Spring Component runs into problems. The problem seems to be with the magic being done in Utils.kt with unwrapCompanionClass. Modifying DemoService by adding the "foo" companion object as I've done below, causes a compile error with the "logger" reference as shown below.

@Component open class DemoService {

companion object foo {
    val someStaticCounter = AtomicInteger(0)

}
@Scheduled(cron = "0 0 0 1 1 *")
suspend open fun newYear() {
    // logger will now cause a compile error
    logger.info("Happy New Year!")
}

...

konrad-kaminski commented 6 years ago

You removed private val logger = logger() from companion object and therefore there's no logger any more. Just add it back to your companion object and everything should work fine.

bdueck commented 6 years ago

I left the anonymous companion object declaration at the end of the class. What I was doing was introducing a new named companion object. What was unexpected is that Utils.kt insisted that I have private val logger = logger() in my new companion object.

konrad-kaminski commented 6 years ago

You cannot have two companion objects in a single class.

bdueck commented 6 years ago

Yep - thanks, dumb mistake on my part. I wasn't aware of that rule in Kotlin - and didn't notice the compiler flagging this - I just focused on the error markets on logger references. Apologies for this.