In Clojure, given a class name as a string, I need to create a new instance of the class. In other words, how would I implement new-instance-from-class-name in
(def my-class-name "org.myorg.pkg.Foo")
; calls constructor of org.myorg.pkg.Foo with arguments 1, 2 and 3
(new-instance-from-class-name my-class-name 1 2 3)
I am looking for a solution more elegant than
- calling the Java newInstance method on a constructor from the class
- using eval, load-string, …
In practice, I will be using it on classes created using defrecord. So if there is any special syntax for that scenario, I would be quite interested.
There are two good ways to do this. Which is best depends on the specific circumstance.
The first is reflection:
That’s like calling
(new Integer "16")…include any other ctor arguments you need in the to-array vector. This is easy, but slower at runtime than usingnewwith sufficient type hints.The second option is as fast as possible, but a bit more complicated, and uses
eval:(defn make-factory [classname & types] (let [args (map #(with-meta (symbol (str "x" %2)) {:tag %1}) types (range))] (eval `(fn [~@args] (new ~(symbol classname) ~@args))))) (def int-factory (make-factory "Integer" 'String)) (int-factory "42")The key point is to eval code that defines an anonymous function, as
make-factorydoes. This is slow — slower than the reflection example above, so only do it as infrequently as possible such as once per class. But having done that you have a regular Clojure function that you can store somewhere, in a var likeint-factoryin this example, or in a hash-map or vector depending on how you’ll be using it. Regardless, this factory function will run at full compiled speed, can be inlined by HotSpot, etc. and will always run much faster than the reflection example.When you’re specifically dealing with classes generated by
deftypeordefrecord, you can skip the type list since those classes always have exactly two ctors each with different arities. This allows something like:(defn record-factory [recordname] (let [recordclass ^Class (resolve (symbol recordname)) max-arg-count (apply max (map #(count (.getParameterTypes %)) (.getConstructors recordclass))) args (map #(symbol (str "x" %)) (range (- max-arg-count 2)))] (eval `(fn [~@args] (new ~(symbol recordname) ~@args))))) (defrecord ExampleRecord [a b c]) (def example-record-factory (record-factory "ExampleRecord")) (example-record-factory "F." "Scott" 'Fitzgerald)