Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions cloud-core/src/main/java/org/incendo/cloud/CommandTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -1170,7 +1170,8 @@ private void checkAmbiguity(final @NonNull CommandNode<C> node) throws Ambiguous
node.children()
.stream()
.filter(n -> n.component() != null)
.collect(Collectors.toList())
.collect(Collectors.toList()),
null
);
}

Expand All @@ -1194,7 +1195,8 @@ private void checkAmbiguity(final @NonNull CommandNode<C> node) throws Ambiguous
node.children()
.stream()
.filter(n -> n.component() != null)
.collect(Collectors.toList())
.collect(Collectors.toList()),
nameOrAlias
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,27 @@ public final class AmbiguousNodeException extends IllegalStateException {
private final CommandNode<?> parentNode;
private final CommandNode<?> ambiguousNode;
private final List<CommandNode<?>> children;
private final String ambiguousName;

/**
* Construct a new ambiguous node exception
*
* @param parentNode Parent node
* @param ambiguousNode Node that caused exception
* @param ambiguousName Ambiguous name or alias
* @param children All children of the parent
*/
@API(status = API.Status.INTERNAL, consumers = "org.incendo.cloud.*")
public AmbiguousNodeException(
final @Nullable CommandNode<?> parentNode,
final @NonNull CommandNode<?> ambiguousNode,
final @NonNull List<@NonNull CommandNode<?>> children
final @NonNull List<@NonNull CommandNode<?>> children,
final @Nullable String ambiguousName
) {
this.parentNode = parentNode;
this.ambiguousNode = ambiguousNode;
this.children = children;
this.ambiguousName = ambiguousName;
}

/**
Expand Down Expand Up @@ -91,13 +95,28 @@ public AmbiguousNodeException(
return Collections.unmodifiableList(this.children);
}

/**
* Returns the ambiguous name or alias of the node.
* Might be null if the reason for this exception is not an ambiguous name or alias (e.g. multiple child nodes with a
* variable argument).
*
* @return the ambiguous name or alias of the node
*/
public @Nullable String getAmbiguousName() {
return this.ambiguousName;
}

@Override
public String getMessage() {
final StringBuilder stringBuilder = new StringBuilder("Ambiguous Node: ")
.append(this.ambiguousNode.component().name())
.append(" cannot be added as a child to ")
.append(this.parentNode == null ? "<root>" : this.parentNode.component().name())
.append(" (All children: ");
.append(this.parentNode == null ? "<root>" : this.parentNode.component().name());
if (this.ambiguousName != null) {
stringBuilder.append(" because of ambiguous name ")
.append(this.ambiguousName);
}
stringBuilder.append(" (All children: ");
final Iterator<CommandNode<?>> childIterator = this.children.iterator();
while (childIterator.hasNext()) {
stringBuilder.append(childIterator.next().component().name());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,14 @@ void testAmbiguousNodes() {
this.commandManager.command(this.commandManager.commandBuilder("ambiguous")
.literal("literal")));
this.setup();

// Two literals with the same alias can not co-exist, causes AmbiguousNodeException
this.commandManager.command(this.commandManager.commandBuilder("ambiguous")
.literal("literal", "alias"));
assertThrows(AmbiguousNodeException.class, () ->
this.commandManager.command(this.commandManager.commandBuilder("ambiguous")
.literal("literal2", "alias")));
this.setup();
}

@Test
Expand Down