Clojure reify-使用另一个宏自动执行Java接口吗?

问题描述 投票:0回答:2

我有一个仅会发出事件的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个要“实现”的函数,而我的所有实现都是调度一个看起来像{:type calling-function-name-kebab-case :parameter-one-kebab-case parameter-one-value :parameter-two-kebab-case parameter-two-value}等的映射。对于另一个宏来说似乎已经成熟了-这也将更加安全,就像基础接口被更多的函数更新一样,我的实现也会如此。

有可能吗?我该如何开始?我的理想情况是直接读取.java代码,但也可以手动将Java代码粘贴到映射结构中?谢谢,

java clojure macros interop
2个回答
0
投票

您可以自己解析出简单的方法数据(我自己还没有尝试过反射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)
                      ]
          }))

然后是一个将方法规范拆开,并将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})))

      ))
  )

0
投票

clojure.reflect命名空间包含获取有关类的信息的方法。不过,我认为它不会为您提供参数名称。但是您可以使用它来实现接近您所要求的内容:

(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")))))
© www.soinside.com 2019 - 2024. All rights reserved.