Skip to content

Commit

Permalink
Unwrapper registration now triggers on ApplicationStartingEvent
Browse files Browse the repository at this point in the history
TODO:
* Create `spring.factories` wrapper
* Port Instantiators to also use `ApplicationStartingEvent`
  • Loading branch information
kurbaniec committed Jan 13, 2024
1 parent 3abdc59 commit 0296ac8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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"))
}


Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -16,8 +18,8 @@ import org.springframework.context.ApplicationListener

// TODO: Remove

class TestListener : ApplicationListener<ApplicationEvent?> {
override fun onApplicationEvent(event: ApplicationEvent) {
class TestListener : ApplicationListener<ApplicationStartingEvent> {
override fun onApplicationEvent(event: ApplicationStartingEvent) {
println("event: $event")
}
}

0 comments on commit 0296ac8

Please sign in to comment.