javafx - Clojure: Calling .invoke without knowing arg types at compile time -
i'm trying figure out way .invoke()
methods of single arg, without knowing type of arg @ runtime.
for example, passing property name following function, i'd set property's value. it's hard-coded double/type
, function should allowed take type of value
arg.
the property names, , therefore type of arg value
not known until runtime, appears method requires array of primitives, @ least number types.
(defn set-prop-val! "sets property value value. uses reflection." [obj prop value] (let [methodname (str "set" prop)] (.. obj getclass (getmethod methodname (into-array [double/type])) (invoke obj (into-array [value]) )))) (set-prop-val! (button.) "translatex" 13.99)
in above function, trying instead (getmethod methodname (into-array [(class value)]))
doesn't work, , nosuchmethodexception javafx.scene.control.button.settranslatex(java.lang.double)
because wants double
not double
.
what i'd numbers primitive type @ runtime, attempts @ calling static method instance have failed, eg (. (class 19.5) type)
(in case illegalargumentexception no matching field found: type class java.lang.class
)
it seems double/type
works, though (= (class 19.5) double)
returns true
.
so few questions come out of this:
- how call static method, such
type
instance? - is there use
double-array
here? me array carry values.invoke
, doesn't appear necessary anyway, notclass[double]
array needed.getmethod
. - should use
clojure.reflect
instead?
i found use clojure.lang.reflector/invokeinstancemethod
need.
Comments
Post a Comment