Skip to content

Commit 4cb914d

Browse files
committed
Also strip Command and Cmd from the beginning of a class name for the default alias
1 parent 831e1c0 commit 4cb914d

File tree

5 files changed

+18
-17
lines changed

5 files changed

+18
-17
lines changed

readme/README_template.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ call the methods on the command implementation.
160160

161161
By overwriting the `Command#getAliases()` method or applying one or multiple [`@Alias`][@Alias JavaDoc] annotations the
162162
aliases to which the command reacts can be configured. If at least one alias is configured, only the explicitly
163-
configured ones are available. If no alias is configured, the class name, stripped by `Command` or `Cmd` suffix
164-
and the first letter lowercased is used as default.
163+
configured ones are available. If no alias is configured, the class name, stripped by `Command` or `Cmd`
164+
suffix and / or prefix and the first letter lowercased is used as default.
165165

166166
#### Asynchronous Command Execution
167167

src/main/java/net/kautler/command/api/Command.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public interface Command<M> {
6666
*
6767
* <p>The default implementation of this method returns the aliases configured using the {@link Alias @Alias}
6868
* annotation. If no alias is configured by annotation, the class name, stripped by {@code Command} or {@code Cmd}
69-
* suffix if present and the first letter lowercased is used as default.
69+
* suffix and / or prefix if present and the first letter lowercased is used as default.
7070
*
7171
* <p>If this method is overwritten and there are annotations, the method overwrite takes precedence.
7272
*
@@ -89,9 +89,8 @@ default List<String> getAliases() {
8989
if (className.isEmpty()) {
9090
className = clazz.getTypeName().substring(clazz.getPackage().getName().length() + 1);
9191
}
92-
String defaultAlias = className
93-
.replaceFirst("(?i)(?:Command|Cmd)$", "")
94-
.replaceFirst("^.", Character.toString(toLowerCase(className.charAt(0))));
92+
String defaultAlias = className.replaceAll("(?i)^(?:Command|Cmd)|(?:Command|Cmd)$", "");
93+
defaultAlias = defaultAlias.replaceFirst("^.", Character.toString(toLowerCase(defaultAlias.charAt(0))));
9594
return singletonList(defaultAlias);
9695
}
9796

src/main/java/net/kautler/command/api/annotation/Alias.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
/**
3030
* An annotation with which one or more aliases for a command can be configured.
3131
* If at least one alias is configured, only the explicitly configured ones are available.
32-
* If no alias is configured, the class name, stripped by {@code Command} or {@code Cmd} suffix if present
33-
* and the first letter lowercased is used as default.
32+
* If no alias is configured, the class name, stripped by {@code Command} or {@code Cmd}
33+
* suffix and / or prefix if present and the first letter lowercased is used as default.
3434
*
3535
* <p>Alternatively to using this annotation the {@link Command#getAliases()} method can be overwritten.
3636
* If that method is overwritten and this annotation is used, the method overwrite takes precedence.

src/pitest/java/net/kautler/test/pitest/ExplicitMutationFilterDetails.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import org.pitest.mutationtest.engine.MutationDetails;
2020

21-
import java.util.Arrays;
2221
import java.util.Collection;
2322
import java.util.Objects;
2423
import java.util.StringJoiner;

src/test/groovy/net/kautler/command/api/CommandTest.groovy

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ class CommandTest extends Specification {
5353
new Test4() || ['test']
5454
new Test5() || ['test1', 'test2']
5555
new Test6() || ['Test1', 'Test2']
56-
new BaseCommand() { } || [testee.getClass().typeName[(testee.getClass().package.name.length() + 1)..-1].uncapitalize()]
56+
new CommandTest7() || ['test7']
57+
new CmdTest8() || ['test8']
58+
new CommandTest9Cmd() || ['test9']
59+
new BaseCommand() { } || [testee.getClass().typeName[(testee.getClass().package.name.length() + 8)..-1].uncapitalize()]
5760

5861
and:
5962
aliases = expectedAliases.size() == 1 ? 'alias' : 'aliases'
@@ -182,7 +185,7 @@ class CommandTest extends Specification {
182185

183186
def 'multiple restrictions with ALL_OF policy should require all those restrictions'() {
184187
given:
185-
def testee = new Test7()
188+
def testee = new CommandTest7()
186189
def testeeClass = testee.getClass()
187190

188191
expect:
@@ -211,7 +214,7 @@ class CommandTest extends Specification {
211214

212215
def 'multiple restrictions with ANY_OF policy should require one of those restrictions'() {
213216
given:
214-
def testee = new Test8()
217+
def testee = new CmdTest8()
215218
def testeeClass = testee.getClass()
216219

217220
expect:
@@ -240,7 +243,7 @@ class CommandTest extends Specification {
240243

241244
def 'multiple restrictions with NONE_OF policy should require all those restrictions negated'() {
242245
given:
243-
def testee = new Test9()
246+
def testee = new CommandTest9Cmd()
244247
def testeeClass = testee.getClass()
245248

246249
expect:
@@ -295,7 +298,7 @@ class CommandTest extends Specification {
295298
}
296299

297300
and:
298-
def testee = new Test9()
301+
def testee = new CommandTest9Cmd()
299302
def testeeClass = testee.getClass()
300303

301304
expect:
@@ -379,19 +382,19 @@ class CommandTest extends Specification {
379382
@RestrictedTo(Restriction2)
380383
@RestrictedTo(Restriction3)
381384
@RestrictionPolicy(ALL_OF)
382-
static class Test7 extends BaseCommand { }
385+
static class CommandTest7 extends BaseCommand { }
383386

384387
@RestrictedTo(Restriction1)
385388
@RestrictedTo(Restriction2)
386389
@RestrictedTo(Restriction3)
387390
@RestrictionPolicy(ANY_OF)
388-
static class Test8 extends BaseCommand { }
391+
static class CmdTest8 extends BaseCommand { }
389392

390393
@RestrictedTo(Restriction1)
391394
@RestrictedTo(Restriction2)
392395
@RestrictedTo(Restriction3)
393396
@RestrictionPolicy(NONE_OF)
394-
static class Test9 extends BaseCommand { }
397+
static class CommandTest9Cmd extends BaseCommand { }
395398

396399
static class BaseRestriction implements Restriction<Object> {
397400
@Override

0 commit comments

Comments
 (0)