Hello.

This is with Clojure 1.2.

This works:

(gen-class
  :name    "com.nsn.isar.saxon.ext_funcs.ClojureReceiver"
  :extends net.sf.saxon.event.ProxyReceiver
  :exposes {nextReceiver { :get getReceiver :set setReceiver  } }
  :exposes-methods { startDocument "superStartDocument",
                     endDocument   "superEndDocument",
                     startElement  "superStartElement",
                     endElement    "superEndElement",
                     attribute     "superAttribute",
                     startContent
"superStartContent",
                     getNamePool   "superGetNamePool" } )

If I however write a function to compute the map for exposes-methods,
nothing seems to work.

I also don't understand why the method names have to be given as
symbols here and not as strings.
{ "endDocument" "superEndDocument" } would not work. It's probably
because of this in gen-class:
(if (contains? exposes-methods (symbol name))

However, not even this would work:

 :exposes-methods { (symbol "startDocument") "superStartDocument" }


I wrote this to compute me a map of name mappings for
the :exposesMethods (the definition of classMethods is of the web,
though):

(defn classMethods
  "Discovers all the public methods of a class or object by
introspection and returns their names in order."
  [x]
    (let [c (if (class? x) x (class x))]
     (distinct (sort (seq
                      (map #(.getName %1)
                       (.getMethods c)))))))

(defn makeCamelCase
  "Upper-cases the first letter of a String."
  [x]
  (str (.toUpperCase (.substring x 0 1)) (.substring x 1)))

(defn createSuperName [x] (str "super" (makeCamelCase x)))

(defn exposeMethods
  "Will compute a :exposes-methods spec for gen-class. Provide it with
a class or object and it will discover
   all public methods names and prefix them with 'super'."
  [x]
    (loop [resultMap {}
           methodNames (classMethods x)]
      (if (empty? methodNames)
        resultMap
        (let [firstMethod (first methodNames)
              restMethods (rest  methodNames)]
          (recur (assoc resultMap (symbol firstMethod)
(createSuperName firstMethod))
                 restMethods)))))

In the REPL it works correctly and gives me a map of the style that
exposes-methods should accept. But if I make a call (exposeMethods
net.sf.saxon.event.ProxyReceiver) within the gen-class macro, it does
not work.

My questions are:

1) Is this a problem of evaluation? Does my call not to exposeMethods
not get evaluated at compile time? (My assumption would have been that
in a Lisp dialect it would get expanded even at compile-time.)
2) Why does only the format { <symbol> <string> } work for the map?
What is the

I tried to read the definition of the gen-class macro, but it is
beyond me on my first week of Clojure. ;-)

Thank you in advance,
Oliver

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

Reply via email to