Created
December 25, 2012 08:03
-
-
Save JacekLach/4372203 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; macroexpand without resolving class names | |
(defn safe-macroexpand-1 [form] | |
(if-not (seq? form) | |
form | |
(let [[op & body] form] | |
(cond | |
((some-fn special-symbol? | |
#(clojure.lang.Compiler/isMacro %) | |
(comp not symbol?)) op) | |
(macroexpand form) ;; these are safe, now java interop | |
;; (.substring s 2 5) => (. s substring 2 5) | |
(.startsWith (name op) ".") | |
`(. (identity ~(first body)) | |
~(symbol (subs (name op) 1)) | |
~@(rest body)) | |
;; (class.Name/member ...) => (. class.Name member ...) | |
(clojure.lang.Compiler/namesStaticMember op) | |
(let [cls (symbol (namespace op)) | |
memb (symbol (name op))] | |
`(. ~cls ~memb ~@body)) | |
;; (ClassName. ...) is handled correctly by macroexpand | |
:else (macroexpand form))))) | |
(defn safe-macroexpand [form] | |
(let [ex (safe-macroexpand-1 form)] | |
(if (identical? ex form) | |
form | |
(safe-macroexpand ex)))) | |
(comment | |
(macroexpand '(no.such.Class/member other args go here)) ;; ClassNotFoundException | |
(safe-macroexpand '(no.such.Class/member other args go here)) ;; (. no.such.Class member other args go here) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment