Skip to content

Commit c677ea1

Browse files
committed
ScriptInfo: try harder when converting types
If we cannot convert directly from source to target type, try converting through String before giving up. With this change, the following now works as desired: // @string(visibility=MESSAGE) text The reason this change fixes the above is that the expression "visibility=MESSAGE" evaluates to the map: visibility -> new org.scijava.sjep.eval.Unresolved("MESSAGE") That is, the variable "visibility" contains an object of type org.scijava.sjep.eval.Unresolved, with a token value of "MESSAGE". We cannot convert from an org.scijava.sjep.eval.Unresolved to an org.scijava.ItemVisibility, but we _can_ convert from Unresolved to String (the toString() method is called, resulting in "MESSAGE"), followed by String to ItemVisibility (the string value is considered to be naming one of the enumeration's values). This heuristic avoids an explicit assumption in the scripting framework of the ParseService being backed by SJEP (it might not be -- someone might be using their own higher priority ParseService).
1 parent b700de6 commit c677ea1

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,13 @@ private boolean is(final String key, final String desired) {
450450

451451
/** Super terse conversion helper method. */
452452
private <T> T as(final Object v, final Class<T> type) {
453-
return convertService.convert(v, type);
453+
final T converted = convertService.convert(v, type);
454+
if (converted != null) return converted;
455+
// NB: Attempt to convert via string.
456+
// This is useful in cases where a weird type of object came back
457+
// (e.g., org.scijava.sjep.eval.Unresolved), but which happens to have a
458+
// nice string representation which ultimately is expressible as the type.
459+
return convertService.convert(v.toString(), type);
454460
}
455461

456462
private <T> List<T> asList(final Object v, final Class<T> type) {

0 commit comments

Comments
 (0)