Mercurial > lasercutter
diff src/clojure/contrib/reflect.clj @ 10:ef7dbbd6452c
added clojure source goodness
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 21 Aug 2010 06:25:44 -0400 |
parents | |
children |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/clojure/contrib/reflect.clj Sat Aug 21 06:25:44 2010 -0400 1.3 @@ -0,0 +1,33 @@ 1.4 +; Copyright (c) 2010 Stuart Halloway & Contributors. All rights 1.5 +; reserved. The use and distribution terms for this software are 1.6 +; covered by the Eclipse Public License 1.0 1.7 +; (http://opensource.org/licenses/eclipse-1.0.php) which can be 1.8 +; found in the file epl-v10.html at the root of this distribution. 1.9 +; By using this software in any fashion, you are agreeing to be 1.10 +; bound by the terms of this license. You must not remove this 1.11 +; notice, or any other, from this software. 1.12 + 1.13 +(ns clojure.contrib.reflect) 1.14 + 1.15 +(defn call-method 1.16 + "Calls a private or protected method. 1.17 + 1.18 + params is a vector of classes which correspond to the arguments to 1.19 + the method e 1.20 + 1.21 + obj is nil for static methods, the instance object otherwise. 1.22 + 1.23 + The method-name is given a symbol or a keyword (something Named)." 1.24 + [klass method-name params obj & args] 1.25 + (-> klass (.getDeclaredMethod (name method-name) 1.26 + (into-array Class params)) 1.27 + (doto (.setAccessible true)) 1.28 + (.invoke obj (into-array Object args)))) 1.29 + 1.30 +(defn get-field 1.31 + "Access to private or protected field. field-name is a symbol or 1.32 + keyword." 1.33 + [klass field-name obj] 1.34 + (-> klass (.getDeclaredField (name field-name)) 1.35 + (doto (.setAccessible true)) 1.36 + (.get obj)))