Skip to content

Commit

Permalink
Use Class.forName instead of kotlin.reflect (#1544)
Browse files Browse the repository at this point in the history
  • Loading branch information
kzotin committed Oct 31, 2024
1 parent 12514f3 commit aa54636
Showing 1 changed file with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,36 @@ package io.realm.kotlin.internal.platform
import io.realm.kotlin.internal.RealmObjectCompanion
import io.realm.kotlin.types.BaseRealmObject
import kotlin.reflect.KClass
import kotlin.reflect.full.companionObjectInstance


// TODO OPTIMIZE Can we eliminate the reflective approach? Maybe by embedding the information
// through the compiler plugin or something similar to the Native findAssociatedObject
@PublishedApi
internal actual fun <T : Any> realmObjectCompanionOrNull(clazz: KClass<T>): RealmObjectCompanion? =
if (clazz.companionObjectInstance is RealmObjectCompanion) {
clazz.companionObjectInstance as RealmObjectCompanion
} else null
internal actual fun <T : Any> realmObjectCompanionOrNull(clazz: KClass<T>): RealmObjectCompanion? {
val cachedClass = reflectionCache[clazz]
if (cachedClass != null) {
return cachedClass
}

val companion = try {
Class.forName("${clazz.java.name}\$Companion").kotlin
} catch (thr: Throwable) {
try {
// For Parcelable classes
Class.forName("${clazz.java.name}\$CREATOR").kotlin
} catch (thr: Throwable) {
null
}
}?.objectInstance as? RealmObjectCompanion

if (companion != null) {
reflectionCache[clazz] = companion
}

return companion
}

private val reflectionCache = mutableMapOf<KClass<*>, RealmObjectCompanion>()

@PublishedApi
internal actual fun <T : BaseRealmObject> realmObjectCompanionOrThrow(clazz: KClass<T>): RealmObjectCompanion =
Expand Down

0 comments on commit aa54636

Please sign in to comment.