Skip to content

Commit 6dff174

Browse files
committed
RunService: allow passing arguments to executions
When running a class, you might want to pass some arguments. To be sure, this is useful for two of our primary use cases: main methods & modules.
1 parent ef2adf3 commit 6dff174

File tree

5 files changed

+32
-9
lines changed

5 files changed

+32
-9
lines changed

src/main/java/org/scijava/command/run/CommandRunner.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ public class CommandRunner extends AbstractClassRunner {
5757
// -- ClassRunner methods --
5858

5959
@Override
60-
public void run(final Class<?> c) {
60+
public void run(final Class<?> c, final Object... args) {
6161
@SuppressWarnings("unchecked")
6262
final Class<? extends Command> commandClass = (Class<? extends Command>) c;
6363
final Plugin annotation = c.getAnnotation(Plugin.class);
6464
final CommandInfo info = new CommandInfo(commandClass, annotation);
6565
pluginService.addPlugin(info);
66-
commandService.run(info, true);
66+
commandService.run(info, true, args);
6767
}
6868

6969
// -- Typed methods --

src/main/java/org/scijava/main/run/MainRunner.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,12 @@ public class MainRunner extends AbstractClassRunner {
5555
// -- ClassRunner methods --
5656

5757
@Override
58-
public void run(final Class<?> c) throws InvocationTargetException {
58+
public void run(final Class<?> c, final Object... args)
59+
throws InvocationTargetException
60+
{
61+
final Object[] sArgs = stringify(args);
5962
try {
60-
getMain(c).invoke(null, new Object[] { new String[0] });
63+
getMain(c).invoke(null, new Object[] { sArgs });
6164
}
6265
catch (final IllegalArgumentException exc) {
6366
throw new InvocationTargetException(exc);
@@ -90,4 +93,19 @@ private Method getMain(final Class<?> c) {
9093
}
9194
}
9295

96+
/** Ensures each element is a {@link String}. */
97+
private String[] stringify(final Object... o) {
98+
final String[] s;
99+
if (o == null) s = null;
100+
else {
101+
s = new String[o.length];
102+
for (int i = 0; i < o.length; i++) {
103+
if (o[i] == null) s[i] = null;
104+
else if (o[i] instanceof String) s[i] = (String) o[i];
105+
else s[i] = o[i].toString();
106+
}
107+
}
108+
return s;
109+
}
110+
93111
}

src/main/java/org/scijava/run/ClassRunner.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
*/
5353
public interface ClassRunner extends HandlerPlugin<Class<?>> {
5454

55-
/** Executes the given class. */
56-
void run(Class<?> c) throws InvocationTargetException;
55+
/** Executes the given class, with the specified arguments. */
56+
void run(Class<?> c, Object... args) throws InvocationTargetException;
5757

5858
}

src/main/java/org/scijava/run/DefaultRunService.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ public class DefaultRunService extends
5555
// -- RunService methods --
5656

5757
@Override
58-
public void run(final Class<?> c) throws InvocationTargetException {
58+
public void run(final Class<?> c, final Object... args)
59+
throws InvocationTargetException
60+
{
5961
for (final ClassRunner runner : getInstances()) {
6062
if (runner.supports(c)) {
6163
runner.run(c);

src/main/java/org/scijava/run/RunService.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ public interface RunService extends
4545
HandlerService<Class<?>, ClassRunner>, SciJavaService
4646
{
4747

48-
/** Executes the given class using the most appropriate handler. */
49-
void run(Class<?> c) throws InvocationTargetException;
48+
/**
49+
* Executes the given class using the most appropriate handler, passing the
50+
* given arguments to the execution.
51+
*/
52+
void run(Class<?> c, Object... args) throws InvocationTargetException;
5053

5154
}

0 commit comments

Comments
 (0)