Skip to content

Instantly share code, notes, and snippets.

@elizarov
Created October 5, 2021 06:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elizarov/91680b7bfa51892f7696357c1231ddc2 to your computer and use it in GitHub Desktop.
Save elizarov/91680b7bfa51892f7696357c1231ddc2 to your computer and use it in GitHub Desktop.
KT-46872
import java.io.*
import java.lang.NullPointerException
data class A(val s: String?) : Serializable {
fun add(b: B) {
bs.add(b)
}
// we'll add B(this) to 'bs'
private val bs = mutableSetOf<B>()
// check is B(this) is in 'bs'
fun isValid() = bs.contains(B(this))
}
data class B(val a: A) : Serializable
fun main() {
try {
val expected = A("A")
expected.add(B(expected))
val baos = ByteArrayOutputStream()
ObjectOutputStream(baos).use {
with(it) {
writeObject(expected)
flush()
}
}
val bais = ByteArrayInputStream(baos.toByteArray())
val actual = ObjectInputStream(bais).use {
it.readObject()
} as A
println(if (expected == actual) "pass" else "fail")
// BUT DID IT REALLY PASS? IS THE DESERIALIZED OBJECT VALID?
println("Is 'expected' valid? ${expected.isValid()}")
println("is 'actual' valid? ${actual.isValid()}")
} catch (npe: NullPointerException) {
println("fail")
npe.printStackTrace()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment