Skip to content

Commit 2a2d8e2

Browse files
committed
CommandModuleItem: implement getDefaultValue()
The fact that commands could not report their default parameter values was an oversight. Unfortunately, to actually do it, we must instantiate a dummy instance of the Command class. But for the vast majority of cases, this approach will work just fine.
1 parent 74cdce1 commit 2a2d8e2

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-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!

0 commit comments

Comments
 (0)