Skip to content

Commit 4c46edd

Browse files
committed
Generalize CodeRunner from Class to Object
Sometimes we want to run Class objects. Sometimes String identifiers. Sometimes maybe other things. Rather than overtype things with a new interface, let's just use good ol' Object here, for flexibility.
1 parent dd3271c commit 4c46edd

File tree

6 files changed

+76
-33
lines changed

6 files changed

+76
-33
lines changed

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,26 @@ public class CommandCodeRunner extends AbstractCodeRunner {
5656
// -- CodeRunner methods --
5757

5858
@Override
59-
public void run(final Class<?> c, final Object... args) {
60-
@SuppressWarnings("unchecked")
61-
final Class<? extends Command> commandClass = (Class<? extends Command>) c;
62-
commandService.run(commandClass, true, args);
59+
public void run(final Object code, final Object... args) {
60+
commandService.run(getCommandClass(code), true, args);
6361
}
6462

6563
// -- Typed methods --
6664

6765
@Override
68-
public boolean supports(final Class<?> c) {
69-
return Command.class.isAssignableFrom(c);
66+
public boolean supports(final Object code) {
67+
return getCommandClass(code) != null;
68+
}
69+
70+
// -- Helper methods --
71+
72+
private Class<? extends Command> getCommandClass(final Object code) {
73+
if (!(code instanceof Class)) return null;
74+
final Class<?> c = (Class<?>) code;
75+
if (!Command.class.isAssignableFrom(c)) return null;
76+
@SuppressWarnings("unchecked")
77+
final Class<? extends Command> commandClass = (Class<? extends Command>) c;
78+
return commandClass;
7079
}
7180

7281
}

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.scijava.plugin.Plugin;
4141
import org.scijava.run.AbstractCodeRunner;
4242
import org.scijava.run.CodeRunner;
43+
import org.scijava.util.ClassUtils;
4344

4445
/**
4546
* Executes the given class's {@code main} method.
@@ -55,12 +56,12 @@ public class MainCodeRunner extends AbstractCodeRunner {
5556
// -- CodeRunner methods --
5657

5758
@Override
58-
public void run(final Class<?> c, final Object... args)
59+
public void run(final Object code, final Object... args)
5960
throws InvocationTargetException
6061
{
6162
final Object[] sArgs = stringify(args);
6263
try {
63-
getMain(c).invoke(null, new Object[] { sArgs });
64+
getMain(code).invoke(null, new Object[] { sArgs });
6465
}
6566
catch (final IllegalArgumentException exc) {
6667
throw new InvocationTargetException(exc);
@@ -73,13 +74,15 @@ public void run(final Class<?> c, final Object... args)
7374
// -- Typed methods --
7475

7576
@Override
76-
public boolean supports(final Class<?> c) {
77-
return getMain(c) != null;
77+
public boolean supports(final Object code) {
78+
return getMain(code) != null;
7879
}
7980

8081
// -- Helper methods --
8182

82-
private Method getMain(final Class<?> c) {
83+
private Method getMain(final Object code) {
84+
final Class<?> c = getClass(code);
85+
if (c == null) return null;
8386
try {
8487
return c.getMethod("main", String[].class);
8588
}
@@ -93,6 +96,12 @@ private Method getMain(final Class<?> c) {
9396
}
9497
}
9598

99+
private Class<?> getClass(final Object code) {
100+
if (code instanceof Class) return (Class<?>) code;
101+
if (code instanceof String) return ClassUtils.loadClass((String) code);
102+
return null;
103+
}
104+
96105
/** Ensures each element is a {@link String}. */
97106
private String[] stringify(final Object... o) {
98107
final String[] s;

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,14 @@
3939
* @author Curtis Rueden
4040
*/
4141
public abstract class AbstractCodeRunner extends
42-
AbstractHandlerPlugin<Class<?>> implements CodeRunner
42+
AbstractHandlerPlugin<Object> implements CodeRunner
4343
{
4444

4545
// -- Typed methods --
4646

4747
@Override
48-
@SuppressWarnings({ "rawtypes", "unchecked" })
49-
public Class<Class<?>> getType() {
50-
return (Class) Class.class;
48+
public Class<Object> getType() {
49+
return Object.class;
5150
}
5251

5352
}

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,17 @@
3333

3434
import java.lang.reflect.InvocationTargetException;
3535

36+
import org.scijava.Identifiable;
3637
import org.scijava.plugin.HandlerPlugin;
3738
import org.scijava.plugin.Plugin;
3839

3940
/**
4041
* A plugin which extends the {@link RunService}'s execution handling. A
41-
* {@link CodeRunner} knows how to execute certain classes, beyond just Java's
42-
* usual {@code main} method.
42+
* {@link CodeRunner} knows how to execute code of a certain form, such as the
43+
* {@code main} method of a Java {@link Class}, or an {@link Identifiable}
44+
* SciJava module.
4345
* <p>
44-
* Class runner plugins discoverable at runtime must implement this interface
46+
* Code runner plugins discoverable at runtime must implement this interface
4547
* and be annotated with @{@link Plugin} with attribute {@link Plugin#type()} =
4648
* {@link CodeRunner}.class. While it possible to create a class runner plugin
4749
* merely by implementing this interface, it is encouraged to instead extend
@@ -50,9 +52,12 @@
5052
*
5153
* @author Curtis Rueden
5254
*/
53-
public interface CodeRunner extends HandlerPlugin<Class<?>> {
55+
public interface CodeRunner extends HandlerPlugin<Object> {
5456

55-
/** Executes the given class, with the specified arguments. */
56-
void run(Class<?> c, Object... args) throws InvocationTargetException;
57+
/**
58+
* Executes the code identified by the given object, passing the
59+
* specified arguments as inputs.
60+
*/
61+
void run(Object code, Object... args) throws InvocationTargetException;
5762

5863
}

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

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
package org.scijava.run;
3333

3434
import java.lang.reflect.InvocationTargetException;
35+
import java.util.Map;
3536

3637
import org.scijava.log.LogService;
3738
import org.scijava.plugin.AbstractHandlerService;
@@ -46,7 +47,7 @@
4647
*/
4748
@Plugin(type = Service.class)
4849
public class DefaultRunService extends
49-
AbstractHandlerService<Class<?>, CodeRunner> implements RunService
50+
AbstractHandlerService<Object, CodeRunner> implements RunService
5051
{
5152

5253
@Parameter
@@ -55,16 +56,29 @@ public class DefaultRunService extends
5556
// -- RunService methods --
5657

5758
@Override
58-
public void run(final Class<?> c, final Object... args)
59+
public void run(final Object code, final Object... args)
5960
throws InvocationTargetException
6061
{
6162
for (final CodeRunner runner : getInstances()) {
62-
if (runner.supports(c)) {
63-
runner.run(c);
63+
if (runner.supports(code)) {
64+
runner.run(code, args);
6465
return;
6566
}
6667
}
67-
throw new IllegalArgumentException("Unknown class type: " + c.getName());
68+
throw new IllegalArgumentException("Unknown code type: " + code);
69+
}
70+
71+
@Override
72+
public void run(final Object code, final Map<String, Object> inputMap)
73+
throws InvocationTargetException
74+
{
75+
for (final CodeRunner runner : getInstances()) {
76+
if (runner.supports(code)) {
77+
runner.run(code, inputMap);
78+
return;
79+
}
80+
}
81+
throw new IllegalArgumentException("Unknown code type: " + code);
6882
}
6983

7084
// -- PTService methods --
@@ -77,9 +91,8 @@ public Class<CodeRunner> getPluginType() {
7791
// -- Typed methods --
7892

7993
@Override
80-
@SuppressWarnings({ "rawtypes", "unchecked" })
81-
public Class<Class<?>> getType() {
82-
return (Class) Class.class;
94+
public Class<Object> getType() {
95+
return Object.class;
8396
}
8497

8598
}

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
package org.scijava.run;
3333

3434
import java.lang.reflect.InvocationTargetException;
35+
import java.util.Map;
3536

3637
import org.scijava.plugin.HandlerService;
3738
import org.scijava.service.SciJavaService;
@@ -42,13 +43,20 @@
4243
* @author Curtis Rueden
4344
*/
4445
public interface RunService extends
45-
HandlerService<Class<?>, CodeRunner>, SciJavaService
46+
HandlerService<Object, CodeRunner>, SciJavaService
4647
{
4748

4849
/**
49-
* Executes the given class using the most appropriate handler, passing the
50-
* given arguments to the execution.
50+
* Executes the given code using the most appropriate handler, passing the
51+
* specified arguments as inputs.
5152
*/
52-
void run(Class<?> c, Object... args) throws InvocationTargetException;
53+
void run(Object code, Object... args) throws InvocationTargetException;
54+
55+
/**
56+
* Executes the given code using the most appropriate handler, passing the
57+
* arguments in the specified map as inputs.
58+
*/
59+
void run(Object code, Map<String, Object> inputMap)
60+
throws InvocationTargetException;
5361

5462
}

0 commit comments

Comments
 (0)