Possible Duplicate:
How to analyze Java source files with Clojure
What tools are available in java/clojure that will allow the parsing of a java source file to make sense of the java code structure?
The motivation for this is for code generation..... say if I had a string that contained java source code
(def java-str
"/**
* @param r the modulus of the complex number to create
* @param theta the argument of the complex number to create
* @return <code>r·e<sup>i·theta</sup></code>
* @throws MathIllegalArgumentException if r is negative
* @since 1.1
*/
public static Complex polar2Complex(double r, double theta) {
if (r < 0) {
throw new MathIllegalArgumentException(
LocalizedFormats.NEGATIVE_COMPLEX_MODULE, r);
}
return new Complex(r * FastMath.cos(theta), r * FastMath.sin(theta));
}
")
Thengen-clj-fn
can be written to generate a clojure string of a function definition that is aware of the java methods, param name, type and order:
(gen-clj-fn java-str)
;;=> "(defn polar-to-complex [#^Double r #^Double theta]
;; (Complex/polar2Complex r theta))"
Ideally, it would be good to get r
and theta
from clojure.reflect
but I'm not sure if it is possible.... I think it only gives type signatures but not the names themselves.