java reflection during macro

2011-12-08 Thread Razvan Rotaru
Hi,

I'm trying to write some macros for java object instanciation. Here's
the code:


(defn- gen-object-method [my-class id option value]
   (let [method (some (java-methods option) (map #(.getName %)
(.getMethods my-class)))]
 (when (not method)
   (throw (new java.lang.IllegalArgumentException (str "A
method for " option " was not found in class " component-class
 (list (symbol (str "." method)) id value)))


(defmacro gen-java-code [my-class &body]

(gen-object-method myclass ~id arg val)
)

Function gen-object-method is called from a macro (that tries to
generate java code). My problem is that my-class is a symbol and not a
java class, so that (.getMethod ) fails with

No matching field found: getMethods for class clojure.lang.Symbol
  [Thrown class java.lang.IllegalArgumentException]


How can I check in a macro that a particular java method exists for a
class or not?

Thanks,
Razvan

-- 
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: java reflection during macro

2011-12-08 Thread Kevin Downey
you can use all the things can use outside of macros:

resolve

Class/forName

etc

On Thu, Dec 8, 2011 at 1:06 PM, Razvan Rotaru  wrote:
> Hi,
>
> I'm trying to write some macros for java object instanciation. Here's
> the code:
>
>
> (defn- gen-object-method [my-class id option value]
>       (let [method (some (java-methods option) (map #(.getName %)
> (.getMethods my-class)))]
>         (when (not method)
>           (throw (new java.lang.IllegalArgumentException (str "A
> method for " option " was not found in class " component-class
>         (list (symbol (str "." method)) id value)))
>
>
> (defmacro gen-java-code [my-class &body]
> 
> (gen-object-method myclass ~id arg val)
> )
>
> Function gen-object-method is called from a macro (that tries to
> generate java code). My problem is that my-class is a symbol and not a
> java class, so that (.getMethod ) fails with
>
> No matching field found: getMethods for class clojure.lang.Symbol
>  [Thrown class java.lang.IllegalArgumentException]
>
>
> How can I check in a macro that a particular java method exists for a
> class or not?
>
> Thanks,
> Razvan
>
> --
> 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



-- 
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: java reflection during macro

2011-12-08 Thread Stuart Sierra
Not sure if it helps, but here's my example of using reflection in a macro:

http://stuartsierra.com/2010/12/16/single-abstract-method-macro

-S

-- 
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: java reflection during macro

2011-12-09 Thread Razvan Rotaru
Thanks. I was missing the call to resolve.

(let [klass (resolve c)]
)

With it it works.

Razvan

On Dec 8, 11:39 pm, Stuart Sierra  wrote:
> Not sure if it helps, but here's my example of using reflection in a macro:
>
> http://stuartsierra.com/2010/12/16/single-abstract-method-macro
>
> -S

-- 
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