我使用chill-scala进行序列化。我想序列化一个存储类型参数信息的泛型类,所以我将TypeTag[T]
作为隐式参数传递:
import com.twitter.chill.{Output, ScalaKryoInstantiator}
import scala.reflect.runtime.universe.TypeTag
object TagTest extends App {
class A[T](implicit val t: TypeTag[T]) {}
val kryo = (new ScalaKryoInstantiator).setRegistrationRequired(false).newKryo()
kryo.writeObject(new Output(new Array[Byte](100000)), new A[Int])
}
但由于某种原因,序列化因java.util.ConcurrentModificationException
而失败:
Exception in thread "main" com.esotericsoftware.kryo.KryoException: java.util.ConcurrentModificationException
Serialization trace:
classes (sun.misc.Launcher$AppClassLoader)
classLoader (scala.reflect.runtime.JavaMirrors$JavaMirror)
$outer (scala.reflect.runtime.JavaMirrors$JavaMirror$$anon$1)
currentOwner (scala.reflect.internal.Trees$TreeTypeSubstituter)
EmptyTreeTypeSubstituter (scala.reflect.runtime.JavaUniverse)
$outer (scala.reflect.api.TypeTags$PredefTypeTag)
t (TagTest$A)
at com.esotericsoftware.kryo.serializers.ObjectField.write(ObjectField.java:101)
at com.esotericsoftware.kryo.serializers.FieldSerializer.write(FieldSerializer.java:518)
at com.esotericsoftware.kryo.Kryo.writeObject(Kryo.java:552)
如果我尝试序列化typeOf[Int]
对象,则序列化失败且出现相同的错误,这表示TypeTag
无法序列化。
是什么原因?如何序列化TypeTag
?