Created
April 24, 2018 11:22
-
-
Save xieyunzi/33a232308547ad5e25cd23e18f1a94c0 to your computer and use it in GitHub Desktop.
clojure reflection
This file contains hidden or 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
(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