diff options
author | Stuart Sierra <mail@stuartsierra.com> | 2010-08-07 16:41:53 -0400 |
---|---|---|
committer | Stuart Sierra <mail@stuartsierra.com> | 2010-08-07 16:41:53 -0400 |
commit | a6a92b9b3d2bfd9a56e1e5e9cfba706d1aeeaae5 (patch) | |
tree | f1f3da9887dc2dc557df3282b0bcbd4d701ec593 /modules/import-static/src | |
parent | e7930c85290f77815cdb00a60604feedfa2d0194 (diff) |
Split all namespaces into sub-modules.
* Examples and tests have not been copied over.
* Clojure test/compile phases are commented out in parent POM.
* May require installing parent POM before full build.
Diffstat (limited to 'modules/import-static/src')
-rw-r--r-- | modules/import-static/src/main/clojure/clojure/contrib/import_static.clj | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/modules/import-static/src/main/clojure/clojure/contrib/import_static.clj b/modules/import-static/src/main/clojure/clojure/contrib/import_static.clj new file mode 100644 index 00000000..c15bac52 --- /dev/null +++ b/modules/import-static/src/main/clojure/clojure/contrib/import_static.clj @@ -0,0 +1,63 @@ +;;; import_static.clj -- import static Java methods/fields into Clojure + +;; by Stuart Sierra, http://stuartsierra.com/ +;; June 1, 2008 + +;; Copyright (c) Stuart Sierra, 2008. All rights reserved. The use +;; and distribution terms for this software are covered by the Eclipse +;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) +;; which can be found in the file epl-v10.html at the root of this +;; distribution. By using this software in any fashion, you are +;; agreeing to be bound by the terms of this license. You must not +;; remove this notice, or any other, from this software. + + + +(ns + ^{:author "Stuart Sierra", + :doc "Import static Java methods/fields into Clojure"} + clojure.contrib.import-static + (:use clojure.set)) + +(defmacro import-static + "Imports the named static fields and/or static methods of the class + as (private) symbols in the current namespace. + + Example: + user=> (import-static java.lang.Math PI sqrt) + nil + user=> PI + 3.141592653589793 + user=> (sqrt 16) + 4.0 + + Note: The class name must be fully qualified, even if it has already + been imported. Static methods are defined as MACROS, not + first-class fns." + [class & fields-and-methods] + (let [only (set (map str fields-and-methods)) + the-class (. Class forName (str class)) + static? (fn [x] + (. java.lang.reflect.Modifier + (isStatic (. x (getModifiers))))) + statics (fn [array] + (set (map (memfn getName) + (filter static? array)))) + all-fields (statics (. the-class (getFields))) + all-methods (statics (. the-class (getMethods))) + fields-to-do (intersection all-fields only) + methods-to-do (intersection all-methods only) + make-sym (fn [string] + (with-meta (symbol string) {:private true})) + import-field (fn [name] + (list 'def (make-sym name) + (list '. class (symbol name)))) + import-method (fn [name] + (list 'defmacro (make-sym name) + '[& args] + (list 'list ''. (list 'quote class) + (list 'apply 'list + (list 'quote (symbol name)) + 'args))))] + `(do ~@(map import-field fields-to-do) + ~@(map import-method methods-to-do)))) |