Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 泛型类型信息的序列化scala?_Java_Scala_Generics_Serialization - Fatal编程技术网

Java 泛型类型信息的序列化scala?

Java 泛型类型信息的序列化scala?,java,scala,generics,serialization,Java,Scala,Generics,Serialization,我正在scala中构建一个DSL,其中包含许多表达式和运算符,我想了解它们自己的类型。例如: import scala.reflect.runtime.universe._ trait Expression[T] { def evaluate: T def evalType: scala.reflect.runtime.universe.Type // represents T } 我想将类型信息用于多种用途(例如,打印表达式类型、按结果类型筛选表达式,以及将表达式集合[u

我正在scala中构建一个DSL,其中包含许多表达式和运算符,我想了解它们自己的类型。例如:

import scala.reflect.runtime.universe._

trait Expression[T] {
    def evaluate: T
    def evalType: scala.reflect.runtime.universe.Type  // represents T
}
我想将类型信息用于多种用途(例如,打印表达式类型、按结果类型筛选表达式,以及将表达式集合[u]强制转换为某种类型或另一种类型)。我可以这样做(如下),但似乎不能同时保留泛型类型信息和类型信息可序列化的属性

我可以让类知道它们的类型参数,如下所示:

import scala.reflect.runtime.universe._

case class TypeAware[T:TypeTag](c:Type=null) {
  val typ:Type = if (c==null) typeOf[T] else c
}  

class RelectionUtilTest {

  @Test
  def serializeTypeTest(): Unit = {
    val obj = TypeAware[Seq[Int]]()
    println(obj.typ) // Seq[Int]
    val copy = SerializationUtil.copyBySerialization(obj)  // via ObjectOutputStream -> ObjectInputStream
    println(copy.typ)
  }
}
…但scala的类型字段不可序列化

或者我可以让类知道T的类

case class ClassAware[T:TypeTag](c:Class[_]=null) {
  val clz:Class[_] = if (c==null) runtimeMirror(this.getClass.getClassLoader).runtimeClass(typeOf[T].typeSymbol.asClass) else c
}

class RelectionUtilTest {

  @Test
  def serializeTypeTest(): Unit = {
    val obj = ClassAware[Seq[Int]]()
    println(obj.typ) // Seq[_]
    val copy = SerializationUtil.copyBySerialization(obj)
    println(copy.typ)
  }
}
但是java类[1;]的类型被删除:-(

那么,如何在scala中序列化泛型类型信息呢

更新:序列化Java的ParameterizedType似乎是一个解决方案,但是