Skip to content

Commit 225d4ba

Browse files
committed
ModuleService: be smart about maps passed to run
This hack works around an issue where some script languages, notably Jython but potentially others too, invoke the wrong run method when called with a map. The Object... varargs method is chosen instead of the Map method, with the map being passed as the sole element of the object array. The code below detects this situation, propagating the map entries into the new map. Thanks to Robert Haase for the suggestion. Closes scijava/scripting-jython#8.
1 parent e13d21f commit 225d4ba

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/main/java/org/scijava/module/DefaultModuleService.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,26 @@ private Map<String, Object> createMap(final Object[] values) {
412412

413413
final HashMap<String, Object> inputMap = new HashMap<>();
414414

415+
if (values.length == 1 && values[0] instanceof Map) {
416+
// NB: This hack works around an issue where some script languages,
417+
// notably Jython but potentially others too, invoke the wrong run
418+
// method when called with a map. The Object... varargs method is
419+
// chosen instead of the Map method, with the map being passed as
420+
// the sole element of the object array. The code below detects
421+
// this situation, propagating the map entries into the new map.
422+
final Map<?, ?> valueMap = (Map<?, ?>) values[0];
423+
for (final Object key : valueMap.keySet()) {
424+
if (!(key instanceof String)) {
425+
log.error("Invalid input name: " + key);
426+
continue;
427+
}
428+
final String name = (String) key;
429+
final Object value = valueMap.get(key);
430+
inputMap.put(name, value);
431+
}
432+
return inputMap;
433+
}
434+
415435
if (values.length % 2 != 0) {
416436
log.error("Ignoring extraneous argument: " + values[values.length - 1]);
417437
}

0 commit comments

Comments
 (0)