Allow non public methods for @CliAvailabilityIndicator
Fixes https://github.com/spring-projects/spring-shell/issues/122
This commit is contained in:
@@ -41,6 +41,7 @@ import org.springframework.shell.support.util.ExceptionUtils;
|
||||
import org.springframework.shell.support.util.NaturalOrderComparator;
|
||||
import org.springframework.shell.support.util.OsUtils;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
@@ -1086,7 +1087,7 @@ public class SimpleParser implements Parser {
|
||||
public final void add(final CommandMarker command) {
|
||||
synchronized (mutex) {
|
||||
commands.add(command);
|
||||
for (final Method method : command.getClass().getMethods()) {
|
||||
for (final Method method : ReflectionUtils.getAllDeclaredMethods(command.getClass())) {
|
||||
CliAvailabilityIndicator availability = method.getAnnotation(CliAvailabilityIndicator.class);
|
||||
if (availability != null) {
|
||||
Assert.isTrue(
|
||||
@@ -1100,6 +1101,7 @@ public class SimpleParser implements Parser {
|
||||
for (String cmd : availability.value()) {
|
||||
Assert.isTrue(!availabilityIndicators.containsKey(cmd),
|
||||
"Cannot specify an availability indicator for '" + cmd + "' more than once");
|
||||
ReflectionUtils.makeAccessible(method);
|
||||
availabilityIndicators.put(cmd, new MethodTarget(method, command));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.shell.converters.StringConverter;
|
||||
import org.springframework.shell.core.annotation.CliAvailabilityIndicator;
|
||||
import org.springframework.shell.core.annotation.CliCommand;
|
||||
import org.springframework.shell.core.annotation.CliOption;
|
||||
import org.springframework.shell.event.ParseResult;
|
||||
@@ -462,6 +463,19 @@ public class SimpleParserTests {
|
||||
assertThat(result, nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAvailabilityIndicator() {
|
||||
MyCommands commands = new MyCommands();
|
||||
parser.add(commands);
|
||||
ParseResult result = parser.parse("foo");
|
||||
assertThat(result, notNullValue());
|
||||
|
||||
commands.fooIsAvailable = false;
|
||||
result = parser.parse("foo");
|
||||
assertThat(result, nullValue());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a matcher that asserts that a completion, when added to {@link #buffer} at the given {@link #offset},
|
||||
* indeed matches the provided matcher.
|
||||
@@ -490,6 +504,14 @@ public class SimpleParserTests {
|
||||
|
||||
public static class MyCommands implements CommandMarker {
|
||||
|
||||
boolean fooIsAvailable = true;
|
||||
|
||||
@CliAvailabilityIndicator("foo")
|
||||
/*package private. see gh-122*/boolean isFooAvailable() {
|
||||
System.out.println("Returning " + fooIsAvailable);
|
||||
return fooIsAvailable;
|
||||
}
|
||||
|
||||
@CliCommand("foo")
|
||||
public void foo() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user