Skip to content

Commit 342ea48

Browse files
committed
ClojureBindings: implement all accessor methods
Previously, they threw UnsupportedOperationException due to the fact that it is tricky to access the list of available user keys. But it _can_ be done, so let's do it! This makes Clojure work much better in the SciJava REPL, which needs the ability to iterate through the variables of the engine bindings.
1 parent 8546b21 commit 342ea48

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

src/main/java/org/scijava/plugins/scripting/clojure/ClojureBindings.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,14 @@
3232
package org.scijava.plugins.scripting.clojure;
3333

3434
import java.util.Collection;
35+
import java.util.HashMap;
3536
import java.util.Map;
3637
import java.util.Set;
3738

3839
import javax.script.Bindings;
3940

41+
import clojure.lang.MapEntry;
42+
import clojure.lang.Namespace;
4043
import clojure.lang.RT;
4144
import clojure.lang.Symbol;
4245
import clojure.lang.Var;
@@ -75,7 +78,7 @@ public boolean containsKey(final Object key) {
7578

7679
@Override
7780
public boolean containsValue(final Object value) {
78-
throw new UnsupportedOperationException();
81+
return map().containsValue(value);
7982
}
8083

8184
@Override
@@ -147,17 +150,33 @@ public void clear() {
147150

148151
@Override
149152
public Set<String> keySet() {
150-
throw new UnsupportedOperationException();
153+
return map().keySet();
151154
}
152155

153156
@Override
154157
public Collection<Object> values() {
155-
throw new UnsupportedOperationException();
158+
return map().values();
156159
}
157160

158161
@Override
159162
public Set<Entry<String, Object>> entrySet() {
160-
throw new UnsupportedOperationException();
163+
return map().entrySet();
161164
}
162165

166+
// -- Helper methods --
167+
168+
private static Map<String, Object> map() {
169+
final Map<String, Object> map = new HashMap<String, Object>();
170+
171+
final Namespace ns = Namespace.find(Symbol.intern(null, USER_NS));
172+
for (final Object el : ns.getMappings()) {
173+
final MapEntry entry = (MapEntry) el;
174+
final Symbol key = (Symbol) entry.key();
175+
final Object value = Var.intern(ns, key).get();
176+
if (value instanceof Var.Unbound) continue; // skip weird variables
177+
map.put(key.getName(), value);
178+
}
179+
180+
return map;
181+
}
163182
}

0 commit comments

Comments
 (0)