Skip to content

Instantly share code, notes, and snippets.

@xieyunzi
Created April 24, 2018 11:22
Show Gist options
  • Save xieyunzi/33a232308547ad5e25cd23e18f1a94c0 to your computer and use it in GitHub Desktop.
Save xieyunzi/33a232308547ad5e25cd23e18f1a94c0 to your computer and use it in GitHub Desktop.
clojure reflection
(defn- invoke-private-method
[obj fn-name-string & args]
(let [m (first (filter (fn [x] (.. x getName (equals fn-name-string)))
(.. obj getClass getDeclaredMethods)))]
(. m (setAccessible true))
(. m (invoke obj (into-array Object args)))))
(defn- invoke-static-method
[klass fn-name-string & args]
(let [m (first (filter (fn [x] (.. x getName (equals fn-name-string)))
(.getDeclaredMethods klass)))]
(. m (setAccessible true))
(. m (invoke klass (into-array Object args)))))
(defn get-private-field
[obj fn-name-string]
(let [m (.. obj getClass (getDeclaredField fn-name-string))]
(. m (setAccessible true))
(. m (get obj))))
(defn set-private-field
[obj fn-name-string value]
(let [m (.. obj getClass (getDeclaredField fn-name-string))]
(. m (setAccessible true))
(. m (set obj value))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment