Skip to content

Commit c17958f

Browse files
committed
Merge branch 'command-default-values'
This makes the getDefaultValue() method work for Command modules. This branch is dedicated to Christian Dietz.
2 parents 74cdce1 + cde0f42 commit c17958f

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,38 @@ public T getMaximumValue() {
143143
return tValue(getParameter().max());
144144
}
145145

146+
@Override
147+
public T getDefaultValue() {
148+
// NB: The default value for a command is the initial field value.
149+
// E.g.:
150+
//
151+
// @Parameter
152+
// private int weekdays = 5;
153+
//
154+
// To obtain this information, we need to instantiate the module, then
155+
// extract the value of the associated field.
156+
//
157+
// Of course, the command might do evil things like:
158+
//
159+
// @Parameter
160+
// private long time = System.currentTimeMillis();
161+
//
162+
// In which case the default value will vary by instance. But there is
163+
// nothing we can really do about that. This is only a best effort.
164+
165+
try {
166+
final Object dummy = getInfo().loadDelegateClass().newInstance();
167+
@SuppressWarnings("unchecked")
168+
final T value = (T) getField().get(dummy);
169+
return value;
170+
}
171+
catch (final InstantiationException | IllegalAccessException
172+
| ClassNotFoundException exc)
173+
{
174+
throw new IllegalStateException(exc);
175+
}
176+
}
177+
146178
@Override
147179
public Number getStepSize() {
148180
// FIXME: stepSize should be typed on T, not Number!

src/test/java/org/scijava/command/CommandModuleTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.scijava.module.Module;
4444
import org.scijava.module.process.AbstractPreprocessorPlugin;
4545
import org.scijava.module.process.PreprocessorPlugin;
46+
import org.scijava.plugin.Parameter;
4647
import org.scijava.plugin.Plugin;
4748

4849
/** Regression tests for {@link CommandModule}. */
@@ -76,6 +77,25 @@ public void testNotCancelable() throws InterruptedException,
7677
assertEquals("NO SINGING!", fire.getCancelReason());
7778
}
7879

80+
@Test
81+
public void testDefaultValues() {
82+
final Context context = new Context(CommandService.class);
83+
final CommandService commandService = context.service(CommandService.class);
84+
final CommandInfo info = //
85+
commandService.getCommand(CommandWithDefaultValues.class);
86+
87+
assertEquals(5, info.getInput("weekdays").getDefaultValue());
88+
89+
final long defaultTime = (Long) info.getInput("time").getDefaultValue();
90+
final long timeDiff = System.currentTimeMillis() - defaultTime;
91+
assertTrue(timeDiff >= 0 && timeDiff < 50); // 50 ms should be enough ;-)
92+
93+
final String defaultName = (String) info.getInput("name").getDefaultValue();
94+
assertEquals("John Jacob Jingleheimer Schmidt", defaultName);
95+
96+
assertEquals(null, info.getInput("thing").getDefaultValue());
97+
}
98+
7999
// -- Helper classes --
80100

81101
/** A command which implements {@link Cancelable}. */
@@ -125,4 +145,27 @@ public void process(final Module module) {
125145
}
126146
}
127147
}
148+
149+
/** A command which assigns default values to its parameters. */
150+
@Plugin(type = Command.class)
151+
public static class CommandWithDefaultValues extends ContextCommand {
152+
153+
@Parameter
154+
private int weekdays = 5;
155+
156+
@Parameter
157+
private long time = System.currentTimeMillis();
158+
159+
@Parameter
160+
private String name = "John Jacob Jingleheimer Schmidt";
161+
162+
@Parameter
163+
private Object thing;
164+
165+
@Override
166+
public void run() {
167+
weekdays = 0;
168+
time = 0;
169+
}
170+
}
128171
}

0 commit comments

Comments
 (0)