Skip to content

Instantly share code, notes, and snippets.

@scotthaleen
Forked from sunng87/reflection.clj
Created March 21, 2019 17:17
Show Gist options
  • Save scotthaleen/f68247ac8b4602a11771b9186883ab80 to your computer and use it in GitHub Desktop.
Save scotthaleen/f68247ac8b4602a11771b9186883ab80 to your computer and use it in GitHub Desktop.
clojure: access private field/method via 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 args))))
(defn private-field [obj fn-name-string]
(let [m (.. obj getClass (getDeclaredField fn-name-string))]
(. m (setAccessible true))
(. m (get obj))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment