1

可能重复:
如何使用 Clojure 分析 Java 源文件

java/clojure 中有哪些工具可以解析 java 源文件以理解 java 代码结构?

这样做的动机是为了代码生成.....假设我有一个包含 java 源代码的字符串

(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));
}
")

然后gen-clj-fn可以编写生成函数定义的 clojure 字符串,该字符串知道 java 方法、参数名称、类型和顺序:

(gen-clj-fn java-str)
;;=> "(defn polar-to-complex [#^Double r #^Double theta] 
;;        (Complex/polar2Complex r theta))"

理想情况下,往返是很好的r,但我不确定它是否可能......我认为它只给出类型签名而不是名称本身。thetaclojure.reflect

4

0 回答 0