From 0296ac85b4dfe7739ed05ecf54a277994a7fedfb Mon Sep 17 00:00:00 2001 From: Kacper Urbaniec Date: Sat, 13 Jan 2024 11:43:43 +0100 Subject: [PATCH] Unwrapper registration now triggers on `ApplicationStartingEvent` TODO: * Create `spring.factories` wrapper * Port Instantiators to also use `ApplicationStartingEvent` --- .../codegen/BeePersistentDSLCodegen.kt | 31 +++++++++++++------ .../persistent/blaze/meta/dsl/TestListener.kt | 6 ++-- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/bee.persistent/src/blaze-processor/kotlin/com/beeproduced/bee/persistent/blaze/processor/codegen/BeePersistentDSLCodegen.kt b/bee.persistent/src/blaze-processor/kotlin/com/beeproduced/bee/persistent/blaze/processor/codegen/BeePersistentDSLCodegen.kt index e172856..6e9b8a7 100644 --- a/bee.persistent/src/blaze-processor/kotlin/com/beeproduced/bee/persistent/blaze/processor/codegen/BeePersistentDSLCodegen.kt +++ b/bee.persistent/src/blaze-processor/kotlin/com/beeproduced/bee/persistent/blaze/processor/codegen/BeePersistentDSLCodegen.kt @@ -3,7 +3,9 @@ package com.beeproduced.bee.persistent.blaze.processor.codegen import com.beeproduced.bee.generative.util.PoetMap import com.beeproduced.bee.generative.util.PoetMap.Companion.addNStatementBuilder import com.beeproduced.bee.generative.util.toPoetClassName +import com.beeproduced.bee.persistent.blaze.processor.codegen.BeePersistentDSLCodegen.PoetConstants.APPLICATION_LISTENER import com.beeproduced.bee.persistent.blaze.processor.codegen.BeePersistentDSLCodegen.PoetConstants.APPLICATION_READY_EVENT +import com.beeproduced.bee.persistent.blaze.processor.codegen.BeePersistentDSLCodegen.PoetConstants.APPLICATION_STARTING_EVENT import com.beeproduced.bee.persistent.blaze.processor.codegen.BeePersistentDSLCodegen.PoetConstants.BEE_SELECTION import com.beeproduced.bee.persistent.blaze.processor.codegen.BeePersistentDSLCodegen.PoetConstants.CLAZZ import com.beeproduced.bee.persistent.blaze.processor.codegen.BeePersistentDSLCodegen.PoetConstants.COMPONENT @@ -80,6 +82,8 @@ class BeePersistentDSLCodegen( const val INLINE_VALUE_UNWRAPPERS = "%inlinevalueunwrappers:T" const val INLINE_VALUE_CLAZZ = "%inlinevalueclazz:T" const val INNER_CLAZZ = "%innerclass:T" + const val APPLICATION_LISTENER = "%applicationlistener:T" + const val APPLICATION_STARTING_EVENT = "%applicationstartingevent:T" } init { @@ -98,6 +102,8 @@ class BeePersistentDSLCodegen( poetMap.addMapping(EVENT_LISTENER, ClassName("org.springframework.context.event", "EventListener")) poetMap.addMapping(APPLICATION_READY_EVENT, ClassName("org.springframework.boot.context.event", "ApplicationReadyEvent")) poetMap.addMapping(INLINE_VALUE_UNWRAPPERS, ClassName("com.beeproduced.bee.persistent.blaze.meta.dsl", "InlineValueUnwrappers")) + poetMap.addMapping(APPLICATION_LISTENER, ClassName("org.springframework.context", "ApplicationListener")) + poetMap.addMapping(APPLICATION_STARTING_EVENT, ClassName("org.springframework.boot.context.event", "ApplicationStartingEvent")) } @@ -403,22 +409,29 @@ class BeePersistentDSLCodegen( } private fun FileSpec.Builder.buildRegistration() = apply { - + if (inlineValues.isEmpty()) return@apply // TODO: Rewrite as listener !! // Also update view instantiator !! + val event = poetMap.classMapping(APPLICATION_STARTING_EVENT) + val listenerInterface = poetMap.classMapping(APPLICATION_LISTENER) + .parameterizedBy(event) + + val registrationName = buildUniqueClassName(packageName, "UnwrapperRegistration") val registration = TypeSpec - .classBuilder(buildUniqueClassName(packageName, "UnwrapperRegistration")) - .addAnnotation(poetMap.classMapping(COMPONENT)) + .classBuilder(registrationName) + .addSuperinterface(listenerInterface) - val eventListenerAnnotation = AnnotationSpec - .builder(poetMap.classMapping(EVENT_LISTENER)) - .addMember("%T::class", poetMap.classMapping(APPLICATION_READY_EVENT)) + // val eventListenerAnnotation = AnnotationSpec + // .builder(poetMap.classMapping(EVENT_LISTENER)) + // .addMember("%T::class", poetMap.classMapping(APPLICATION_READY_EVENT)) val register = FunSpec - .builder("register") - .addAnnotation(eventListenerAnnotation.build()) + .builder("onApplicationEvent") + .addModifiers(KModifier.OVERRIDE) + .addParameter("event", event) + // .addAnnotation(eventListenerAnnotation.build()) register.apply { // Use ยท to omit line breaks // See: https://github.com/square/kotlinpoet/issues/598#issuecomment-454337042 @@ -434,7 +447,7 @@ class BeePersistentDSLCodegen( addType(registration.build()) - writeToFile("spring", "org.springframework.context.ApplicationListener=com.beeproduced.bee.persistent.blaze.meta.dsl.TestListener") + writeToFile("spring", "org.springframework.context.ApplicationListener=$packageName.$registrationName") } // https://stackoverflow.com/a/76545160/12347616 diff --git a/bee.persistent/src/blaze/kotlin/com/beeproduced/bee/persistent/blaze/meta/dsl/TestListener.kt b/bee.persistent/src/blaze/kotlin/com/beeproduced/bee/persistent/blaze/meta/dsl/TestListener.kt index fb46d30..744f14e 100644 --- a/bee.persistent/src/blaze/kotlin/com/beeproduced/bee/persistent/blaze/meta/dsl/TestListener.kt +++ b/bee.persistent/src/blaze/kotlin/com/beeproduced/bee/persistent/blaze/meta/dsl/TestListener.kt @@ -1,5 +1,7 @@ package com.beeproduced.bee.persistent.blaze.meta.dsl +import org.springframework.boot.context.event.ApplicationStartedEvent +import org.springframework.boot.context.event.ApplicationStartingEvent import org.springframework.context.ApplicationEvent import org.springframework.context.ApplicationListener @@ -16,8 +18,8 @@ import org.springframework.context.ApplicationListener // TODO: Remove -class TestListener : ApplicationListener { - override fun onApplicationEvent(event: ApplicationEvent) { +class TestListener : ApplicationListener { + override fun onApplicationEvent(event: ApplicationStartingEvent) { println("event: $event") } } \ No newline at end of file