I need the ability to create one of a number of java classes which have constructors taking a known list of parameters. There are a large enough number of classes that having a set of conditions testing for each class name would be unmanageable. The classes are from a third-party jar file so I cannot modify them either.
The trouble is that new is special form and does not evaluate it's first parameter so if I write an expression like
(new (Class/forName klassname) param1 param2) it fails because (Class/forName klassname) is treated as the class name rather than being evaluated first.
The closest I've got so far is to write the following macro
(defmacro make-proxy
"Build proxy class from name and map"
[klassname params] (list 'new (Class/forName klassname) '(:hostname params) '(:port params)))
This gets me some of the way, I can now write
(make-proxy "com.example.foo" {:hostname "127.0.0.1 :port 9559})
but it only works for constant strings since the first parameter is evaluated when the macro is expanded.
Can anyone suggest a solution?
thanks
Dave