Improve the annotated command method code and add more supported annotations

This commit is contained in:
Alexander Söderberg 2020-09-18 21:30:00 +02:00
parent 1e58ca3f13
commit 3f852d068e
No known key found for this signature in database
GPG key ID: C0207FF7EA146678
15 changed files with 392 additions and 59 deletions

View file

@ -30,6 +30,7 @@ import com.intellectualsites.commands.CommandManager;
import com.intellectualsites.commands.arguments.CommandArgument;
import com.intellectualsites.commands.arguments.parser.ArgumentParser;
import com.intellectualsites.commands.arguments.parser.ParserParameters;
import com.intellectualsites.commands.arguments.parser.StandardParameters;
import com.intellectualsites.commands.execution.CommandExecutionHandler;
import com.intellectualsites.commands.meta.CommandMeta;
@ -42,11 +43,11 @@ import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.regex.Pattern;
@ -58,20 +59,63 @@ import java.util.regex.Pattern;
*/
public final class AnnotationParser<C, M extends CommandMeta> {
private static final Predicate<String> PATTERN_ARGUMENT_LITERAL = Pattern.compile("([A-Za-z0-9]+)(|([A-Za-z0-9]+))*")
private static final Predicate<String> PATTERN_ARGUMENT_LITERAL = Pattern.compile("([A-Za-z0-9]+)(|([A-Za-z0-9]+))*")
.asPredicate();
private static final Predicate<String> PATTERN_ARGUMENT_REQUIRED = Pattern.compile("<([A-Za-z0-9]+)>")
.asPredicate();
private static final Predicate<String> PATTERN_ARGUMENT_OPTIONAL = Pattern.compile("\\[([A-Za-z0-9]+)]")
.asPredicate();
private static final Predicate<String> PATTERN_ARGUMENT_REQUIRED = Pattern.compile("<([A-Za-z0-9]+)>").asPredicate();
private static final Predicate<String> PATTERN_ARGUMENT_OPTIONAL = Pattern.compile("\\[([A-Za-z0-9]+)]").asPredicate();
private final Function<ParserParameters, M> metaMapper;
private final CommandManager<C, M> manager;
private final Map<Class<? extends Annotation>, Function<? extends Annotation, ParserParameters>> annotationMappers;
private final Class<C> commandSenderClass;
/**
* Construct a new annotation parser
*
* @param manager Command manager instance
* @param manager Command manager instance
* @param commandSenderClass Command sender class
* @param metaMapper Function that is used to create {@link CommandMeta} instances from annotations on the
* command methods. These annotations will be mapped to
* {@link com.intellectualsites.commands.arguments.parser.ParserParameter}. Mappers for the
* parser parameters can be registered using {@link #registerAnnotationMapper(Class, Function)}
*/
public AnnotationParser(@Nonnull final CommandManager<C, M> manager) {
public AnnotationParser(@Nonnull final CommandManager<C, M> manager,
@Nonnull final Class<C> commandSenderClass,
@Nonnull final Function<ParserParameters, M> metaMapper) {
this.commandSenderClass = commandSenderClass;
this.manager = manager;
this.metaMapper = metaMapper;
this.annotationMappers = Maps.newHashMap();
this.registerAnnotationMapper(Description.class, d -> ParserParameters.single(StandardParameters.DESCRIPTION, d.value()));
}
/**
* Register an annotation mapper
*
* @param annotation Annotation class
* @param mapper Mapping function
* @param <A> Annotation type
*/
public <A extends Annotation> void registerAnnotationMapper(@Nonnull final Class<A> annotation,
@Nonnull final Function<A, ParserParameters> mapper) {
this.annotationMappers.put(annotation, mapper);
}
@Nonnull
private M createMeta(@Nonnull final Annotation[] annotations) {
final ParserParameters parameters = ParserParameters.empty();
for (final Annotation annotation : annotations) {
@SuppressWarnings("ALL") final Function function = this.annotationMappers.get(annotation.annotationType());
if (function == null) {
continue;
}
//noinspection unchecked
parameters.merge((ParserParameters) function.apply(annotation));
}
return this.metaMapper.apply(parameters);
}
/**
@ -84,13 +128,16 @@ public final class AnnotationParser<C, M extends CommandMeta> {
*/
@Nonnull
public <T> Collection<Command<C, M>> parse(@Nonnull final T instance) {
final Method[] methods = instance.getClass().getMethods();
final Method[] methods = instance.getClass().getDeclaredMethods();
final Collection<CommandMethodPair> commandMethodPairs = new ArrayList<>();
for (final Method method : methods) {
final CommandMethod commandMethod = method.getAnnotation(CommandMethod.class);
if (commandMethod == null) {
continue;
}
if (!method.isAccessible()) {
method.setAccessible(true);
}
if (method.getReturnType() != Void.TYPE) {
throw new IllegalArgumentException(String.format("@CommandMethod annotated method '%s' has non-void return type",
method.getName()));
@ -112,13 +159,13 @@ public final class AnnotationParser<C, M extends CommandMeta> {
for (final CommandMethodPair commandMethodPair : methodPairs) {
final CommandMethod commandMethod = commandMethodPair.getCommandMethod();
final Method method = commandMethodPair.getMethod();
final LinkedHashMap<String, ArgumentMode> tokens = this.parseSyntax(commandMethod.value());
final LinkedHashMap<String, SyntaxFragment> tokens = this.parseSyntax(commandMethod.value());
/* Determine command name */
final String commandToken = commandMethod.value().split(" ")[0].split("\\|")[0];
@SuppressWarnings("ALL")
Command.Builder builder = this.manager.commandBuilder(commandToken,
Collections.emptyList(),
manager.createDefaultCommandMeta());
tokens.get(commandToken).getMinor(),
this.createMeta(method.getAnnotations()));
final Collection<ArgumentParameterPair> arguments = this.getArguments(method);
final Map<String, CommandArgument<C, ?>> commandArguments = Maps.newHashMap();
/* Go through all annotated parameters and build up the argument tree */
@ -130,13 +177,13 @@ public final class AnnotationParser<C, M extends CommandMeta> {
}
boolean commandNameFound = false;
/* Build the command tree */
for (final Map.Entry<String, ArgumentMode> entry : tokens.entrySet()) {
for (final Map.Entry<String, SyntaxFragment> entry : tokens.entrySet()) {
if (!commandNameFound) {
commandNameFound = true;
continue;
}
if (entry.getValue() == ArgumentMode.LITERAL) {
builder = builder.literal(entry.getKey());
if (entry.getValue().getArgumentMode() == ArgumentMode.LITERAL) {
builder = builder.literal(entry.getKey(), entry.getValue().getMinor().toArray(new String[0]));
} else {
final CommandArgument<C, ?> argument = commandArguments.get(entry.getKey());
if (argument == null) {
@ -148,8 +195,24 @@ public final class AnnotationParser<C, M extends CommandMeta> {
builder = builder.argument(argument);
}
}
/* Try to find the command sender type */
Class<? extends C> senderType = null;
for (final Parameter parameter : method.getParameters()) {
if (parameter.isAnnotationPresent(Argument.class)) {
continue;
}
if (this.commandSenderClass.isAssignableFrom(parameter.getType())) {
senderType = (Class<? extends C>) parameter.getType();
break;
}
}
/* Decorate command data */
builder = builder.withPermission(commandMethod.permission()).withSenderType(commandMethod.requiredSender());
builder = builder.withPermission(commandMethod.permission());
if (commandMethod.requiredSender() != Object.class) {
builder = builder.withSenderType(commandMethod.requiredSender());
} else if (senderType != null) {
builder = builder.withSenderType(senderType);
}
/* Construct the handler */
final CommandExecutionHandler<C> commandExecutionHandler = commandContext -> {
final List<Object> parameters = new ArrayList<>(method.getParameterCount());
@ -189,7 +252,7 @@ public final class AnnotationParser<C, M extends CommandMeta> {
@Nonnull
@SuppressWarnings("unchecked")
private CommandArgument<C, ?> buildArgument(@Nonnull final Method method,
@Nullable final ArgumentMode argumentMode,
@Nullable final SyntaxFragment syntaxFragment,
@Nonnull final ArgumentParameterPair argumentPair) {
final Parameter parameter = argumentPair.getParameter();
final Collection<Annotation> annotations = Arrays.asList(parameter.getAnnotations());
@ -204,16 +267,15 @@ public final class AnnotationParser<C, M extends CommandMeta> {
+ "for that type",
parameter.getName(), method.getName(),
token.toString())));
if (argumentMode == null || argumentMode == ArgumentMode.LITERAL) {
if (syntaxFragment == null || syntaxFragment.getArgumentMode() == ArgumentMode.LITERAL) {
throw new IllegalArgumentException(String.format(
"Invalid command argument '%s' in method '%s': "
+ "Missing syntax mapping", argumentPair.getArgument().value(), method.getName()));
+ "Missing syntax mapping", argumentPair.getArgument().value(), method.getName()));
}
final Argument argument = argumentPair.getArgument();
@SuppressWarnings("ALL")
final CommandArgument.Builder argumentBuilder = CommandArgument.ofType(parameter.getType(),
argument.value());
if (argumentMode == ArgumentMode.OPTIONAL) {
@SuppressWarnings("ALL") final CommandArgument.Builder argumentBuilder = CommandArgument.ofType(parameter.getType(),
argument.value());
if (syntaxFragment.getArgumentMode() == ArgumentMode.OPTIONAL) {
if (argument.defaultValue().isEmpty()) {
argumentBuilder.asOptional();
} else {
@ -226,22 +288,30 @@ public final class AnnotationParser<C, M extends CommandMeta> {
}
@Nonnull
LinkedHashMap<String, ArgumentMode> parseSyntax(@Nonnull final String syntax) {
LinkedHashMap<String, SyntaxFragment> parseSyntax(@Nonnull final String syntax) {
final StringTokenizer stringTokenizer = new StringTokenizer(syntax, " ");
final LinkedHashMap<String, ArgumentMode> map = new LinkedHashMap<>();
final LinkedHashMap<String, SyntaxFragment> map = new LinkedHashMap<>();
while (stringTokenizer.hasMoreTokens()) {
final String token = stringTokenizer.nextToken();
String major;
List<String> minor = new ArrayList<>();
ArgumentMode mode;
if (PATTERN_ARGUMENT_REQUIRED.test(token)) {
map.put(token.substring(1, token.length() - 1), ArgumentMode.REQUIRED);
major = token.substring(1, token.length() - 1);
mode = ArgumentMode.REQUIRED;
} else if (PATTERN_ARGUMENT_OPTIONAL.test(token)) {
map.put(token.substring(1, token.length() - 1), ArgumentMode.OPTIONAL);
major = token.substring(1, token.length() - 1);
mode = ArgumentMode.OPTIONAL;
} else if (PATTERN_ARGUMENT_LITERAL.test(token)) {
final String[] literals = token.split("\\|");
/* Actually use the other literals as well */
map.put(literals[0], ArgumentMode.LITERAL);
major = literals[0];
minor.addAll(Arrays.asList(literals).subList(1, literals.length));
mode = ArgumentMode.LITERAL;
} else {
throw new IllegalArgumentException(String.format("Unrecognizable syntax token '%s'", syntax));
}
map.put(major, new SyntaxFragment(major, minor, mode));
}
return map;
}
@ -306,7 +376,41 @@ public final class AnnotationParser<C, M extends CommandMeta> {
enum ArgumentMode {
LITERAL, OPTIONAL, REQUIRED
LITERAL,
OPTIONAL,
REQUIRED
}
private static final class SyntaxFragment {
private final String major;
private final List<String> minor;
private final ArgumentMode argumentMode;
private SyntaxFragment(@Nonnull final String major,
@Nonnull final List<String> minor,
@Nonnull final ArgumentMode argumentMode) {
this.major = major;
this.minor = minor;
this.argumentMode = argumentMode;
}
@Nonnull
private String getMajor() {
return this.major;
}
@Nonnull
private List<String> getMinor() {
return this.minor;
}
@Nonnull
private ArgumentMode getArgumentMode() {
return this.argumentMode;
}
}
}

View file

@ -0,0 +1,45 @@
//
// MIT License
//
// Copyright (c) 2020 Alexander Söderberg
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package com.intellectualsites.commands.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Maps to {@link com.intellectualsites.commands.arguments.parser.StandardParameters#DESCRIPTION}
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Description {
/**
* Command description
*
* @return Command syntax
*/
String value() default "";
}

View file

@ -23,7 +23,6 @@
//
package com.intellectualsites.commands.annotations;
import com.google.common.collect.Maps;
import com.intellectualsites.commands.Command;
import com.intellectualsites.commands.CommandManager;
import com.intellectualsites.commands.annotations.specifier.Range;
@ -34,7 +33,6 @@ import org.junit.jupiter.api.Test;
import javax.annotation.Nonnull;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.CompletionException;
class AnnotationParserTest {
@ -45,18 +43,7 @@ class AnnotationParserTest {
@BeforeAll
static void setup() {
manager = new TestCommandManager();
annotationParser = new AnnotationParser<>(manager);
}
@Test
void testSyntaxParsing() {
final String text = "literal <required> [optional]";
final Map<String, AnnotationParser.ArgumentMode> arguments = annotationParser.parseSyntax(text);
final Map<String, AnnotationParser.ArgumentMode> map = Maps.newLinkedHashMap();
map.put("literal", AnnotationParser.ArgumentMode.LITERAL);
map.put("required", AnnotationParser.ArgumentMode.REQUIRED);
map.put("optional", AnnotationParser.ArgumentMode.OPTIONAL);
Assertions.assertEquals(map, arguments);
annotationParser = new AnnotationParser<>(manager, TestCommandSender.class, p -> SimpleCommandMeta.empty());
}
@Test
@ -64,11 +51,12 @@ class AnnotationParserTest {
final Collection<Command<TestCommandSender, SimpleCommandMeta>> commands = annotationParser.parse(this);
Assertions.assertFalse(commands.isEmpty());
manager.executeCommand(new TestCommandSender(), "test 10").join();
manager.executeCommand(new TestCommandSender(), "t 10").join();
Assertions.assertThrows(CompletionException.class, () ->
manager.executeCommand(new TestCommandSender(), "test 101").join());
}
@CommandMethod("test <int> [string]")
@CommandMethod("test|t <int> [string]")
public void testCommand(@Nonnull final TestCommandSender sender,
@Argument("int") @Range(max = "100") final int argument,
@Nonnull @Argument(value = "string", defaultValue = "potato") final String string) {