diff --git a/src/main/java/KryoBeanDefinition.kt b/src/main/java/KryoBeanDefinition.kt index fa92234..2c1f868 100644 --- a/src/main/java/KryoBeanDefinition.kt +++ b/src/main/java/KryoBeanDefinition.kt @@ -1,4 +1,5 @@ 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 @@ -6,13 +7,16 @@ 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") @@ -20,7 +24,7 @@ fun main() { val smthg: Definition = { it -> Dimension() } - val beanDef = BeanDefinition( + val beanDef = BeanDefinition( StringQualifier("foo"), Dimension::class, definition = smthg, @@ -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(kryo, BeanDefinition::class.java) { +// override fun create(kryo: Kryo, input: Input, type: Class<*>?): BeanDefinition { +// return BeanDefinition() +// } +// }) + kryo.register(BeanDefinition::class.java, object : Serializer>() { + + 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>? + ): 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> + + return BeanDefinition(scopeQualifier, primaryType, qualifier, definition, kind, secondaryTypes) + } + }) + + + kryo.register(TypeQualifier::class.java, object : Serializer() { + + 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? + ): TypeQualifier { + + val type = kryo!!.readObjectOrNull(input, KClass::class.java) + return TypeQualifier(type) + } + }) + + kryo.register(StringQualifier::class.java, object : Serializer() { + + override fun write(kryo: Kryo?, output: Output?, qualifier: StringQualifier) { + kryo!!.writeObject(output, qualifier.value) + } + + override fun read( + kryo: Kryo?, + input: Input?, + type: Class? + ): StringQualifier { + + val value = kryo!!.readObjectOrNull(input, String::class.java) + return StringQualifier(value) + } + }) + + kryo.register(kotlin.jvm.internal.ClassReference::class.java, object : Serializer() { + + 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? + ): kotlin.jvm.internal.ClassReference { + + val value = kryo!!.readClassAndObject(input) as java.lang.Class<*> + return kotlin.jvm.internal.ClassReference(value) + } + }) + return kryo } \ No newline at end of file diff --git a/src/main/java/KryoDefinition.kt b/src/main/java/KryoDefinition.kt index 8638b90..61dfca4 100644 --- a/src/main/java/KryoDefinition.kt +++ b/src/main/java/KryoDefinition.kt @@ -14,7 +14,7 @@ import java.io.FileOutputStream fun main() { val saveFile = File("file.bin") - val kryo = buildKryo() + val kryo = buildKryoDefinition() val smthg: Definition = { it -> Dimension() } @@ -35,7 +35,7 @@ fun main() { } -private fun buildKryo(): Kryo { +private fun buildKryoDefinition(): Kryo { val kryo = Kryo() diff --git a/src/main/java/KryoInstanceFactory.kt b/src/main/java/KryoInstanceFactory.kt index 6f8459e..f2595da 100644 --- a/src/main/java/KryoInstanceFactory.kt +++ b/src/main/java/KryoInstanceFactory.kt @@ -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 -} \ No newline at end of file diff --git a/src/main/java/KryoKoin.kt b/src/main/java/KryoKoin.kt new file mode 100644 index 0000000..850ba1d --- /dev/null +++ b/src/main/java/KryoKoin.kt @@ -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() + println("result is ${result}") +} + + diff --git a/src/main/java/KryoLambda.kt b/src/main/java/KryoLambda.kt index c440d62..28186f3 100644 --- a/src/main/java/KryoLambda.kt +++ b/src/main/java/KryoLambda.kt @@ -12,7 +12,7 @@ typealias Builder = String.(Int) -> T fun main() { val saveFile = File("file.bin") - val kryo = buildKryo() + val kryo = buildLambdaKryo() val smthg: Builder = { it -> println(this) @@ -33,7 +33,7 @@ fun main() { } -private fun buildKryo(): Kryo { +private fun buildLambdaKryo(): Kryo { val kryo = Kryo() kryo.setReferences(true)