I couldn't find a way to alter Clojure's import behavior, but I did find a couple of hacks to do what I need.
First, JavaFX provides builder classes, so the cleanest way in this particular case would be to use ButtonBuilder
to create new Buttons.
Second way would be to write a simple Java class that wraps the Button
, and then from Clojure's side import that wrapping class. It's an OK solution when working with smaller number of problematic classes.
Third way would be to import at runtime, something like this (thanks to guys at #clojure for helping out with this):
(defn import-at-runtime [name]
(.importClass (the-ns *ns*)
(clojure.lang.RT/classForName name)))
(import-at-runtime "javafx.scene.control.Button")
(let [button (eval `(new ~(symbol "javafx.scene.control.Button") ~"Button Text"))
In the end, this seems like a ugly wart in Clojure's Java interop, it would be great if it could be fixed in the future.
UPDATE: There's also clojure.lang.RT/classForNameNonLoading, but unfortunately, it's not public
as of Clojure 1.6. It's easy to re-implement it in Clojure, though:
(fn [^String class-name]
(Class/forName class-name false (clojure.lang.RT/baseLoader)))
Later, the class can be instantiated with clojure.lang.Reflector/invokeConstructor
.