Skip to content
This repository was archived by the owner on Dec 10, 2025. It is now read-only.

Commit 0cc16b0

Browse files
committed
refactor(event): enhance GeneratedInvoker with owner field and access method
1 parent fdd73db commit 0cc16b0

File tree

1 file changed

+19
-14
lines changed
  • surf-cloud-core/surf-cloud-core-common/src/main/kotlin/dev/slne/surf/cloud/core/common/event

1 file changed

+19
-14
lines changed

surf-cloud-core/surf-cloud-core-common/src/main/kotlin/dev/slne/surf/cloud/core/common/event/CloudEventBusImpl.kt

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,18 @@ import dev.slne.surf.cloud.api.common.util.mutableObjectListOf
1111
import dev.slne.surf.cloud.api.common.util.synchronize
1212
import dev.slne.surf.cloud.core.common.coroutines.CloudEventBusScope
1313
import dev.slne.surf.surfapi.core.api.util.findAnnotation
14-
import io.ktor.client.plugins.cache.storage.FileStorage
1514
import it.unimi.dsi.fastutil.objects.ObjectList
1615
import kotlinx.coroutines.launch
1716
import kotlinx.coroutines.withContext
1817
import net.bytebuddy.ByteBuddy
1918
import net.bytebuddy.description.modifier.FieldManifestation
2019
import net.bytebuddy.description.modifier.Visibility
21-
import net.bytebuddy.description.type.TypeDescription
2220
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy
2321
import net.bytebuddy.implementation.FieldAccessor
2422
import net.bytebuddy.implementation.MethodCall
2523
import net.bytebuddy.implementation.bytecode.assign.Assigner
26-
import net.bytebuddy.implementation.bytecode.assign.TypeCasting
27-
import net.bytebuddy.implementation.bytecode.member.MethodVariableAccess
2824
import net.bytebuddy.matcher.ElementMatchers
29-
import net.bytebuddy.utility.CompoundList
3025
import org.springframework.core.ResolvableType
31-
import org.springframework.core.convert.TypeDescriptor
3226
import org.springframework.expression.spel.standard.SpelExpressionParser
3327
import org.springframework.expression.spel.support.StandardEvaluationContext
3428
import java.lang.reflect.Method
@@ -125,14 +119,19 @@ class CloudEventBusImpl : CloudEventBus {
125119
.subclass(EventListenerInvoker::class.java)
126120
.implement(GeneratedInvoker::class.java)
127121
.defineField("owner", instance.javaClass, Visibility.PRIVATE, FieldManifestation.FINAL)
122+
.defineMethod("getOwner", Any::class.java, Visibility.PUBLIC)
123+
.intercept(FieldAccessor.ofField("owner"))
128124
.defineConstructor(Visibility.PUBLIC).withParameters(instance.javaClass)
129-
.intercept(MethodCall.invoke(Object::class.java.getConstructor())
130-
.andThen(FieldAccessor.ofField("owner").setsArgumentAt(0)))
125+
.intercept(
126+
MethodCall.invoke(Object::class.java.getConstructor())
127+
.andThen(FieldAccessor.ofField("owner").setsArgumentAt(0))
128+
)
131129
.method(ElementMatchers.named("invoke"))
132-
.intercept(MethodCall.invoke(method)
133-
.onField("owner")
134-
.withArgument(0)
135-
.withAssigner(Assigner.DEFAULT, Assigner.Typing.DYNAMIC)
130+
.intercept(
131+
MethodCall.invoke(method)
132+
.onField("owner")
133+
.withArgument(0)
134+
.withAssigner(Assigner.DEFAULT, Assigner.Typing.DYNAMIC)
136135
)
137136
.make()
138137
.load(instance.javaClass.classLoader, ClassLoadingStrategy.Default.INJECTION)
@@ -143,15 +142,21 @@ class CloudEventBusImpl : CloudEventBus {
143142
}
144143

145144
private class ReflectionInvoker(val instance: Any, val method: Method) : EventListenerInvoker {
146-
init { method.isAccessible = true }
145+
init {
146+
method.isAccessible = true
147+
}
148+
147149
override suspend fun invoke(event: CloudEvent) {
148150
CloudEventBusScope.launch {
149151
val kfn = method.kotlinFunction ?: error("Not a Kotlin function")
150152
kfn.callSuspend(instance, event)
151153
}.join()
152154
}
153155
}
154-
internal interface GeneratedInvoker { val owner: Any }
156+
157+
internal interface GeneratedInvoker {
158+
val owner: Any
159+
}
155160
}
156161

157162
val cloudEventBusImpl = CloudEventBus.instance as CloudEventBusImpl

0 commit comments

Comments
 (0)