Skip to content

Make CommandMethod applicable to class #301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 19, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -406,13 +406,17 @@ private <T> void parseParsers(final @NonNull T instance) {
final @NonNull Object instance,
final @NonNull Collection<@NonNull CommandMethodPair> methodPairs
) {
final AnnotationAccessor classAnnotations = AnnotationAccessor.of(instance.getClass());
final CommandMethod classCommandMethod = classAnnotations.annotation(CommandMethod.class);
final String syntaxPrefix = classCommandMethod == null ? "" : (classCommandMethod.value() + " ");
final Collection<Command<C>> commands = new ArrayList<>();
for (final CommandMethodPair commandMethodPair : methodPairs) {
final CommandMethod commandMethod = commandMethodPair.getCommandMethod();
final Method method = commandMethodPair.getMethod();
final List<SyntaxFragment> tokens = this.syntaxParser.apply(commandMethod.value());
final String syntax = syntaxPrefix + commandMethod.value();
final List<SyntaxFragment> tokens = this.syntaxParser.apply(syntax);
/* Determine command name */
final String commandToken = commandMethod.value().split(" ")[0].split("\\|")[0];
final String commandToken = syntax.split(" ")[0].split("\\|")[0];
@SuppressWarnings("rawtypes") final CommandManager manager = this.manager;
final SimpleCommandMeta.Builder metaBuilder = SimpleCommandMeta.builder()
.with(this.metaFactory.apply(method));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* Used to declare a class method as a command method
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface CommandMethod {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import cloud.commandframework.meta.SimpleCommandMeta;
import io.leangen.geantyref.TypeToken;

import java.util.ArrayList;
import java.util.HashSet;

import java.util.Set;
Expand Down Expand Up @@ -91,7 +92,9 @@ void setup() {
(injector, builder) -> builder.argument(IntegerArgument.of(injector.value()))
);
/* Parse the class. Required for both testMethodConstruction() and testNamedSuggestionProvider() */
commands = annotationParser.parse(this);
commands = new ArrayList<>();
commands.addAll(annotationParser.parse(this));
commands.addAll(annotationParser.parse(new ClassCommandMethod()));
}

@Test
Expand All @@ -104,6 +107,7 @@ void testMethodConstruction() {
manager.executeCommand(new TestCommandSender(), "test 101").join());
manager.executeCommand(new TestCommandSender(), "flagcommand -p").join();
manager.executeCommand(new TestCommandSender(), "flagcommand --print --word peanut").join();
manager.executeCommand(new TestCommandSender(), "class method").join();
}

@Test
Expand Down Expand Up @@ -253,6 +257,16 @@ public void testInjectedParameters(
System.out.printf("Injected value: %s\n", injectableValue.toString());
}

@CommandMethod("class")
private static class ClassCommandMethod {

@CommandMethod("method")
public void annotatedMethod() {
System.out.println("kekw");
}

}

@CommandPermission("some.permission")
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
Expand Down