Skip to content

Instantly share code, notes, and snippets.

@yamashiro
Created June 29, 2012 07:35
Show Gist options
  • Save yamashiro/3016492 to your computer and use it in GitHub Desktop.
Save yamashiro/3016492 to your computer and use it in GitHub Desktop.
package study
import org.specs2.mutable.Specification
import scalaz.Identity
import org.sisioh.dddbase.core.Entity
import java.io._
class Hoge extends Specification {
"Serial " should {
"aaa" in {
val bar = new Bar(Identity(1), "aaa")
val bos = new ByteArrayOutputStream()
val oos = new ObjectOutputStream(bos)
oos.writeObject(bar)
val bis = new ByteArrayInputStream(bos.toByteArray)
val ois = new ObjectInputStream(bis)
val readBar = ois.readObject().asInstanceOf[Bar]
readBar.identity must_== Identity(1)
readBar.str must_== "aaa"
}
}
}
class Bar(@transient val identity:Identity[Int], val str:String) extends Entity[Int] with Serializable {
@throws(classOf[IOException])
private def writeObject(stream:java.io.ObjectOutputStream) : Unit = {
stream.defaultWriteObject()
stream.writeInt(identity.value)
}
@throws(classOf[IOException])
private def readObject(stream:java.io.ObjectInputStream) : Unit = {
stream.defaultReadObject()
val hoge = stream.readInt()
val field = getClass.getDeclaredField("identity")
field.setAccessible(true)
println("hoge [" + hoge + "]")
field.set(this, Identity(hoge))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment