[英]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 個函數要“實現”,我所有的實現都是發送一個 map,看起來像{:type calling-function-name-kebab-case:parameter-one-kebab-case parameter-one-value:parameter-two-kebab-case parameter-two-value}
等。對於另一個宏來說似乎已經成熟了——這也會更安全,因為如果底層接口更新了更多功能,我的實現也會如此。
那可能嗎? 我什至如何開始? 我的理想方案是直接讀取 .java 代碼,但或者我可以手動將 Java 代碼粘貼到 map 結構中? 謝謝,
您可以自己解析出簡單的方法數據(我自己沒有嘗試過反射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)
]
}))
然后,一個 function 將方法規范分開,並將 args 解構為類型和名稱。 我們使用一個庫將 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})))
))
)
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 #"\("))))
output 代碼的相關位參數名稱現在如下所示:
(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")))))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.