1

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&middot;e<sup>i&middot;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.

Community
  • 1
  • 1
zcaudate
  • 13,998
  • 7
  • 64
  • 124
  • 1
    It may be possible but not feasible. – Ankur Sep 27 '12 at 06:57
  • 2
    It's not entirely clear to me why you want to do this. What's the purpose behind this? Depending on what you are trying to do, there is probably a much better way. Parsing Java properly is hard, it requires much more than just decoding the syntax of a single method. For example you'd need to resolve the classnames in function arguments via import statements with a valid classpath etc. You'd probably have to pull in about 80% of a full Java compiler to handle all the special cases. – mikera Sep 27 '12 at 07:05
  • I'm not looking for a full java interpreter... I really have no idea how a parser works so its a good oppotunity to learn something new. What i want is the method, the parameter names and the docstring within the code. The compiled java jar file can be in memory. The motivation is for literate clojure code generation when wrapping java objects. – zcaudate Sep 27 '12 at 08:44
  • So your goal is to parse Java source files to generate clojure functions? Note that JavaDoc strings do not survive compilation, so you'll have to take source as input. – noahlz Sep 27 '12 at 09:49
  • That's the whole point of the question :) – zcaudate Sep 27 '12 at 10:34
  • Do you also also have a complied version of the Java source? If not then you will need a full compiler anyway to make executable code out of it.... – mikera Sep 27 '12 at 11:51
  • Yeah.... So the use case would be some sort of leinigen plugin.... The compiled .jar file as well as the .java files are all accessible. And then you generate the clojure wrapped methods with documentation into a normal clj file. It's useful for people like me that are slow typers. – zcaudate Sep 27 '12 at 12:18
  • Or this? http://stackoverflow.com/questions/9375626/how-to-analyze-java-source-files-with-clojure – noahlz Sep 27 '12 at 14:38

0 Answers0