zcaudate
zcaudate

Reputation: 14258

how to get the constructor of a class that contains a primitive in clojure?

In order to access the constructor of a class that has typed arguments, I can do:

> (.newInstance (.getConstructor java.util.Date (into-array java.lang.Class []))
          (object-array []))
;=> #inst "2013-04-27T04:00:55.498-00:00"

> (.newInstance (.getConstructor java.util.Date (into-array java.lang.Class [java.lang.String]))
          (object-array ["27 Apr 2013"]))
;=> #inst "2013-04-26T14:00:00.000-00:00"

but java.util.Date also takes a long as an argument and I wish to write something like

> (.newInstance (.getConstructor java.util.Date (into-array java.lang.Class [long]))
          (object-array [0]))

how can this be done?

Upvotes: 0

Views: 248

Answers (1)

sw1nn
sw1nn

Reputation: 7328

The object wrappers for primitives all have a static TYPE field that can be used in these cases.

So for your specific example you need...

(.newInstance 
  (.getConstructor java.util.Date 
     (into-array java.lang.Class [java.lang.Long/TYPE]))
        (object-array [0]))
;=> #inst "1970-01-01T00:00:00.000-00:00"

Upvotes: 1

Related Questions