Skip to content

Commit 748545b

Browse files
committed
events: Prefer using Class
There was no reason to use `KClass` here
1 parent 5e99720 commit 748545b

File tree

5 files changed

+19
-19
lines changed

5 files changed

+19
-19
lines changed

BotCommands-core/src/main/kotlin/io/github/freya022/botcommands/internal/core/ClassPathFunction.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ private typealias ClassPathFunctionIterable = Iterable<ClassPathFunction>
1313

1414
internal sealed class ClassPathFunction {
1515
abstract val clazz: KClass<*>
16+
abstract val javaClazz: Class<*>
1617
abstract val instance: Any
1718

1819
abstract val function: KFunction<*>
@@ -38,6 +39,8 @@ internal class LazyClassPathFunction internal constructor(
3839
override val clazz: KClass<*>,
3940
method: Method
4041
) : ClassPathFunction() {
42+
43+
override val javaClazz: Class<*> get() = clazz.java
4144
override val function: KFunction<*> by lazy { method.asKFunction() }
4245
override val instance by context.serviceContainer.lazy(clazz)
4346
override val methodAccessor: MethodAccessor<*> by lazy { MethodAccessorFactoryProvider.getAccessorFactory().create(instance, function) }
@@ -52,6 +55,7 @@ internal class InstanceClassPathFunction internal constructor(
5255
override val function: KFunction<*>
5356
) : ClassPathFunction() {
5457
override val clazz: KClass<*> get() = instance::class
58+
override val javaClazz: Class<*> get() = instance.javaClass
5559
override val methodAccessor = MethodAccessorFactoryProvider.getAccessorFactory().create(instance, function)
5660
}
5761

BotCommands-core/src/main/kotlin/io/github/freya022/botcommands/internal/core/hooks/EventDispatcherImpl.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ internal class EventDispatcherImpl internal constructor(
2929

3030
internal fun onEvent(event: GenericEvent) {
3131
// No need to check for `event` type as if it's in the map, then it's recognized
32-
val handlers = eventListenerRegistry[event::class] ?: return
32+
val handlers = eventListenerRegistry[event.javaClass] ?: return
3333

3434
// Run blocking handlers first
3535
handlers[RunMode.BLOCKING]?.let { eventHandlers ->
@@ -63,7 +63,7 @@ internal class EventDispatcherImpl internal constructor(
6363

6464
override suspend fun dispatchEvent(event: Any) {
6565
// No need to check for `event` type as if it's in the map, then it's recognized
66-
val handlers = eventListenerRegistry[event::class] ?: return
66+
val handlers = eventListenerRegistry[event.javaClass] ?: return
6767

6868
// Run blocking handlers first
6969
handlers[RunMode.BLOCKING]?.forEach { eventHandler ->
@@ -86,7 +86,7 @@ internal class EventDispatcherImpl internal constructor(
8686
override fun dispatchEventAsync(event: Any): List<Deferred<Unit>> {
8787
// Try not to switch context on non-handled events
8888
// No need to check for `event` type as if it's in the map, then it's recognized
89-
val handlers = eventListenerRegistry[event::class] ?: return emptyList()
89+
val handlers = eventListenerRegistry[event.javaClass] ?: return emptyList()
9090

9191
return handlers.map { eventHandler ->
9292
asyncCoroutineScope.async { runEventHandler(eventHandler, event) }

BotCommands-core/src/main/kotlin/io/github/freya022/botcommands/internal/core/hooks/EventListenerRegistry.kt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,19 @@ import io.github.freya022.botcommands.internal.core.service.canCreateWrappedServ
1616
import io.github.freya022.botcommands.internal.core.service.tryGetWrappedService
1717
import io.github.freya022.botcommands.internal.core.toClassPathFunctions
1818
import io.github.freya022.botcommands.internal.utils.*
19-
import io.github.freya022.botcommands.internal.utils.ReflectionUtils.declaringClass
2019
import io.github.freya022.botcommands.internal.utils.ReflectionUtils.nonInstanceParameters
2120
import io.github.oshai.kotlinlogging.KotlinLogging
2221
import net.dv8tion.jda.api.events.Event
2322
import net.dv8tion.jda.api.events.GenericEvent
2423
import net.dv8tion.jda.api.requests.GatewayIntent
2524
import java.util.concurrent.ConcurrentHashMap
26-
import kotlin.reflect.KClass
2725
import kotlin.reflect.full.functions
2826
import kotlin.reflect.jvm.jvmErasure
2927
import kotlin.time.Duration
3028
import kotlin.time.toDuration
3129
import kotlin.time.toDurationUnit
3230

33-
private typealias EventMap = MutableMap<KClass<*>, EventListenerList>
31+
private typealias EventMap = MutableMap<Class<*>, EventListenerList>
3432

3533
private val logger = KotlinLogging.logger { }
3634

@@ -54,7 +52,7 @@ internal class EventListenerRegistry internal constructor(
5452
.addAsEventListeners()
5553
}
5654

57-
internal operator fun get(eventType: KClass<*>): EventListenerList? {
55+
internal operator fun get(eventType: Class<*>): EventListenerList? {
5856
return map[eventType]
5957
}
6058

@@ -67,7 +65,7 @@ internal class EventListenerRegistry internal constructor(
6765
}
6866

6967
internal fun removeEventListener(listener: Any) {
70-
listeners.remove(listener::class.java)?.let { instanceMap ->
68+
listeners.remove(listener.javaClass)?.let { instanceMap ->
7169
instanceMap.forEach { (kClass, functions) ->
7270
val functionMap = map[kClass]
7371
?: throwInternal("Listener was registered without having its functions added to the listener map")
@@ -89,10 +87,10 @@ internal class EventListenerRegistry internal constructor(
8987

9088
val parameters = function.nonInstanceParameters
9189

92-
val eventErasure = parameters.first().type.jvmErasure
90+
val eventErasure = parameters.first().type.jvmErasure.java
9391
if (!annotation.ignoreIntents && eventErasure.isSubclassOf<Event>()) {
9492
@Suppress("UNCHECKED_CAST")
95-
val requiredIntents = GatewayIntent.fromEvents(eventErasure.java as Class<out Event>)
93+
val requiredIntents = GatewayIntent.fromEvents(eventErasure as Class<out Event>)
9694
val missingIntents = requiredIntents - jdaService.intents - config.ignoredIntents - annotation.ignoredIntents
9795
if (missingIntents.isNotEmpty()) {
9896
return@forEach logger.debug { "Skipping event listener ${function.shortSignature} as it is missing intents: $missingIntents" }
@@ -123,7 +121,7 @@ internal class EventListenerRegistry internal constructor(
123121
})
124122

125123
val allEventTypes = eventTreeService.getSubclasses(eventErasure) + eventErasure
126-
classPathFunc.function.declaringClass.java.let { clazz ->
124+
classPathFunc.javaClazz.let { clazz ->
127125
val instanceMap = listeners.computeIfAbsent(clazz) { hashMapOf() }
128126

129127
allEventTypes.forEach {

BotCommands-core/src/main/kotlin/io/github/freya022/botcommands/internal/core/hooks/EventTreeService.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,23 @@ import io.github.freya022.botcommands.api.core.service.annotations.BService
66
import io.github.oshai.kotlinlogging.KotlinLogging
77
import net.dv8tion.jda.api.events.GenericEvent
88
import java.util.*
9-
import kotlin.reflect.KClass
10-
import kotlin.reflect.jvm.jvmName
119

1210
private val logger = KotlinLogging.logger { }
1311

1412
@BService
1513
internal class EventTreeService internal constructor() {
16-
private val map: Map<KClass<*>, List<KClass<*>>> = ClassGraph()
14+
private val map: Map<Class<*>, List<Class<*>>> = ClassGraph()
1715
.acceptPackages(GenericEvent::class.java.packageName, BGenericEvent::class.java.packageName)
1816
.disableRuntimeInvisibleAnnotations()
1917
.disableModuleScanning()
2018
.enableClassInfo()
2119
.scan().use { scanResult ->
2220
(scanResult.getClassesImplementing(GenericEvent::class.java) + scanResult.getClassesImplementing(BGenericEvent::class.java)).associate { info ->
23-
info.loadClass().kotlin to Collections.unmodifiableList(info.subclasses.map { subclassInfo -> subclassInfo.loadClass().kotlin })
21+
info.loadClass() to Collections.unmodifiableList(info.subclasses.map { subclassInfo -> subclassInfo.loadClass() })
2422
}
2523
}
2624

27-
internal fun getSubclasses(kClass: KClass<*>): List<KClass<*>> = map[kClass] ?: emptyList<KClass<*>>().also {
28-
logger.warn { "Unknown event type: ${kClass.jvmName}" }
25+
internal fun getSubclasses(clazz: Class<*>): List<Class<*>> = map[clazz] ?: emptyList<Class<*>>().also {
26+
logger.warn { "Unknown event type: ${clazz.name}" }
2927
}
30-
}
28+
}

BotCommands-core/src/test/kotlin/io/github/freya022/botcommands/core/hooks/EventDispatcherTests.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ object EventDispatcherTests {
4040
val expectedInstance = ReadyTestListener()
4141
val expectedFunction = ReadyTestListener::onReady
4242
val listenerRegistry = mockk<EventListenerRegistry> {
43-
every { get(BReadyEvent::class) } returns mockk<EventListenerList> {
43+
every { get(BReadyEvent::class.java) } returns mockk<EventListenerList> {
4444
every { get(any<BEventListener.RunMode>()) } returns emptyList()
4545
every { get(BEventListener.RunMode.BLOCKING) } returns listOf(
4646
EventHandlerFunction(

0 commit comments

Comments
 (0)