Skip to content

Commit 685051b

Browse files
committed
Allow non public methods for @CliAvailabilityIndicator
Fixes spring-projects#122
1 parent fdf892c commit 685051b

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/main/java/org/springframework/shell/core/SimpleParser.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.springframework.shell.support.util.NaturalOrderComparator;
4242
import org.springframework.shell.support.util.OsUtils;
4343
import org.springframework.util.Assert;
44+
import org.springframework.util.ReflectionUtils;
4445
import org.springframework.util.StringUtils;
4546

4647
/**
@@ -1086,7 +1087,7 @@ public Set<String> getEveryCommand() {
10861087
public final void add(final CommandMarker command) {
10871088
synchronized (mutex) {
10881089
commands.add(command);
1089-
for (final Method method : command.getClass().getMethods()) {
1090+
for (final Method method : ReflectionUtils.getAllDeclaredMethods(command.getClass())) {
10901091
CliAvailabilityIndicator availability = method.getAnnotation(CliAvailabilityIndicator.class);
10911092
if (availability != null) {
10921093
Assert.isTrue(
@@ -1100,6 +1101,7 @@ public final void add(final CommandMarker command) {
11001101
for (String cmd : availability.value()) {
11011102
Assert.isTrue(!availabilityIndicators.containsKey(cmd),
11021103
"Cannot specify an availability indicator for '" + cmd + "' more than once");
1104+
ReflectionUtils.makeAccessible(method);
11031105
availabilityIndicators.put(cmd, new MethodTarget(method, command));
11041106
}
11051107
}

src/test/java/org/springframework/shell/core/SimpleParserTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.junit.Test;
3232

3333
import org.springframework.shell.converters.StringConverter;
34+
import org.springframework.shell.core.annotation.CliAvailabilityIndicator;
3435
import org.springframework.shell.core.annotation.CliCommand;
3536
import org.springframework.shell.core.annotation.CliOption;
3637
import org.springframework.shell.event.ParseResult;
@@ -462,6 +463,19 @@ public void testMixedCaseOptions_SHL_98() {
462463
assertThat(result, nullValue());
463464
}
464465

466+
@Test
467+
public void testAvailabilityIndicator() {
468+
MyCommands commands = new MyCommands();
469+
parser.add(commands);
470+
ParseResult result = parser.parse("foo");
471+
assertThat(result, notNullValue());
472+
473+
commands.fooIsAvailable = false;
474+
result = parser.parse("foo");
475+
assertThat(result, nullValue());
476+
477+
}
478+
465479
/**
466480
* Return a matcher that asserts that a completion, when added to {@link #buffer} at the given {@link #offset},
467481
* indeed matches the provided matcher.
@@ -490,6 +504,14 @@ protected boolean matches(Object item, Description mismatchDescription) {
490504

491505
public static class MyCommands implements CommandMarker {
492506

507+
boolean fooIsAvailable = true;
508+
509+
@CliAvailabilityIndicator("foo")
510+
/*package private. see gh-122*/boolean isFooAvailable() {
511+
System.out.println("Returning " + fooIsAvailable);
512+
return fooIsAvailable;
513+
}
514+
493515
@CliCommand("foo")
494516
public void foo() {
495517

0 commit comments

Comments
 (0)