Skip to content

Commit

Permalink
added serializers to fix bean-def kryozation
Browse files Browse the repository at this point in the history
  • Loading branch information
Holger Brandl committed Nov 9, 2021
1 parent dccc1e6 commit b8152c6
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 16 deletions.
96 changes: 94 additions & 2 deletions src/main/java/KryoBeanDefinition.kt
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
import com.esotericsoftware.kryo.Kryo
import com.esotericsoftware.kryo.Serializer
import com.esotericsoftware.kryo.io.Input
import com.esotericsoftware.kryo.io.Output
import org.koin.core.context.GlobalContext
import org.koin.core.definition.BeanDefinition
import org.koin.core.definition.Definition
import org.koin.core.definition.Kind
import org.koin.core.parameter.ParametersHolder
import org.koin.core.qualifier.Qualifier
import org.koin.core.qualifier.StringQualifier
import org.koin.core.qualifier.TypeQualifier
import org.koin.core.scope.Scope
import org.koin.core.scope.ScopeID
import java.awt.Dimension
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import kotlin.reflect.KClass

fun main() {
val saveFile = File("file.bin")
val kryo = buildKryo()

val smthg: Definition<Dimension> = { it -> Dimension() }

val beanDef = BeanDefinition<Dimension>(
val beanDef = BeanDefinition(
StringQualifier("foo"),
Dimension::class,
definition = smthg,
Expand All @@ -46,11 +50,99 @@ fun main() {
}


private fun buildKryo(): Kryo {
fun buildKryo(): Kryo {
val kryo = Kryo()

kryo.setOptimizedGenerics(false);
kryo.setReferences(true)
kryo.isRegistrationRequired = false

// kryo.register(InstanceRegistry::class.java, object : FieldSerializer<Any?>(kryo, BeanDefinition::class.java) {
// override fun create(kryo: Kryo, input: Input, type: Class<*>?): BeanDefinition {
// return BeanDefinition()
// }
// })
kryo.register(BeanDefinition::class.java, object : Serializer<BeanDefinition<*>>() {

override fun write(kryo: Kryo?, output: Output?, beanDef: BeanDefinition<*>) {
kryo!!.writeObject(output, beanDef.scopeQualifier)
kryo.writeClassAndObject(output, beanDef.primaryType)
kryo.writeObjectOrNull(output, beanDef.qualifier, Qualifier::class.java)
kryo.writeClassAndObject(output, beanDef.definition)
kryo.writeObject(output, beanDef.kind)
kryo.writeClassAndObject(output, beanDef.secondaryTypes)

println(beanDef)
}

override fun read(
kryo: Kryo?,
input: Input?,
type: Class<out BeanDefinition<*>>?
): BeanDefinition<*> {

val scopeQualifier = kryo!!.readObject(input, StringQualifier::class.java)
val primaryType = kryo.readClassAndObject(input) as KClass<*>
val qualifier = kryo.readObjectOrNull(input, Qualifier::class.java)
val definition = kryo.readClassAndObject(input) as Definition<*>
val kind = kryo.readObject(input, Kind::class.java)
val secondaryTypes = kryo.readClassAndObject(input) as List<KClass<*>>

return BeanDefinition(scopeQualifier, primaryType, qualifier, definition, kind, secondaryTypes)
}
})


kryo.register(TypeQualifier::class.java, object : Serializer<TypeQualifier>() {

override fun write(kryo: Kryo?, output: Output?, qualifier: TypeQualifier) {
kryo!!.writeObjectOrNull(output, qualifier.type, TypeQualifier::class.java)
}

override fun read(
kryo: Kryo?,
input: Input?,
type: Class<out TypeQualifier>?
): TypeQualifier {

val type = kryo!!.readObjectOrNull(input, KClass::class.java)
return TypeQualifier(type)
}
})

kryo.register(StringQualifier::class.java, object : Serializer<StringQualifier>() {

override fun write(kryo: Kryo?, output: Output?, qualifier: StringQualifier) {
kryo!!.writeObject(output, qualifier.value)
}

override fun read(
kryo: Kryo?,
input: Input?,
type: Class<out StringQualifier>?
): StringQualifier {

val value = kryo!!.readObjectOrNull(input, String::class.java)
return StringQualifier(value)
}
})

kryo.register(kotlin.jvm.internal.ClassReference::class.java, object : Serializer<kotlin.jvm.internal.ClassReference>() {

override fun write(kryo: Kryo?, output: Output?, qualifier: kotlin.jvm.internal.ClassReference) {
kryo!!.writeClassAndObject(output, qualifier.jClass)
}

override fun read(
kryo: Kryo?,
input: Input?,
type: Class<out kotlin.jvm.internal.ClassReference>?
): kotlin.jvm.internal.ClassReference {

val value = kryo!!.readClassAndObject(input) as java.lang.Class<*>
return kotlin.jvm.internal.ClassReference(value)
}
})

return kryo
}
4 changes: 2 additions & 2 deletions src/main/java/KryoDefinition.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import java.io.FileOutputStream

fun main() {
val saveFile = File("file.bin")
val kryo = buildKryo()
val kryo = buildKryoDefinition()

val smthg: Definition<Dimension> = { it -> Dimension() }

Expand All @@ -35,7 +35,7 @@ fun main() {
}


private fun buildKryo(): Kryo {
private fun buildKryoDefinition(): Kryo {

val kryo = Kryo()

Expand Down
10 changes: 0 additions & 10 deletions src/main/java/KryoInstanceFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,3 @@ fun main() {
restored.get(InstanceContext(koin, Scope(StringQualifier("foo"), ScopeID(), false, koin), ParametersHolder()))
println("result is ${result}")
}


private fun buildKryo(): Kryo {
val kryo = Kryo()

kryo.setReferences(true)
kryo.isRegistrationRequired = false

return kryo
}
37 changes: 37 additions & 0 deletions src/main/java/KryoKoin.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import com.esotericsoftware.kryo.io.Input
import com.esotericsoftware.kryo.io.Output
import org.koin.core.Koin
import org.koin.core.context.GlobalContext
import org.koin.dsl.module
import java.awt.Dimension
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream

fun main() {
val saveFile = File("file.bin")
val kryo = buildKryo()

val koin = GlobalContext.startKoin {}.koin

koin. loadModules(listOf(
module(createdAtStart = true) {
single { Dimension(23,3) }
}
))

val output = Output(FileOutputStream(saveFile))
kryo.writeClassAndObject(output, koin)
output.close()

// the downstream bits here do not matter, because it fails already while saving

println("restoring...")
val input = Input(FileInputStream(saveFile));
val restored = kryo.readClassAndObject(input) as Koin

val result = restored.get<Dimension>()
println("result is ${result}")
}


4 changes: 2 additions & 2 deletions src/main/java/KryoLambda.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ typealias Builder<T> = String.(Int) -> T

fun main() {
val saveFile = File("file.bin")
val kryo = buildKryo()
val kryo = buildLambdaKryo()

val smthg: Builder<Dimension> = { it ->
println(this)
Expand All @@ -33,7 +33,7 @@ fun main() {
}


private fun buildKryo(): Kryo {
private fun buildLambdaKryo(): Kryo {
val kryo = Kryo()

kryo.setReferences(true)
Expand Down

0 comments on commit b8152c6

Please sign in to comment.