Skip to content

Instantly share code, notes, and snippets.

@gfredericks
Created November 9, 2011 20:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gfredericks/1352844 to your computer and use it in GitHub Desktop.
Save gfredericks/1352844 to your computer and use it in GitHub Desktop.
def bean impl
(defmacro defbean
[class-name
field-names
& interface-specs]
(let [sym-base (str (gensym)),
prefix-sym #(->> % name (str sym-base) symbol),
setter-name
(fn [field-name]
(->> field-name name inf/capitalize (str sym-base "set") symbol)),
setters
(for [field-name field-names]
`(defn ~(setter-name field-name)
[this# v#]
(swap!
(. this# ~'state)
assoc
'~field-name
v#))),
interface-methods
(for [meth interface-specs, :when (sequential? meth)]
(let [[meth-name arg-list & body] meth]
`(defn ~(prefix-sym meth-name)
~arg-list
; Create locals for all the field-names
(let [{:syms [~@field-names]} (. ~(first arg-list) ~'state)]
~@body))))]
`(do
~@setters
~@interface-methods
(defn ~(prefix-sym 'init)
[]
[[] (atom {})])
(gen-class
:name ~(str (name (ns-name *ns*)) "." class-name),
:prefix ~sym-base,
:state ~'state,
:init ~'init,
:implements [~@(filter symbol? interface-specs)],
:methods
[~@(for [field-name field-names]
[(->> field-name name inf/capitalize (str "set") symbol)
[Object]
Object])]))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment