annotate src/clojure/contrib/import_static.clj @ 10:ef7dbbd6452c

added clojure source goodness
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Aug 2010 06:25:44 -0400
parents
children
rev   line source
rlm@10 1 ;;; import_static.clj -- import static Java methods/fields into Clojure
rlm@10 2
rlm@10 3 ;; by Stuart Sierra, http://stuartsierra.com/
rlm@10 4 ;; June 1, 2008
rlm@10 5
rlm@10 6 ;; Copyright (c) Stuart Sierra, 2008. All rights reserved. The use
rlm@10 7 ;; and distribution terms for this software are covered by the Eclipse
rlm@10 8 ;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
rlm@10 9 ;; which can be found in the file epl-v10.html at the root of this
rlm@10 10 ;; distribution. By using this software in any fashion, you are
rlm@10 11 ;; agreeing to be bound by the terms of this license. You must not
rlm@10 12 ;; remove this notice, or any other, from this software.
rlm@10 13
rlm@10 14
rlm@10 15
rlm@10 16 (ns
rlm@10 17 ^{:author "Stuart Sierra",
rlm@10 18 :doc "Import static Java methods/fields into Clojure"}
rlm@10 19 clojure.contrib.import-static
rlm@10 20 (:use clojure.set))
rlm@10 21
rlm@10 22 (defmacro import-static
rlm@10 23 "Imports the named static fields and/or static methods of the class
rlm@10 24 as (private) symbols in the current namespace.
rlm@10 25
rlm@10 26 Example:
rlm@10 27 user=> (import-static java.lang.Math PI sqrt)
rlm@10 28 nil
rlm@10 29 user=> PI
rlm@10 30 3.141592653589793
rlm@10 31 user=> (sqrt 16)
rlm@10 32 4.0
rlm@10 33
rlm@10 34 Note: The class name must be fully qualified, even if it has already
rlm@10 35 been imported. Static methods are defined as MACROS, not
rlm@10 36 first-class fns."
rlm@10 37 [class & fields-and-methods]
rlm@10 38 (let [only (set (map str fields-and-methods))
rlm@10 39 the-class (. Class forName (str class))
rlm@10 40 static? (fn [x]
rlm@10 41 (. java.lang.reflect.Modifier
rlm@10 42 (isStatic (. x (getModifiers)))))
rlm@10 43 statics (fn [array]
rlm@10 44 (set (map (memfn getName)
rlm@10 45 (filter static? array))))
rlm@10 46 all-fields (statics (. the-class (getFields)))
rlm@10 47 all-methods (statics (. the-class (getMethods)))
rlm@10 48 fields-to-do (intersection all-fields only)
rlm@10 49 methods-to-do (intersection all-methods only)
rlm@10 50 make-sym (fn [string]
rlm@10 51 (with-meta (symbol string) {:private true}))
rlm@10 52 import-field (fn [name]
rlm@10 53 (list 'def (make-sym name)
rlm@10 54 (list '. class (symbol name))))
rlm@10 55 import-method (fn [name]
rlm@10 56 (list 'defmacro (make-sym name)
rlm@10 57 '[& args]
rlm@10 58 (list 'list ''. (list 'quote class)
rlm@10 59 (list 'apply 'list
rlm@10 60 (list 'quote (symbol name))
rlm@10 61 'args))))]
rlm@10 62 `(do ~@(map import-field fields-to-do)
rlm@10 63 ~@(map import-method methods-to-do))))