Skip to content

Commit cdd703e

Browse files
committed
Fix alias usage with Command annotation
- Fix alias command extraction from existing @command annotations so that we actually get multiple aliases defined if more than one defined on a method level. - Fix rendering issue in a help stg template when multiple aliases exists. - Fixes #796
1 parent 811f1f9 commit cdd703e

File tree

6 files changed

+87
-13
lines changed

6 files changed

+87
-13
lines changed

spring-shell-core/src/main/java/org/springframework/shell/command/annotation/support/CommandAnnotationUtils.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.springframework.shell.command.annotation.support;
1717

18+
import java.util.List;
19+
import java.util.stream.Collectors;
1820
import java.util.stream.Stream;
1921

2022
import org.springframework.core.annotation.MergedAnnotation;
@@ -85,10 +87,10 @@ static String[] deduceCommand(MergedAnnotation<?> left, MergedAnnotation<?> righ
8587
*
8688
* @param left the left side annotation
8789
* @param right the right side annotation
88-
* @return deduced boolean for alias field
90+
* @return deduced arrays for alias field
8991
*/
90-
static String[] deduceAlias(MergedAnnotation<?> left, MergedAnnotation<?> right) {
91-
return deduceStringArray(ALIAS, left, right);
92+
static String[][] deduceAlias(MergedAnnotation<?> left, MergedAnnotation<?> right) {
93+
return deduceStringArrayLeftPrefixes(ALIAS, left, right);
9294
}
9395

9496
/**
@@ -149,6 +151,21 @@ static String deduceDescription(MergedAnnotation<?> left, MergedAnnotation<?> ri
149151
return mode;
150152
}
151153

154+
private static String[][] deduceStringArrayLeftPrefixes(String field, MergedAnnotation<?> left, MergedAnnotation<?> right) {
155+
List<String> prefix = Stream.of(left.getStringArray(field))
156+
.flatMap(command -> Stream.of(command.split(" ")))
157+
.filter(command -> StringUtils.hasText(command))
158+
.map(command -> command.strip())
159+
.collect(Collectors.toList());
160+
161+
return Stream.of(right.getStringArray(field))
162+
.flatMap(command -> Stream.of(command.split(" ")))
163+
.filter(command -> StringUtils.hasText(command))
164+
.map(command -> command.strip())
165+
.map(command -> Stream.concat(prefix.stream(), Stream.of(command)).collect(Collectors.toList()))
166+
.map(arr -> arr.toArray(String[]::new))
167+
.toArray(String[][]::new);
168+
}
152169

153170
private static String[] deduceStringArray(String field, MergedAnnotation<?> left, MergedAnnotation<?> right) {
154171
return Stream.of(left.getStringArray(field), right.getStringArray(field))

spring-shell-core/src/main/java/org/springframework/shell/command/annotation/support/CommandRegistrationFactoryBean.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,9 @@ private CommandRegistration buildRegistration() {
189189
}
190190

191191
// alias
192-
String[] deduceAlias = CommandAnnotationUtils.deduceAlias(classAnn, methodAnn);
193-
if (deduceAlias.length > 0) {
194-
builder.withAlias().command(deduceAlias);
192+
String[][] deduceAlias = CommandAnnotationUtils.deduceAlias(classAnn, methodAnn);
193+
for (String[] a : deduceAlias) {
194+
builder.withAlias().command(a);
195195
}
196196

197197
// target

spring-shell-core/src/test/java/org/springframework/shell/command/annotation/support/CommandAnnotationUtilsTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,13 @@ private static class AliasValues4 {
133133
void testAlias() {
134134
assertThat(CommandAnnotationUtils.deduceAlias(aliasDefault, aliasDefault)).isEmpty();
135135
assertThat(CommandAnnotationUtils.deduceAlias(aliasDefault, aliasValues1))
136-
.isEqualTo(new String[] { "one", "two" });
136+
.isEqualTo(new String[][] { { "one" }, { "two" } });
137137
assertThat(CommandAnnotationUtils.deduceAlias(aliasValues1, aliasValues2))
138-
.isEqualTo(new String[] { "one", "two", "three", "four" });
138+
.isEqualTo(new String[][] { { "one", "two", "three" }, { "one", "two", "four" } });
139139
assertThat(CommandAnnotationUtils.deduceAlias(aliasDefault, aliasValues3))
140-
.isEqualTo(new String[] { "five", "six", "seven" });
140+
.isEqualTo(new String[][] { { "five" }, { "six" }, { "seven" } });
141141
assertThat(CommandAnnotationUtils.deduceAlias(aliasDefault, aliasValues4))
142-
.isEqualTo(new String[] { "eight", "nine" });
142+
.isEqualTo(new String[][] { { "eight" }, { "nine" } });
143143
}
144144

145145
private static MergedAnnotation<Command> groupValue1 = MergedAnnotations.from(GroupValues1.class)

spring-shell-core/src/test/java/org/springframework/shell/command/annotation/support/CommandRegistrationFactoryBeanTests.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ void command(){
6363

6464
@Test
6565
void commandCommonThings() {
66-
configCommon(OnBothClassAndMethod.class, new OnBothClassAndMethod())
66+
configCommon(OnBothClassAndMethod.class, new OnBothClassAndMethod(), "command1", new Class[] { })
6767
.run((context) -> {
6868
CommandRegistrationFactoryBean fb = context.getBean(FACTORYBEANREF,
6969
CommandRegistrationFactoryBean.class);
@@ -75,13 +75,30 @@ void commandCommonThings() {
7575
assertThat(registration.getAliases().get(0).getCommand()).isEqualTo("three four");
7676
assertThat(registration.getGroup()).isEqualTo("group2");
7777
});
78+
configCommon(OnBothClassAndMethod.class, new OnBothClassAndMethod(), "command2", new Class[] { })
79+
.run((context) -> {
80+
CommandRegistrationFactoryBean fb = context.getBean(FACTORYBEANREF,
81+
CommandRegistrationFactoryBean.class);
82+
assertThat(fb).isNotNull();
83+
CommandRegistration registration = fb.getObject();
84+
assertThat(registration).isNotNull();
85+
assertThat(registration.getCommand()).isEqualTo("one three");
86+
assertThat(registration.getAliases()).hasSize(2);
87+
assertThat(registration.getAliases().get(0).getCommand()).isEqualTo("three four");
88+
assertThat(registration.getAliases().get(1).getCommand()).isEqualTo("three five");
89+
assertThat(registration.getGroup()).isEqualTo("group2");
90+
});
7891
}
7992

8093
@Command(command = "one", alias = "three", group = "group1")
8194
private static class OnBothClassAndMethod {
8295

8396
@Command(command = "two", alias = "four", group = "group2")
84-
void command(){
97+
void command1(){
98+
}
99+
100+
@Command(command = "three", alias = { "four", "five" }, group = "group2")
101+
void command2(){
85102
}
86103
}
87104

spring-shell-samples/spring-shell-sample-e2e/src/main/java/org/springframework/shell/samples/e2e/AliasCommands.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,38 @@
1818
import org.springframework.context.annotation.Bean;
1919
import org.springframework.shell.command.CommandRegistration;
2020
import org.springframework.shell.command.annotation.Command;
21+
import org.springframework.shell.standard.ShellComponent;
22+
import org.springframework.shell.standard.ShellMethod;
2123
import org.springframework.stereotype.Component;
2224

2325
public class AliasCommands {
2426

27+
@ShellComponent
28+
public static class LegacyAnnotation extends BaseE2ECommands {
29+
30+
@ShellMethod(key = { LEGACY_ANNO + "alias-1", LEGACY_ANNO + "aliasfor-1" }, group = GROUP)
31+
public String testAlias1LegacyAnnotation() {
32+
return "Hello from alias command";
33+
}
34+
35+
@ShellMethod(key = { LEGACY_ANNO + "alias-2", LEGACY_ANNO + "alias1for-2", LEGACY_ANNO + "alias2for-2" }, group = GROUP)
36+
public String testAlias2LegacyAnnotation() {
37+
return "Hello from alias command";
38+
}
39+
}
40+
2541
@Command(command = BaseE2ECommands.ANNO, alias = BaseE2ECommands.ANNO, group = BaseE2ECommands.GROUP)
2642
public static class AliasCommandsAnnotation extends BaseE2ECommands {
2743

2844
@Command(command = "alias-1", alias = "aliasfor-1")
2945
public String testAlias1Annotation() {
3046
return "Hello from alias command";
3147
}
48+
49+
@Command(command = "alias-2", alias = { "alias1for-2", "alias2for-2" })
50+
public String testAlias2Annotation() {
51+
return "Hello from alias command";
52+
}
3253
}
3354

3455
@Component
@@ -49,6 +70,25 @@ public CommandRegistration testAlias1Registration(CommandRegistration.BuilderSup
4970
.and()
5071
.build();
5172
}
73+
74+
@Bean
75+
public CommandRegistration testAlias2Registration(CommandRegistration.BuilderSupplier builder) {
76+
return builder.get()
77+
.command(REG, "alias-2")
78+
.group(GROUP)
79+
.withAlias()
80+
.command(REG, "alias1for-2")
81+
.and()
82+
.withAlias()
83+
.command(REG, "alias2for-2")
84+
.and()
85+
.withTarget()
86+
.function(ctx -> {
87+
return "Hello from alias command";
88+
})
89+
.and()
90+
.build();
91+
}
5292
}
5393

5494
}

spring-shell-standard-commands/src/main/resources/template/help-command-default.stg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ availability(availability) ::= <<
7070
aliases(aliases) ::= <<
7171
<if(aliases)>
7272
<("ALSO KNOWN AS"); format="style-highlight">
73-
<(aliases); separator=", ">
73+
<aliases: { a | <a>}; separator=", ">
7474
<endif>
7575
>>
7676

0 commit comments

Comments
 (0)