The Clojure class provides a minimal interface to bootstrap Clojure access from other JVM languages. It provides: The ability to use Clojure's namespaces to locate an arbitrary var , returning the var's clojure.lang.IFn interface. <l
| 49 | * map.invoke(inc, Clojure.read("[1 2 3]"));</pre> |
| 50 | */ |
| 51 | public class Clojure { |
| 52 | private Clojure() {} |
| 53 | |
| 54 | private static Symbol asSym(Object o) { |
| 55 | Symbol s; |
| 56 | if (o instanceof String) { |
| 57 | s = Symbol.intern((String) o); |
| 58 | } else { |
| 59 | s = (Symbol) o; |
| 60 | } |
| 61 | return s; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Returns the var associated with qualifiedName. |
| 66 | * |
| 67 | * @param qualifiedName a String or clojure.lang.Symbol |
| 68 | * @return a clojure.lang.IFn |
| 69 | */ |
| 70 | public static IFn var(Object qualifiedName) { |
| 71 | Symbol s = asSym(qualifiedName); |
| 72 | return var(s.getNamespace(), s.getName()); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns an IFn associated with the namespace and name. |
| 77 | * |
| 78 | * @param ns a String or clojure.lang.Symbol |
| 79 | * @param name a String or clojure.lang.Symbol |
| 80 | * @return a clojure.lang.IFn |
| 81 | */ |
| 82 | public static IFn var(Object ns, Object name) { |
| 83 | return Var.intern(asSym(ns), asSym(name)); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Read one object from the String s. Reads data in the |
| 88 | * <a href="http://edn-format.org">edn format</a>. |
| 89 | * @param s a String |
| 90 | * @return an Object, or nil. |
| 91 | */ |
| 92 | public static Object read(String s) { |
| 93 | return EDN_READ_STRING.invoke(s); |
| 94 | } |
| 95 | |
| 96 | static { |
| 97 | RT.init(); |
| 98 | Symbol edn = (Symbol) var("clojure.core", "symbol").invoke("clojure.edn"); |
| 99 | var("clojure.core", "require").invoke(edn); |
| 100 | } |
| 101 | private static final IFn EDN_READ_STRING = var("clojure.edn", "read-string"); |
| 102 | } |