Skip to content

Commit 235051d

Browse files
committed
DefaultScriptService: support arrays of aliases
When typing a script parameter on e.g. "String[][]", where "String" is aliases to "java.lang.String", we want this to work as expected for all array dimensionalities, rather than needing to explicitly alias all the array types too.
1 parent 0ce6c58 commit 235051d

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/main/java/org/scijava/script/DefaultScriptService.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,12 @@ public void addAlias(final String alias, final Class<?> type) {
254254
public synchronized Class<?> lookupClass(final String alias)
255255
throws ScriptException
256256
{
257-
final Class<?> type = aliasMap().get(alias);
258-
if (type != null) return type;
257+
final String componentAlias = stripArrayNotation(alias);
258+
final Class<?> type = aliasMap().get(componentAlias);
259+
if (type != null) {
260+
final int arrayDim = (alias.length() - componentAlias.length()) / 2;
261+
return makeArrayType(type, arrayDim);
262+
}
259263

260264
try {
261265
final Class<?> c = ClassUtils.loadClass(alias, false);
@@ -473,4 +477,16 @@ private Future<ScriptModule> cast(final Future<Module> future) {
473477
return (Future) future;
474478
}
475479

480+
// -- Helper methods - aliases --
481+
482+
private String stripArrayNotation(final String alias) {
483+
if (!alias.endsWith("[]")) return alias;
484+
return stripArrayNotation(alias.substring(0, alias.length() - 2));
485+
}
486+
487+
private Class<?> makeArrayType(final Class<?> type, final int arrayDim) {
488+
if (arrayDim <= 0) return type;
489+
return makeArrayType(ClassUtils.getArrayClass(type), arrayDim - 1);
490+
}
491+
476492
}

0 commit comments

Comments
 (0)