是否可以將元數據附加到Clojure gen-class?將元數據附加到Clojure gen-class
我想實現一個服務器,該服務器使用需要Java註釋添加到類中的庫。
從查斯Emerick的,等人,即將出版的新書「編程的Clojure」(第9.7.3節),加上註解根類方法是容易的,但沒有添加類級別的註解提及。
是否可以將元數據附加到Clojure gen-class?將元數據附加到Clojure gen-class
我想實現一個服務器,該服務器使用需要Java註釋添加到類中的庫。
從查斯Emerick的,等人,即將出版的新書「編程的Clojure」(第9.7.3節),加上註解根類方法是容易的,但沒有添加類級別的註解提及。
我不認爲這是可能的。
豐富希基提到添加註釋,在這個線程 https://groups.google.com/group/clojure/browse_thread/thread/d2128e1505c0c117 支持,但據我可以看到這是隻爲DEFTYPE/defrecord。我當然可能錯了。
這兩個
(ns genclass.example
(:gen-class ^{:doc "example class"}))
和
(ns genclass.example)
(with-meta
(gen-class
:name genclass.example.ClassA
:methods [[hello [] void]])
{:doc "Example class"})
編譯失敗對我來說。從異常
Exception in thread "main" java.lang.IllegalArgumentException: Metadata can only be applied to IMetas (example.clj:4)`
這聽起來像這是不可能的。
是的,我在這裏找到一個很好的例子:
https://github.com/clojure/clojure/blob/master/test/clojure/test_clojure/genclass/examples.clj
下面是一些代碼內聯,因此不會在未來消失:
(gen-class :name ^{Deprecated {}
SuppressWarnings ["Warning1"] ; discarded
java.lang.annotation.Target []}
clojure.test_clojure.genclass.examples.ExampleAnnotationClass
:prefix "annot-"
:methods [[^{Deprecated {}
Override {}} ;discarded
foo [^{java.lang.annotation.Retention java.lang.annotation.RetentionPolicy/SOURCE
java.lang.annotation.Target [java.lang.annotation.ElementType/TYPE
java.lang.annotation.ElementType/PARAMETER]}
String] void]])
附加信息添加這是因爲我無法在任何其他地方找到記錄,因此可以向構造函數添加註釋。
您可以通過將元數據添加到構造函數對的第一個數組中來向構造函數添加註釋。就像這樣:
(gen-class
:name "FooClass"
:init "init"
:constructors {^{Inject {}} [Configuration] []}
:state "state"
:implements [FooInterface]
:prefix "ref-")
我已經開始使用'deftype'代替'GEN-class'我的JAX-RS REST服務器,之後在查斯的書的例子。它似乎是「更清潔」的。 – Ralph
您也可能喜歡以下流程圖,也可以通過Chas:http://cemerick.com/2011/07/05/flowchart-for-choosing-the-right-clojure-type-definition-form/ 它可能位於他的書,我還沒有... – Paul
我在twitter上看到了流程圖。很不錯!謝謝。 – Ralph