I have this code which appears over and over in my project for each type of class we serialize to Avro
class FooKryoRegistrator extends KryoRegistrator {
override def registerClasses(kryo: Kryo) {
kryo.register(classOf[Foo], SpecificRecordBinarySerializer(classTag[Foo]))
kryo.register(classOf[GenericData.Array[_]], new SpecificInstanceCollectionSerializer(classOf[java.util.ArrayList[_]]))
}
}
Now instead of having to write this Registrator class for each and every entity (Foo, Bar, Baz.....) in my project, I thought it will be better to make this generic. So I did
class GenericKryoRegistrator[T] extends KryoRegistrator {
override def registerClasses(kryo: Kryo) {
kryo.register(classOf[T], SpecificRecordBinarySerializer(classTag[T]))
kryo.register(classOf[GenericData.Array[_]], new SpecificInstanceCollectionSerializer(classOf[java.util.ArrayList[_]]))
}
}
But I get a compile time error
class type required but T found
I googled on this error and found a recommendation that I should use ClassTag instead of class. So I changed the implementation to
class GenericKryoRegistrator[T] extends KryoRegistrator {
override def registerClasses(kryo: Kryo) {
kryo.register(classTag[T].runtimeClass, SpecificRecordBinarySerializer(classTag[T]))
kryo.register(classOf[GenericData.Array[_]], new SpecificInstanceCollectionSerializer(classOf[java.util.ArrayList[_]]))
}
}
but now it says
No ClassTag available for T