Clojure reify - 使用另一个宏自动实现 Java 接口?
Clojure reify - automate implementation of Java interface with another macro?
我有一个只发出事件的 java 接口,我正尝试在 Clojure 中实现它。 Java接口是这样的(现实中还有很多其他方法):
public interface EWrapper {
void accountSummary(int reqId, String account, String tag, String value, String currency);
void accountSummaryEnd(int reqId);
}
我的 Clojure 代码如下所示:
(defn create
"Creates a wrapper calling a single function (cb) with maps that all have a :type to indicate
what type of messages was received, and event parameters
"
[cb]
(reify
EWrapper
(accountSummary [this reqId account tag value currency]
(dispatch-message cb {:type :account-summary :request-id reqId :account account :tag tag :value value :currency currency}))
(accountSummaryEnd [this reqId]
(dispatch-message cb {:type :account-summary-end :request-id reqId}))
))
我有大约 75 个函数到 "implement",我所有的实现都是分派一个看起来像 {:type calling-function-name-kebab-case :parameter-one-kebab-case parameter-one-value :parameter-two-kebab-case parameter-two-value}
等的地图。另一个宏似乎已经成熟 - 这也更安全,就像底层一样界面更新了更多功能,我的实现也会更新。
这可能吗?我该如何开始?我的理想情况是直接读取 .java 代码,但我也可以手动将 Java 代码粘贴到地图结构中?谢谢,
clojure.reflect
命名空间包含获取有关 class 信息的方法。不过,我认为它不会为您提供参数名称。但是您可以使用它来实现接近您要求的东西:
(ns playground.reify
(:require [clojure.reflect :as r])
(:import EWrapper))
(defn kebab-case [s]
;; TODO
s)
(defn arg-name [index]
(symbol (str "arg" index)))
(defn generate-method [member this cb]
(let [arg-names (mapv arg-name (range (count (:parameter-types member))))
method-name (:name member)]
`(~method-name [~this ~@arg-names]
(~cb {:type ~(keyword (kebab-case method-name))
:args ~arg-names}))))
(defmacro reify-ewrapper [this cb]
`(reify EWrapper
~@(map #(generate-method % this cb) (:members (r/reflect EWrapper)))))
(defn create [cb]
(reify-ewrapper this cb))
reify-ewrapper 宏调用将展开为
(reify*
[EWrapper]
(accountSummary
[this arg0 arg1 arg2 arg3 arg4]
(cb {:args [arg0 arg1 arg2 arg3 arg4], :type :accountSummary}))
(accountSummaryEnd
[this arg0]
(cb {:args [arg0], :type :accountSummaryEnd})))
要获得正确的参数名称,您可能需要解析原始 Java 源代码,我认为它们没有保留在字节码中。
带有参数名称的扩展解决方案
如果您确实需要参数名称,这里有一个小型解析器可以提取它们。您需要先要求 clojure.string :as cljstr
:
(defn parse-method [[name-str arg-str]]
(let [arg-sliced (subs arg-str 0 (cljstr/index-of arg-str ")"))
param-pairs (for [p (cljstr/split arg-sliced #",")]
(into []
(comp (map cljstr/trim)
(remove empty?)
(map symbol))
(cljstr/split p #" ")))]
{:name (symbol (subs name-str (inc (cljstr/last-index-of name-str " "))))
:parameter-types (mapv first param-pairs)
:parameter-names (mapv second param-pairs)}))
(defn parse-interface [s]
(map parse-method (partition 2 1 (cljstr/split s #"\("))))
输出参数名称的相关代码位现在如下所示:
(defn generate-method [member this cb]
(let [arg-names (:parameter-names member)
method-name (:name member)]
`(~method-name [~this ~@arg-names]
(~cb ~(merge {:type (keyword (kebab-case method-name))}
(zipmap (map (comp keyword kebab-case str)
arg-names)
arg-names))))))
(defmacro reify-ewrapper [this cb]
`(reify EWrapper
~@(map #(generate-method % this cb) (parse-interface (slurp "javasrc/EWrapper.java")))))
你可以自己解析出简单的方法数据(我自己没试过反射API)。这是一个示例,包括用于演示的单元测试。
首先,将 Java 源代码放入 Clojure 数据结构中:
(ns tst.demo.core
(:use tupelo.core tupelo.test)
(:require
[camel-snake-kebab.core :as csk]
[schema.core :as s]
[tupelo.string :as ts]))
(def java-spec
(quote {:interface EWrapper
:methods [; assume have structure of
; <ret-type> <method-name> <arglist>, where <arglist> => (<type1> <name1>, <type2> <name2> ...)
void accountSummary (int reqId, String accountName, String tag, String value, String currencyName)
void accountSummaryEnd (int reqId)
]
}))
然后,一个函数将方法规范分开,并将参数解构为类型和名称。我们使用库将 CamelCase 转换为 kabob-case:
(defn reify-gen
[spec-map]
(let [methods-data (partition 3 (grab :methods spec-map))
; >> (spyx-pretty methods-data)
method-entries (forv [mdata methods-data]
(let [[ret-type mname arglist] mdata ; ret-type unused
mname-kebab (csk/->kebab-case mname)
arg-pairs (partition 2 arglist)
arg-types (mapv first arg-pairs) ; unused
arg-names (mapv second arg-pairs)
arg-names-kebab (mapv csk/->kebab-case arg-names)
arg-names-kebab-kw (mapv ->kw arg-names-kebab)
mresult (list mname (prepend
(quote this)
arg-names)
(list
mname-kebab
(glue {:type (->kw mname-kebab)}
(zipmap arg-names-kebab-kw arg-names))))]
; (spyx-pretty mresult)
mresult ))]
(->list
(prepend
(quote reify)
(grab :interface spec-map)
method-entries))))
以及一个单元测试来演示:
(dotest
(newline)
(is= (spyx-pretty (reify-gen java-spec))
(quote
(reify
EWrapper
(accountSummary
[this reqId accountName tag value currencyName]
(account-summary
{:type :account-summary
:req-id reqId,
:account-name accountName,
:tag tag,
:value value,
:currency-name currencyName}))
(accountSummaryEnd
[this reqId]
(account-summary-end {:type :account-summary-end, :req-id reqId})))
))
)
我有一个只发出事件的 java 接口,我正尝试在 Clojure 中实现它。 Java接口是这样的(现实中还有很多其他方法):
public interface EWrapper {
void accountSummary(int reqId, String account, String tag, String value, String currency);
void accountSummaryEnd(int reqId);
}
我的 Clojure 代码如下所示:
(defn create
"Creates a wrapper calling a single function (cb) with maps that all have a :type to indicate
what type of messages was received, and event parameters
"
[cb]
(reify
EWrapper
(accountSummary [this reqId account tag value currency]
(dispatch-message cb {:type :account-summary :request-id reqId :account account :tag tag :value value :currency currency}))
(accountSummaryEnd [this reqId]
(dispatch-message cb {:type :account-summary-end :request-id reqId}))
))
我有大约 75 个函数到 "implement",我所有的实现都是分派一个看起来像 {:type calling-function-name-kebab-case :parameter-one-kebab-case parameter-one-value :parameter-two-kebab-case parameter-two-value}
等的地图。另一个宏似乎已经成熟 - 这也更安全,就像底层一样界面更新了更多功能,我的实现也会更新。
这可能吗?我该如何开始?我的理想情况是直接读取 .java 代码,但我也可以手动将 Java 代码粘贴到地图结构中?谢谢,
clojure.reflect
命名空间包含获取有关 class 信息的方法。不过,我认为它不会为您提供参数名称。但是您可以使用它来实现接近您要求的东西:
(ns playground.reify
(:require [clojure.reflect :as r])
(:import EWrapper))
(defn kebab-case [s]
;; TODO
s)
(defn arg-name [index]
(symbol (str "arg" index)))
(defn generate-method [member this cb]
(let [arg-names (mapv arg-name (range (count (:parameter-types member))))
method-name (:name member)]
`(~method-name [~this ~@arg-names]
(~cb {:type ~(keyword (kebab-case method-name))
:args ~arg-names}))))
(defmacro reify-ewrapper [this cb]
`(reify EWrapper
~@(map #(generate-method % this cb) (:members (r/reflect EWrapper)))))
(defn create [cb]
(reify-ewrapper this cb))
reify-ewrapper 宏调用将展开为
(reify*
[EWrapper]
(accountSummary
[this arg0 arg1 arg2 arg3 arg4]
(cb {:args [arg0 arg1 arg2 arg3 arg4], :type :accountSummary}))
(accountSummaryEnd
[this arg0]
(cb {:args [arg0], :type :accountSummaryEnd})))
要获得正确的参数名称,您可能需要解析原始 Java 源代码,我认为它们没有保留在字节码中。
带有参数名称的扩展解决方案
如果您确实需要参数名称,这里有一个小型解析器可以提取它们。您需要先要求 clojure.string :as cljstr
:
(defn parse-method [[name-str arg-str]]
(let [arg-sliced (subs arg-str 0 (cljstr/index-of arg-str ")"))
param-pairs (for [p (cljstr/split arg-sliced #",")]
(into []
(comp (map cljstr/trim)
(remove empty?)
(map symbol))
(cljstr/split p #" ")))]
{:name (symbol (subs name-str (inc (cljstr/last-index-of name-str " "))))
:parameter-types (mapv first param-pairs)
:parameter-names (mapv second param-pairs)}))
(defn parse-interface [s]
(map parse-method (partition 2 1 (cljstr/split s #"\("))))
输出参数名称的相关代码位现在如下所示:
(defn generate-method [member this cb]
(let [arg-names (:parameter-names member)
method-name (:name member)]
`(~method-name [~this ~@arg-names]
(~cb ~(merge {:type (keyword (kebab-case method-name))}
(zipmap (map (comp keyword kebab-case str)
arg-names)
arg-names))))))
(defmacro reify-ewrapper [this cb]
`(reify EWrapper
~@(map #(generate-method % this cb) (parse-interface (slurp "javasrc/EWrapper.java")))))
你可以自己解析出简单的方法数据(我自己没试过反射API)。这是一个示例,包括用于演示的单元测试。
首先,将 Java 源代码放入 Clojure 数据结构中:
(ns tst.demo.core
(:use tupelo.core tupelo.test)
(:require
[camel-snake-kebab.core :as csk]
[schema.core :as s]
[tupelo.string :as ts]))
(def java-spec
(quote {:interface EWrapper
:methods [; assume have structure of
; <ret-type> <method-name> <arglist>, where <arglist> => (<type1> <name1>, <type2> <name2> ...)
void accountSummary (int reqId, String accountName, String tag, String value, String currencyName)
void accountSummaryEnd (int reqId)
]
}))
然后,一个函数将方法规范分开,并将参数解构为类型和名称。我们使用库将 CamelCase 转换为 kabob-case:
(defn reify-gen
[spec-map]
(let [methods-data (partition 3 (grab :methods spec-map))
; >> (spyx-pretty methods-data)
method-entries (forv [mdata methods-data]
(let [[ret-type mname arglist] mdata ; ret-type unused
mname-kebab (csk/->kebab-case mname)
arg-pairs (partition 2 arglist)
arg-types (mapv first arg-pairs) ; unused
arg-names (mapv second arg-pairs)
arg-names-kebab (mapv csk/->kebab-case arg-names)
arg-names-kebab-kw (mapv ->kw arg-names-kebab)
mresult (list mname (prepend
(quote this)
arg-names)
(list
mname-kebab
(glue {:type (->kw mname-kebab)}
(zipmap arg-names-kebab-kw arg-names))))]
; (spyx-pretty mresult)
mresult ))]
(->list
(prepend
(quote reify)
(grab :interface spec-map)
method-entries))))
以及一个单元测试来演示:
(dotest
(newline)
(is= (spyx-pretty (reify-gen java-spec))
(quote
(reify
EWrapper
(accountSummary
[this reqId accountName tag value currencyName]
(account-summary
{:type :account-summary
:req-id reqId,
:account-name accountName,
:tag tag,
:value value,
:currency-name currencyName}))
(accountSummaryEnd
[this reqId]
(account-summary-end {:type :account-summary-end, :req-id reqId})))
))
)