File tree 4 files changed +89
-1
lines changed
main/kotlin/com/fasterxml/jackson/module/kotlin
test/kotlin/com/fasterxml/jackson/module/kotlin
4 files changed +89
-1
lines changed Original file line number Diff line number Diff line change @@ -5,13 +5,19 @@ import com.fasterxml.jackson.databind.introspect.AnnotatedMember
5
5
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod
6
6
import com.fasterxml.jackson.databind.introspect.AnnotatedWithParams
7
7
import com.fasterxml.jackson.databind.util.LRUMap
8
+ import java.io.Serializable
8
9
import java.lang.reflect.Constructor
9
10
import java.lang.reflect.Executable
10
11
import java.lang.reflect.Method
11
12
import kotlin.reflect.KFunction
12
13
import kotlin.reflect.jvm.kotlinFunction
13
14
14
- internal class ReflectionCache (reflectionCacheSize : Int ) {
15
+ internal class ReflectionCache (reflectionCacheSize : Int ) : Serializable {
16
+ companion object {
17
+ // Increment is required when properties that use LRUMap are changed.
18
+ private const val serialVersionUID = 1L
19
+ }
20
+
15
21
sealed class BooleanTriState (val value : Boolean? ) {
16
22
class True : BooleanTriState (true )
17
23
class False : BooleanTriState (false )
Original file line number Diff line number Diff line change
1
+ package com.fasterxml.jackson.module.kotlin
2
+
3
+ import junit.framework.TestCase
4
+ import java.io.ByteArrayInputStream
5
+ import java.io.ByteArrayOutputStream
6
+ import java.io.ObjectInputStream
7
+ import java.io.ObjectOutputStream
8
+
9
+ fun jdkSerialize (o : Any ): ByteArray {
10
+ val bytes = ByteArrayOutputStream (1000 )
11
+ val obOut = ObjectOutputStream (bytes)
12
+ obOut.writeObject(o)
13
+ obOut.close()
14
+ return bytes.toByteArray()
15
+ }
16
+
17
+ fun <T > jdkDeserialize (raw : ByteArray ): T ? {
18
+ val objIn = ObjectInputStream (ByteArrayInputStream (raw))
19
+ return try {
20
+ @Suppress(" UNCHECKED_CAST" )
21
+ objIn.readObject() as T
22
+ } catch (e: ClassNotFoundException ) {
23
+ TestCase .fail(" Missing class: " + e.message)
24
+ null
25
+ } finally {
26
+ objIn.close()
27
+ }
28
+ }
Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ import org.junit.Assert.assertEquals
11
11
import org.junit.Assert.assertFalse
12
12
import org.junit.Assert.assertTrue
13
13
import org.junit.Test
14
+ import kotlin.test.assertNotNull
14
15
15
16
class KotlinModuleTest {
16
17
/* *
@@ -103,4 +104,27 @@ class KotlinModuleTest {
103
104
104
105
assertTrue(module.strictNullChecks)
105
106
}
107
+
108
+ @Test
109
+ fun jdkSerializabilityTest () {
110
+ val module = KotlinModule .Builder ().apply {
111
+ withReflectionCacheSize(123 )
112
+ enable(NullToEmptyCollection )
113
+ enable(NullToEmptyMap )
114
+ enable(NullIsSameAsDefault )
115
+ enable(SingletonSupport )
116
+ enable(StrictNullChecks )
117
+ }.build()
118
+
119
+ val serialized = jdkSerialize(module)
120
+ val deserialized = jdkDeserialize<KotlinModule >(serialized)
121
+
122
+ assertNotNull(deserialized)
123
+ assertEquals(123 , deserialized.reflectionCacheSize)
124
+ assertTrue(deserialized.nullToEmptyCollection)
125
+ assertTrue(deserialized.nullToEmptyMap)
126
+ assertTrue(deserialized.nullIsSameAsDefault)
127
+ assertEquals(CANONICALIZE , deserialized.singletonSupport)
128
+ assertTrue(deserialized.strictNullChecks)
129
+ }
106
130
}
Original file line number Diff line number Diff line change
1
+ package com.fasterxml.jackson.module.kotlin
2
+
3
+ import org.junit.Test
4
+ import kotlin.test.assertNotNull
5
+
6
+ class ReflectionCacheTest {
7
+ @Test
8
+ fun serializeEmptyCache () {
9
+ val cache = ReflectionCache (100 )
10
+ val serialized = jdkSerialize(cache)
11
+ val deserialized = jdkDeserialize<ReflectionCache >(serialized)
12
+
13
+ assertNotNull(deserialized)
14
+ // Deserialized instance also do not raise exceptions
15
+ deserialized.kotlinFromJava(ReflectionCacheTest ::class .java.getDeclaredMethod(" serializeEmptyCache" ))
16
+ }
17
+
18
+ @Test
19
+ fun serializeNotEmptyCache () {
20
+ val method = ReflectionCacheTest ::class .java.getDeclaredMethod(" serializeNotEmptyCache" )
21
+
22
+ val cache = ReflectionCache (100 ).apply { kotlinFromJava(method) }
23
+ val serialized = jdkSerialize(cache)
24
+ val deserialized = jdkDeserialize<ReflectionCache >(serialized)
25
+
26
+ assertNotNull(deserialized)
27
+ // Deserialized instance also do not raise exceptions
28
+ deserialized.kotlinFromJava(method)
29
+ }
30
+ }
You can’t perform that action at this time.
0 commit comments