Skip to content

Commit

Permalink
Merge pull request #634 from k163377/fix-#295
Browse files Browse the repository at this point in the history
Fix ReflectionCache to be serializable
  • Loading branch information
k163377 authored Feb 26, 2023
2 parents 0f981bb + c6ee34c commit 7493f7d
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@ import com.fasterxml.jackson.databind.introspect.AnnotatedMember
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod
import com.fasterxml.jackson.databind.introspect.AnnotatedWithParams
import com.fasterxml.jackson.databind.util.LRUMap
import java.io.Serializable
import java.lang.reflect.Constructor
import java.lang.reflect.Executable
import java.lang.reflect.Method
import kotlin.reflect.KFunction
import kotlin.reflect.jvm.kotlinFunction

internal class ReflectionCache(reflectionCacheSize: Int) {
internal class ReflectionCache(reflectionCacheSize: Int) : Serializable {
companion object {
// Increment is required when properties that use LRUMap are changed.
private const val serialVersionUID = 1L
}

sealed class BooleanTriState(val value: Boolean?) {
class True : BooleanTriState(true)
class False : BooleanTriState(false)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.fasterxml.jackson.module.kotlin

import junit.framework.TestCase
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.ObjectInputStream
import java.io.ObjectOutputStream

fun jdkSerialize(o: Any): ByteArray {
val bytes = ByteArrayOutputStream(1000)
val obOut = ObjectOutputStream(bytes)
obOut.writeObject(o)
obOut.close()
return bytes.toByteArray()
}

fun <T> jdkDeserialize(raw: ByteArray): T? {
val objIn = ObjectInputStream(ByteArrayInputStream(raw))
return try {
@Suppress("UNCHECKED_CAST")
objIn.readObject() as T
} catch (e: ClassNotFoundException) {
TestCase.fail("Missing class: " + e.message)
null
} finally {
objIn.close()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
import kotlin.test.assertNotNull

class KotlinModuleTest {
/**
Expand Down Expand Up @@ -103,4 +104,27 @@ class KotlinModuleTest {

assertTrue(module.strictNullChecks)
}

@Test
fun jdkSerializabilityTest() {
val module = KotlinModule.Builder().apply {
withReflectionCacheSize(123)
enable(NullToEmptyCollection)
enable(NullToEmptyMap)
enable(NullIsSameAsDefault)
enable(SingletonSupport)
enable(StrictNullChecks)
}.build()

val serialized = jdkSerialize(module)
val deserialized = jdkDeserialize<KotlinModule>(serialized)

assertNotNull(deserialized)
assertEquals(123, deserialized.reflectionCacheSize)
assertTrue(deserialized.nullToEmptyCollection)
assertTrue(deserialized.nullToEmptyMap)
assertTrue(deserialized.nullIsSameAsDefault)
assertEquals(CANONICALIZE, deserialized.singletonSupport)
assertTrue(deserialized.strictNullChecks)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.fasterxml.jackson.module.kotlin

import org.junit.Test
import kotlin.test.assertNotNull

class ReflectionCacheTest {
@Test
fun serializeEmptyCache() {
val cache = ReflectionCache(100)
val serialized = jdkSerialize(cache)
val deserialized = jdkDeserialize<ReflectionCache>(serialized)

assertNotNull(deserialized)
// Deserialized instance also do not raise exceptions
deserialized.kotlinFromJava(ReflectionCacheTest::class.java.getDeclaredMethod("serializeEmptyCache"))
}

@Test
fun serializeNotEmptyCache() {
val method = ReflectionCacheTest::class.java.getDeclaredMethod("serializeNotEmptyCache")

val cache = ReflectionCache(100).apply { kotlinFromJava(method) }
val serialized = jdkSerialize(cache)
val deserialized = jdkDeserialize<ReflectionCache>(serialized)

assertNotNull(deserialized)
// Deserialized instance also do not raise exceptions
deserialized.kotlinFromJava(method)
}
}

0 comments on commit 7493f7d

Please sign in to comment.