Re: Constructing Java Interop calls
On Thu, Oct 29, 2009 at 1:38 PM, Meikel Brandmeyer m...@kotka.de wrote: All good here, but, if I do the eval variation, user= (eval (list (symbol .setFileSelectionMode) jfc 1)) Another example which shows that eval is not worth the trouble. It is better to use reflection. You cannot embed the JFileChooser as a object into the code. You have to construct it in your eval. (eval ` (let [chooser# (JFileChooser.)] (~(symbol .setFileSelectionMode) chooser# 1) chosser#)). Thanks for all the people that helped. But I just wonder, why? I mean, why one cannot embed an object in the eval code? Tiago --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Constructing Java Interop calls
eval calls read for somethings. 2009/10/30 Tiago Antão tiagoan...@gmail.com: On Thu, Oct 29, 2009 at 1:38 PM, Meikel Brandmeyer m...@kotka.de wrote: All good here, but, if I do the eval variation, user= (eval (list (symbol .setFileSelectionMode) jfc 1)) Another example which shows that eval is not worth the trouble. It is better to use reflection. You cannot embed the JFileChooser as a object into the code. You have to construct it in your eval. (eval ` (let [chooser# (JFileChooser.)] (~(symbol .setFileSelectionMode) chooser# 1) chosser#)). Thanks for all the people that helped. But I just wonder, why? I mean, why one cannot embed an object in the eval code? Tiago -- And what is good, Phaedrus, And what is not good— Need we ask anyone to tell us these things? --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Constructing Java Interop calls
On Wed, Oct 28, 2009 at 10:15 PM, Alex Osborne a...@meshy.org wrote: Tiago Antão wrote: Again, the point here is to be able to construct method names (full call signatures, really) on runtime. I am lost. As in newbie clueless :( As others have suggested you need to use either Java's reflection or Clojure's eval. Not quite -- this works as long as the Bla part is a constant: (defmacro setProperty [field obj value] (let [cct (symbol (.concat .set (str field)))] `(~cct ~obj ~value))) user= (macroexpand-1 '(setProperty Bla x 1)) (.setBla x 1) --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Constructing Java Interop calls
On Thu, Oct 29, 2009 at 2:15 AM, Alex Osborne a...@meshy.org wrote: Using eval (which will also work for dynamically calling Clojure functions): (let [obj some string fname .substring] (eval (list (symbol fname) obj 2))) Thanks a lot. I was trying to avoid reflection (ie, looking for a clojure idiom), especially to get an understanding on what are clojure limitations. The eval form still shows some problems, if I do this preparation: (import javax.swing.JFileChooser) (def jfc (new JFileChooser)) And then do: user= (.setFileSelectionMode jfc 1) nil All good here, but, if I do the eval variation, user= (eval (list (symbol .setFileSelectionMode) jfc 1)) I get: #CompilerException java.lang.RuntimeException: Can't embed object in code, maybe print-dup not defined In theory it should work? What basic mistake am I doing here? BTW: user= (println (list (symbol .setFileSelectionMode) jfc 1)) (.setFileSelectionMode #JFileChooser javax.swing.JFileChooser[,0,0,0x0,invalid,layout=ja...] 1) So the list for the eval looks good... Many thanks, Tiago -- The hottest places in hell are reserved for those who, in times of moral crisis, maintain a neutrality. - Dante --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Constructing Java Interop calls
Hi, On Oct 29, 2:07 pm, Tiago Antão tiagoan...@gmail.com wrote: The eval form still shows some problems, if I do this preparation: (import javax.swing.JFileChooser) (def jfc (new JFileChooser)) And then do: user= (.setFileSelectionMode jfc 1) nil All good here, but, if I do the eval variation, user= (eval (list (symbol .setFileSelectionMode) jfc 1)) Another example which shows that eval is not worth the trouble. It is better to use reflection. You cannot embed the JFileChooser as a object into the code. You have to construct it in your eval. (eval ` (let [chooser# (JFileChooser.)] (~(symbol .setFileSelectionMode) chooser# 1) chosser#)). Hopes this helps. Sincerely Meikel --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Constructing Java Interop calls
user= ((eval `(fn [x#] (~(symbol .setFileSelectionMode) x# 1))) jfc) nil user= On Thu, Oct 29, 2009 at 6:38 AM, Meikel Brandmeyer m...@kotka.de wrote: Hi, On Oct 29, 2:07 pm, Tiago Antão tiagoan...@gmail.com wrote: The eval form still shows some problems, if I do this preparation: (import javax.swing.JFileChooser) (def jfc (new JFileChooser)) And then do: user= (.setFileSelectionMode jfc 1) nil All good here, but, if I do the eval variation, user= (eval (list (symbol .setFileSelectionMode) jfc 1)) Another example which shows that eval is not worth the trouble. It is better to use reflection. You cannot embed the JFileChooser as a object into the code. You have to construct it in your eval. (eval ` (let [chooser# (JFileChooser.)] (~(symbol .setFileSelectionMode) chooser# 1) chosser#)). Hopes this helps. Sincerely Meikel -- And what is good, Phaedrus, And what is not good— Need we ask anyone to tell us these things? --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Constructing Java Interop calls
Hi, Sorry for the newbie question, but I am trying to understand how to construct and call java dynamically from clojure. As an example, imagine that there is a bean property called Bla and one wants to set Bla to 1 on object x, which has that property. So, the objective would be to construct, the following java equivalent x.setBla(1); I defined a macro called setProperty like this: (setProperty Bla x 1) And the definition is (very wrong): (defmacro setProperty [field obj value] `(let [cct# (symbol (.concat .set ~field))] (cct# ~obj ~value) ) ) If I do (println (setProperty Bla x 1)) I get 1 (the setBla is a void, so I should not get 1). bla is never set to 1, by the way :( Note that I am not trying to sort the particular case of beans. I am trying to understand the general mechanism to construct symbols (java interop) and execute them with a set of parameters over a java object. Thanks a lot and sorry for the newbie question, T -- The hottest places in hell are reserved for those who, in times of moral crisis, maintain a neutrality. - Dante --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Constructing Java Interop calls
Is there a reason you don't want to use doto? http://clojure.org/java_interop#toc15 ie (doto Bla (.setProperty x 1)) 2009/10/28 Tiago Antão tiagoan...@gmail.com: Hi, Sorry for the newbie question, but I am trying to understand how to construct and call java dynamically from clojure. As an example, imagine that there is a bean property called Bla and one wants to set Bla to 1 on object x, which has that property. So, the objective would be to construct, the following java equivalent x.setBla(1); I defined a macro called setProperty like this: (setProperty Bla x 1) And the definition is (very wrong): (defmacro setProperty [field obj value] `(let [cct# (symbol (.concat .set ~field))] (cct# ~obj ~value) ) ) If I do (println (setProperty Bla x 1)) I get 1 (the setBla is a void, so I should not get 1). bla is never set to 1, by the way :( Note that I am not trying to sort the particular case of beans. I am trying to understand the general mechanism to construct symbols (java interop) and execute them with a set of parameters over a java object. Thanks a lot and sorry for the newbie question, T -- The hottest places in hell are reserved for those who, in times of moral crisis, maintain a neutrality. - Dante -- Omnem crede diem tibi diluxisse supremum. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Constructing Java Interop calls
On Wed, Oct 28, 2009 at 7:34 PM, Wilson MacGyver wmacgy...@gmail.com wrote: Is there a reason you don't want to use doto? http://clojure.org/java_interop#toc15 ie (doto Bla (.setProperty x 1)) I really want to do something different: (def x (new StringBuffer )) (doto x (.setLength 2)) But my point is to be able to construct the method name in runtime. Using doto, I still have the same problem: (defmacro setProperty [field obj value] `(let [cct# (symbol (.concat .set ~field))] (doto ~obj (cct# ~value)) ) ) If I do: (def sb (new StringBuffer )) (setProperty Length sb 2) sb I get (It should be aa). Again, the point here is to be able to construct method names (full call signatures, really) on runtime. I am lost. As in newbie clueless :( Thanks a lot, T --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Constructing Java Interop calls
2009/10/28 Tiago Antão tiagoan...@gmail.com: [...] Again, the point here is to be able to construct method names (full call signatures, really) on runtime. I am lost. As in newbie clueless :( I suspect you want reflection, but I don't know off hand how to do it. -- Michael Wood esiot...@gmail.com --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Constructing Java Interop calls
Hi, Am 28.10.2009 um 20:46 schrieb Tiago Antão: But my point is to be able to construct the method name in runtime. You'll need reflection for that. AFAIU method calls are wired in the bytecode and hence the method name must be known at compile time. Sincerely Meikel smime.p7s Description: S/MIME cryptographic signature
Re: Constructing Java Interop calls
you can always just construct the call as a string or as a datastructure and pass it through read/eval On Wed, Oct 28, 2009 at 2:04 PM, Meikel Brandmeyer m...@kotka.de wrote: Hi, Am 28.10.2009 um 20:46 schrieb Tiago Antão: But my point is to be able to construct the method name in runtime. You'll need reflection for that. AFAIU method calls are wired in the bytecode and hence the method name must be known at compile time. Sincerely Meikel -- And what is good, Phaedrus, And what is not good— Need we ask anyone to tell us these things? --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Constructing Java Interop calls
Tiago Antão wrote: Again, the point here is to be able to construct method names (full call signatures, really) on runtime. I am lost. As in newbie clueless :( As others have suggested you need to use either Java's reflection or Clojure's eval. Here's some examples: Using reflection: (let [obj some string method (.getDeclaredMethod (class obj) substring (into-array Class [Integer/TYPE]))] (.invoke method obj (to-array [2]))) = me string If you want to know more about what you can do with reflection, consult: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html Using eval (which will also work for dynamically calling Clojure functions): (let [obj some string fname .substring] (eval (list (symbol fname) obj 2))) = me string --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---