On Tue, Feb 28, 2012 at 10:40 AM, Bost <rostislav.svob...@gmail.com> wrote: > I have a java class with several static methods: > > package org.domain.test; > > public class My { > static public void sBar(String x, String y) > { System.out.println("sBar "+x+":"+y); } > static public void sBaz(String x, String y) > { System.out.println("sBaz "+x+":"+y); } > } > > I'd like to execute these methods from clojure. The concrete method > will be specified at runtime. > > test.clj: > (ns test) > (:import org.domain.test.My) > > ; this is what I need but it does not work: > (defn foo [f x y] > (. org.domain.test.My sBar x y) ;hard coded call of the > java static method sBar > ;(. org.domain.test.My f x y)) ;compiler error > ;(map #(. org.domain.test.My % x y) [f]) ;compiler error > ;(apply (. org.domain.test.My f x y)) ;compiler error > ;(. org.domain.test.My #(f) x y) ;compiler error > )
Static calls compile down to an "invokeStatic" at the bytecode level, which means you need the details available at compile time. That makes your only options "eval" or reflection. If you don't mind using undocumented implementation details, clojure has some nice reflection facilities included: https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/Reflector.java#L205 eval: (defn invoke-static [class f & args] (eval `(. ~class ~f ~@args))) (invoke-static org.domain.test.My 'sBar x y) Reflection: (clojure.lang.Reflector/invokeStatic org.domain.test.My "sBar" (into-array Object [x y])) --Aaron -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en