-
Notifications
You must be signed in to change notification settings - Fork 8
/
util.clj
49 lines (41 loc) · 1.27 KB
/
util.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
(ns lambdaisland.witchcraft.util
(:require [clojure.string :as str]))
(set! *warn-on-reflection* true)
(defn enum->map [enum]
(into {}
(map (juxt #(keyword (str/replace (str/lower-case (.name ^Enum %)) "_" "-")) identity))
(java.util.EnumSet/allOf enum)))
(defn accessible-field ^java.lang.reflect.Field [^Class klz field]
(doto (.getDeclaredField klz field)
(.setAccessible true)))
(defn set-field! [klz field obj val]
(.set (accessible-field klz field) obj val))
(defn set-static! [klz field val]
(set-field! klz field klz val))
(defn dasherize
"Converts an underscored or camelized string
into an dasherized one."
[s]
(when s
(-> s
(str/replace #"([A-Z][a-z]+)" (fn [[match c]]
(if c (str "-" (str/lower-case c)) "")))
(str/replace #"([A-Z]+)" "-$1")
(str/replace #"[-_\s]+" "-")
(str/replace #"^-" "")
(str/replace #"-$" ""))))
(defmacro when-class-exists
{:style/indent 1}
[classname & body]
(try
(Class/forName (str classname))
`(do ~@body)
(catch ClassNotFoundException e)))
(defmacro if-class-exists
{:style/indent 1}
[classname then else]
(try
(Class/forName (str classname))
then
(catch ClassNotFoundException e
else)))