Skip to content

Commit

Permalink
[FLINK-11865] Extract compileCbf from TraversableSerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
Igal Shilman authored and aljoscha committed Mar 12, 2019
1 parent 6f66e23 commit 3938c10
Showing 1 changed file with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
package org.apache.flink.api.scala.typeutils

import java.io.{ObjectInputStream, ObjectOutputStream}
import java.io.ObjectInputStream

import org.apache.flink.annotation.Internal
import org.apache.flink.api.common.typeutils._
Expand All @@ -44,17 +44,10 @@ class TraversableSerializer[T <: TraversableOnce[E], E](
protected def legacyCbfCode: String = null

def compileCbf(code: String): CanBuildFrom[T, E, T] = {

import scala.reflect.runtime.universe._
import scala.tools.reflect.ToolBox

val tb = runtimeMirror(Thread.currentThread().getContextClassLoader).mkToolBox()
val tree = tb.parse(code)
val compiled = tb.compile(tree)
val cbf = compiled()
cbf.asInstanceOf[CanBuildFrom[T, E, T]]
val cl = Thread.currentThread().getContextClassLoader
TraversableSerializer.compileCbf(cl, code)
}

override def duplicate = {
val duplicateElementSerializer = elementSerializer.duplicate()
if (duplicateElementSerializer eq elementSerializer) {
Expand Down Expand Up @@ -173,3 +166,22 @@ class TraversableSerializer[T <: TraversableOnce[E], E](
new TraversableSerializerSnapshot[T, E](this)
}
}

object TraversableSerializer {

def compileCbf[T, E](classLoader: ClassLoader, code: String): CanBuildFrom[T, E, T] = {
compileCbfInternal(classLoader, code)
}

private def compileCbfInternal[T, E](classLoader: ClassLoader, code: String): CanBuildFrom[T, E, T] = {
import scala.reflect.runtime.universe._
import scala.tools.reflect.ToolBox

val tb = runtimeMirror(classLoader).mkToolBox()
val tree = tb.parse(code)
val compiled = tb.compile(tree)
val cbf = compiled()
cbf.asInstanceOf[CanBuildFrom[T, E, T]]
}

}

0 comments on commit 3938c10

Please sign in to comment.