Skip to content

Commit fffec56

Browse files
committed
Merge pull request #159 from twitter/asjava
Support AsJava/AsScala
2 parents 16ef6db + 6c42f05 commit fffec56

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

chill-scala/src/main/scala/com/twitter/chill/ScalaKryoInstantiator.scala

+15
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ import scala.util.matching.Regex
4343
import com.twitter.chill.java.PackageRegistrar
4444
import _root_.java.io.Serializable
4545

46+
import scala.collection.JavaConverters._
47+
4648
/** This class has a no-arg constructor, suitable for use with reflection instantiation
4749
* It has no registered serializers, just the standard Kryo configured for Kryo.
4850
*/
@@ -87,6 +89,19 @@ class ScalaKryoInstantiator extends EmptyScalaKryoInstantiator {
8789

8890
class ScalaCollectionsRegistrar extends IKryoRegistrar {
8991
def apply(newK: Kryo) {
92+
// for binary compat this is here, but could be moved to RichKryo
93+
def useField[T](cls: Class[T]) {
94+
val fs = new com.esotericsoftware.kryo.serializers.FieldSerializer(newK, cls)
95+
fs.setIgnoreSyntheticFields(false) // scala generates a lot of these attributes
96+
newK.register(cls, fs)
97+
}
98+
// The wrappers are private classes:
99+
useField(List(1, 2, 3).asJava.getClass)
100+
useField(List(1, 2, 3).iterator.asJava.getClass)
101+
useField(Map(1 -> 2, 4 -> 3).asJava.getClass)
102+
useField(new _root_.java.util.ArrayList().asScala.getClass)
103+
useField(new _root_.java.util.HashMap().asScala.getClass)
104+
90105
/*
91106
* Note that subclass-based use: addDefaultSerializers, else: register
92107
* You should go from MOST specific, to least to specific when using

chill-scala/src/test/scala/com/twitter/chill/KryoSpec.scala

+6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import scala.collection.mutable.{ArrayBuffer => MArrayBuffer, HashMap => MHashMa
2323
import _root_.java.util.PriorityQueue
2424
import _root_.java.util.Locale
2525
import scala.collection.mutable
26+
import scala.collection.JavaConverters._
27+
2628
/*
2729
* This is just a test case for Kryo to deal with. It should
2830
* be outside KryoSpec, otherwise the enclosing class, KryoSpec
@@ -85,6 +87,10 @@ class KryoSpec extends Specification with BaseProperties {
8587
Vector(1,2,3,4,5),
8688
TestValMap(null),
8789
Some("junk"),
90+
List(1, 2, 3).asJava,
91+
Map("hey" -> 1, "you" -> 2).asJava,
92+
new _root_.java.util.ArrayList(Seq(1, 2, 3).asJava).asScala,
93+
new _root_.java.util.HashMap[Int,Int](Map(1 -> 2, 3 -> 4).asJava).asScala,
8894
(),
8995
'hai)
9096
.asInstanceOf[List[AnyRef]]

0 commit comments

Comments
 (0)