Skip to content

Commit 509b5c1

Browse files
committed
CommandInfo: disallow immutable BOTH parameters
The purpose of BOTH is to indicate than an input reference will be changed in-place. It is a programming error to label an immutable parameter as BOTH; rather, the immutable parameter should be an INPUT and the (presumably affiliated) OUTPUT should be a separate variable. Arguably, this logic should live somewhere deeper than CommandInfo; we can cross that bridge later when we come to it.
1 parent 2eebc2a commit 509b5c1

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/main/java/org/scijava/command/CommandInfo.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.scijava.Cancelable;
4444
import org.scijava.Context;
4545
import org.scijava.InstantiableException;
46+
import org.scijava.ItemIO;
4647
import org.scijava.ItemVisibility;
4748
import org.scijava.ValidityProblem;
4849
import org.scijava.event.EventService;
@@ -470,6 +471,15 @@ private void checkFields(final Class<?> type) {
470471
valid = false;
471472
}
472473

474+
if (param.type() == ItemIO.BOTH && isImmutable(f.getType())) {
475+
// NB: The BOTH type signifies that the parameter will be changed
476+
// in-place somehow. But immutable parameters cannot be changed in
477+
// such a manner, so it makes no sense to label them as BOTH.
478+
final String error = "Immutable BOTH parameter: " + f;
479+
problems.add(new ValidityProblem(error));
480+
valid = false;
481+
}
482+
473483
if (!valid) {
474484
// NB: Skip invalid parameters.
475485
continue;
@@ -491,6 +501,13 @@ private void checkFields(final Class<?> type) {
491501
}
492502
}
493503

504+
private boolean isImmutable(final Class<?> type) {
505+
// NB: All eight primitive types, as well as the boxed primitive
506+
// wrapper classes, as well as strings, are immutable objects.
507+
return ClassUtils.isNumber(type) || ClassUtils.isText(type) ||
508+
ClassUtils.isBoolean(type);
509+
}
510+
494511
private Class<?> loadCommandClass() {
495512
try {
496513
return loadClass();

0 commit comments

Comments
 (0)