Question

I'm trying to create a scala object, kafka.utils.ZKStringSerializer in clojure. (It's in org.apache.kafka/kafka_2.10 "0.8.0")

Since I know little about scala, I don't know how to call its constructor. I tried like this:

(import 'kafka.utils.ZKStringSerializer)
(ZKStringSerializer.)                      
; or (new ZKStringSerializer)

And got an error: CompilerException java.lang.IllegalArgumentException: No matching ctor found for class kafka.utils.ZKStringSerializer

I tried using (clojure.reflect/reflect ZKStringSerializer) to see its methods but there are only some static methods. And (class ZKStringSerializer) tells me it is a class, not an instance I want.

The object is implemented like this:

object ZKStringSerializer extends ZkSerializer {

  @throws(classOf[ZkMarshallingError])
  def serialize(data : Object) : Array[Byte] = data.asInstanceOf[String].getBytes("UTF-8")

  @throws(classOf[ZkMarshallingError])
  def deserialize(bytes : Array[Byte]) : Object = {
    if (bytes == null)
      null
    else
      new String(bytes, "UTF-8")
  }
}
Was it helpful?

Solution

All scala objects are singletons in terms of java. There is no public constructor. You should use static field MODULE$ to get an instance of singleton.

I don't know clojure, but according to this page it looks like you should use this:

ZKStringSerializer$/MODULE$

Note also that the actual type name of object contains $ as last character.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top