Skip to content

Commit 7cc0b56

Browse files
committed
CommandInfo: allow shadowed Service parameters
When a command extends another command, we often have both classes use specific services. It's better to be able to name these Service parameters consistently, than to avoid shadowing parameters at all costs. So let's exclude all Service parameters from the validity check.
1 parent c4cb3a7 commit 7cc0b56

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import org.scijava.plugin.Parameter;
5353
import org.scijava.plugin.Plugin;
5454
import org.scijava.plugin.PluginInfo;
55+
import org.scijava.service.Service;
5556
import org.scijava.util.ClassUtils;
5657
import org.scijava.util.StringMaker;
5758
import org.scijava.util.Types;
@@ -460,7 +461,8 @@ private void checkFields(final Class<?> type) {
460461
}
461462

462463
final String name = f.getName();
463-
if (inputMap.containsKey(name) || outputMap.containsKey(name)) {
464+
if ((inputMap.containsKey(name) || outputMap.containsKey(name))
465+
&& !Service.class.isAssignableFrom(f.getType())) {
464466
// NB: Shadowed parameters are bad because they are ambiguous.
465467
final String error = "Invalid duplicate parameter: " + f;
466468
problems.add(new ValidityProblem(error));

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@
3939
import java.util.Arrays;
4040
import java.util.Iterator;
4141

42+
import org.junit.After;
4243
import org.junit.Before;
4344
import org.junit.Test;
4445
import org.scijava.Context;
4546
import org.scijava.command.CommandInfoTest.CommandWithEnumParam.Choice;
47+
import org.scijava.log.LogService;
4648
import org.scijava.module.ModuleItem;
4749
import org.scijava.plugin.Parameter;
4850

@@ -53,14 +55,20 @@
5355
*/
5456
public class CommandInfoTest {
5557

58+
private Context ctx;
5659
private CommandService commandService;
5760

5861
@Before
5962
public void setUp() {
60-
final Context ctx = new Context(CommandService.class);
63+
ctx = new Context(CommandService.class);
6164
commandService = ctx.getService(CommandService.class);
6265
}
6366

67+
@After
68+
public void tearDown() {
69+
ctx.dispose();
70+
}
71+
6472
@Test
6573
public void testEnumParam() {
6674
final CommandInfo info = commandService.getCommand(
@@ -88,6 +96,12 @@ public void testEnumParam() {
8896
choice.getChoices());
8997
}
9098

99+
@Test
100+
public void testDuplicateServiceParameters() {
101+
CommandInfo commandInfo = new CommandInfo(ExtendedServiceCommand.class);
102+
assertTrue(commandInfo.isValid());
103+
}
104+
91105
// -- Helper classes --
92106

93107
/** A command with an enum parameter. */
@@ -112,4 +126,26 @@ public void run() {
112126
// NB: No implementation needed.
113127
}
114128
}
129+
130+
private static class ServiceCommand implements Command {
131+
132+
@Parameter
133+
private LogService logService;
134+
135+
@Override
136+
public void run() {
137+
// do nothing
138+
}
139+
}
140+
141+
private static class ExtendedServiceCommand extends ServiceCommand {
142+
143+
@Parameter
144+
private LogService logService;
145+
146+
@Override
147+
public void run() {
148+
// do nothing
149+
}
150+
}
115151
}

0 commit comments

Comments
 (0)