Merge branch 'master' into commonDb
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Dieser Commit ist enthalten in:
Lixfel 2022-09-20 16:41:50 +02:00
Commit 9043ab9dc5
9 geänderte Dateien mit 222 neuen und 42 gelöschten Zeilen

Datei anzeigen

@ -144,16 +144,20 @@ public abstract class AbstractSWCommand<T> {
for (Method method : methods) { for (Method method : methods) {
add(Register.class, method, i -> i > 0, true, null, (anno, parameters) -> { add(Register.class, method, i -> i > 0, true, null, (anno, parameters) -> {
if (!anno.help()) return; if (!anno.help()) return;
boolean error = false;
if (parameters.length != 2) { if (parameters.length != 2) {
commandSystemWarning(() -> "The method '" + method.toString() + "' is lacking parameters or has too many"); commandSystemWarning(() -> "The method '" + method.toString() + "' is lacking parameters or has too many");
error = true;
} }
if (!parameters[parameters.length - 1].isVarArgs()) { if (!parameters[parameters.length - 1].isVarArgs()) {
commandSystemWarning(() -> "The method '" + method.toString() + "' is lacking the varArgs parameters as last Argument"); commandSystemWarning(() -> "The method '" + method.toString() + "' is lacking the varArgs parameters as last Argument");
error = true;
} }
if (parameters[parameters.length - 1].getType().getComponentType() != String.class) { if (parameters[parameters.length - 1].getType().getComponentType() != String.class) {
commandSystemWarning(() -> "The method '" + method.toString() + "' is lacking the varArgs parameters of type '" + String.class.getTypeName() + "' as last Argument"); commandSystemWarning(() -> "The method '" + method.toString() + "' is lacking the varArgs parameters of type '" + String.class.getTypeName() + "' as last Argument");
return; error = true;
} }
if (error) return;
commandHelpList.add(new SubCommand<>(this, method, anno.value(), new HashMap<>(), new HashMap<>(), true, null, anno.noTabComplete())); commandHelpList.add(new SubCommand<>(this, method, anno.value(), new HashMap<>(), new HashMap<>(), true, null, anno.noTabComplete()));
}); });

Datei anzeigen

@ -21,39 +21,79 @@ package de.steamwar.command;
import java.io.PrintStream; import java.io.PrintStream;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.lang.reflect.Executable;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.function.Function;
import java.util.stream.Stream;
public class CommandFrameworkException extends RuntimeException { public class CommandFrameworkException extends RuntimeException {
private InvocationTargetException invocationTargetException; private Function causeMessage;
private String alias; private Throwable cause;
private String[] args; private Function stackTraceExtractor;
private String extraStackTraces;
private String message; private String message;
static CommandFrameworkException commandGetExceptions(String type, Class<?> clazzType, Executable executable, int index) {
return new CommandFrameworkException(throwable -> {
return CommandFrameworkException.class.getTypeName() + ": Error while getting " + type + " for " + clazzType.getTypeName() + " with parameter index " + index;
}, null, throwable -> Stream.empty(), executable.getDeclaringClass().getTypeName() + "." + executable.getName() + "(Unknown Source)");
}
static CommandFrameworkException commandPartExceptions(String type, Throwable cause, String current, Class<?> clazzType, Executable executable, int index) {
return new CommandFrameworkException(e -> {
return CommandFrameworkException.class.getTypeName() + ": Error while " + type + " (" + current + ") to type " + clazzType.getTypeName() + " with parameter index " + index;
}, cause, exception -> {
StackTraceElement[] stackTraceElements = exception.getStackTrace();
int last = 0;
for (int i = 0; i < stackTraceElements.length; i++) {
if (stackTraceElements[i].getClassName().equals(CommandPart.class.getTypeName())) {
last = i;
break;
}
}
return Arrays.stream(stackTraceElements).limit(last - 1);
}, executable.getDeclaringClass().getTypeName() + "." + executable.getName() + "(Unknown Source)");
}
CommandFrameworkException(InvocationTargetException invocationTargetException, String alias, String[] args) { CommandFrameworkException(InvocationTargetException invocationTargetException, String alias, String[] args) {
super(invocationTargetException); this(e -> {
this.invocationTargetException = invocationTargetException; StringBuilder st = new StringBuilder();
this.alias = alias; st.append(e.getCause().getClass().getTypeName());
this.args = args; if (e.getCause().getMessage() != null) {
st.append(": ").append(e.getCause().getMessage());
}
if (alias != null && !alias.isEmpty()) {
st.append("\n").append("Performed command: " + alias + " " + String.join(" ", args));
}
return st.toString();
}, invocationTargetException, e -> {
StackTraceElement[] stackTraceElements = e.getCause().getStackTrace();
return Arrays.stream(stackTraceElements).limit(stackTraceElements.length - e.getStackTrace().length);
}, null);
}
private <T extends Throwable> CommandFrameworkException(Function<T, String> causeMessage, T cause, Function<T, Stream<StackTraceElement>> stackTraceExtractor, String extraStackTraces) {
super(causeMessage.apply(cause), cause);
this.causeMessage = causeMessage;
this.cause = cause;
this.stackTraceExtractor = stackTraceExtractor;
this.extraStackTraces = extraStackTraces;
} }
public synchronized String getBuildStackTrace() { public synchronized String getBuildStackTrace() {
if (message != null) { if (message != null) {
return message; return message;
} }
StackTraceElement[] stackTraceElements = invocationTargetException.getCause().getStackTrace();
StringBuilder st = new StringBuilder(); StringBuilder st = new StringBuilder();
st.append(invocationTargetException.getCause().getClass().getTypeName()); st.append(causeMessage.apply(cause)).append("\n");
if (invocationTargetException.getCause().getMessage() != null) { ((Stream<StackTraceElement>) stackTraceExtractor.apply(cause)).forEach(stackTraceElement -> {
st.append(": ").append(invocationTargetException.getCause().getMessage()); st.append("\tat ").append(stackTraceElement.toString()).append("\n");
} });
st.append("\n"); if (extraStackTraces != null) {
if (alias != null && !alias.isEmpty()) { st.append("\tat ").append(extraStackTraces).append("\n");
st.append("Performed command: ").append(alias).append(" ").append(String.join(" ", args)).append("\n");
}
for (int i = 0; i < stackTraceElements.length - invocationTargetException.getStackTrace().length; i++) {
st.append("\tat ").append(stackTraceElements[i].toString()).append("\n");
} }
message = st.toString(); message = st.toString();
return message; return message;

Datei anzeigen

@ -23,6 +23,7 @@ import lombok.AllArgsConstructor;
import lombok.Setter; import lombok.Setter;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.lang.reflect.Parameter;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
@ -55,34 +56,31 @@ class CommandPart<T> {
@Setter @Setter
private boolean allowNullValues = false; private boolean allowNullValues = false;
public CommandPart(AbstractSWCommand<T> command, AbstractTypeMapper<T, ?> typeMapper, AbstractValidator<T, Object> validator, Class<?> varArgType, String optional) { private Parameter parameter;
private int parameterIndex;
public CommandPart(AbstractSWCommand<T> command, AbstractTypeMapper<T, ?> typeMapper, AbstractValidator<T, Object> validator, Class<?> varArgType, String optional, Parameter parameter, int parameterIndex) {
this.command = command; this.command = command;
this.typeMapper = typeMapper; this.typeMapper = typeMapper;
this.validator = validator; this.validator = validator;
this.varArgType = varArgType; this.varArgType = varArgType;
this.optional = optional; this.optional = optional;
this.parameter = parameter;
this.parameterIndex = parameterIndex;
validatePart(); validatePart();
} }
public void setNext(CommandPart<T> next) { public void setNext(CommandPart<T> next) {
if (varArgType != null) { if (varArgType != null) {
throw new IllegalArgumentException("There can't be a next part if this is a vararg part!"); throw new IllegalArgumentException("There can't be a next part if this is a vararg part! In method " + parameter.getDeclaringExecutable() + " with parameter " + parameterIndex);
} }
this.next = next; this.next = next;
} }
private void validatePart() { private void validatePart() {
if (optional != null && varArgType != null) { if (optional != null && varArgType != null) {
throw new IllegalArgumentException("A vararg part can't have an optional part!"); throw new IllegalArgumentException("A vararg part can't have an optional part! In method " + parameter.getDeclaringExecutable() + " with parameter " + parameterIndex);
}
if (optional != null) {
try {
typeMapper.map(null, EMPTY_ARRAY, optional);
} catch (Exception e) {
throw new IllegalArgumentException("The optional part is not valid!");
}
} }
} }
@ -113,7 +111,7 @@ class CommandPart<T> {
if (!ignoreAsArgument) { if (!ignoreAsArgument) {
if (!onlyUseIfNoneIsGiven) { if (!onlyUseIfNoneIsGiven) {
current.add(typeMapper.map(sender, EMPTY_ARRAY, optional)); current.add(typeMapper.map(sender, EMPTY_ARRAY, optional));
} else if(startIndex >= args.length) { } else if (startIndex >= args.length) {
current.add(typeMapper.map(sender, EMPTY_ARRAY, optional)); current.add(typeMapper.map(sender, EMPTY_ARRAY, optional));
} else { } else {
throw new CommandParseException(); throw new CommandParseException();
@ -170,14 +168,23 @@ class CommandPart<T> {
private Collection<String> tabCompletes(T sender, String[] args, int startIndex) { private Collection<String> tabCompletes(T sender, String[] args, int startIndex) {
return TabCompletionCache.tabComplete(sender, typeMapper, command, () -> { return TabCompletionCache.tabComplete(sender, typeMapper, command, () -> {
return typeMapper.tabCompletes(sender, Arrays.copyOf(args, startIndex), args[startIndex]); try {
return typeMapper.tabCompletes(sender, Arrays.copyOf(args, startIndex), args[startIndex]);
} catch (Throwable e) {
throw CommandFrameworkException.commandPartExceptions("tabcompleting", e, args[startIndex], (varArgType != null ? varArgType : parameter.getType()), parameter.getDeclaringExecutable(), parameterIndex);
}
}); });
} }
private CheckArgumentResult checkArgument(Consumer<Runnable> errors, T sender, String[] args, int index) { private CheckArgumentResult checkArgument(Consumer<Runnable> errors, T sender, String[] args, int index) {
Object value;
try { try {
Object value = typeMapper.map(sender, Arrays.copyOf(args, index), args[index]); value = typeMapper.map(sender, Arrays.copyOf(args, index), args[index]);
if (validator != null && errors != null) { } catch (Exception e) {
return new CheckArgumentResult(false, null);
}
if (validator != null && errors != null) {
try {
if (!validator.validate(sender, value, (s, objects) -> { if (!validator.validate(sender, value, (s, objects) -> {
errors.accept(() -> { errors.accept(() -> {
command.sendMessage(sender, s, objects); command.sendMessage(sender, s, objects);
@ -185,10 +192,10 @@ class CommandPart<T> {
})) { })) {
return new CheckArgumentResult(false, null); return new CheckArgumentResult(false, null);
} }
} catch (Throwable e) {
throw CommandFrameworkException.commandPartExceptions("validating", e, args[index], (varArgType != null ? varArgType : parameter.getType()), parameter.getDeclaringExecutable(), parameterIndex);
} }
return new CheckArgumentResult(allowNullValues || value != null, value);
} catch (Exception e) {
return new CheckArgumentResult(false, null);
} }
return new CheckArgumentResult(allowNullValues || value != null, value);
} }
} }

Datei anzeigen

@ -78,7 +78,7 @@ public class SWCommandUtils {
CommandPart<T> first = null; CommandPart<T> first = null;
CommandPart<T> current = null; CommandPart<T> current = null;
for (String s : subCommand) { for (String s : subCommand) {
CommandPart commandPart = new CommandPart(command, createMapper(s), null, null, null); CommandPart commandPart = new CommandPart(command, createMapper(s), null, null, null, null, -1);
commandPart.setIgnoreAsArgument(true); commandPart.setIgnoreAsArgument(true);
if (current != null) { if (current != null) {
current.setNext(commandPart); current.setNext(commandPart);
@ -96,7 +96,7 @@ public class SWCommandUtils {
AbstractSWCommand.OptionalValue optionalValue = parameter.getAnnotation(AbstractSWCommand.OptionalValue.class); AbstractSWCommand.OptionalValue optionalValue = parameter.getAnnotation(AbstractSWCommand.OptionalValue.class);
AbstractSWCommand.AllowNull allowNull = parameter.getAnnotation(AbstractSWCommand.AllowNull.class); AbstractSWCommand.AllowNull allowNull = parameter.getAnnotation(AbstractSWCommand.AllowNull.class);
CommandPart<T> commandPart = new CommandPart<>(command, typeMapper, validator, varArgType, optionalValue != null ? optionalValue.value() : null); CommandPart<T> commandPart = new CommandPart<>(command, typeMapper, validator, varArgType, optionalValue != null ? optionalValue.value() : null, parameter, i);
commandPart.setOnlyUseIfNoneIsGiven(optionalValue != null && optionalValue.onlyUINIG()); commandPart.setOnlyUseIfNoneIsGiven(optionalValue != null && optionalValue.onlyUINIG());
commandPart.setAllowNullValues(allowNull != null); commandPart.setAllowNullValues(allowNull != null);
if (current != null) { if (current != null) {

Datei anzeigen

@ -46,6 +46,11 @@ public class SubCommand<T> {
SubCommand(AbstractSWCommand<T> abstractSWCommand, Method method, String[] subCommand, Map<String, AbstractTypeMapper<T, ?>> localTypeMapper, Map<String, AbstractValidator<T, ?>> localValidator, boolean help, String[] description, boolean noTabComplete) { SubCommand(AbstractSWCommand<T> abstractSWCommand, Method method, String[] subCommand, Map<String, AbstractTypeMapper<T, ?>> localTypeMapper, Map<String, AbstractValidator<T, ?>> localValidator, boolean help, String[] description, boolean noTabComplete) {
this.abstractSWCommand = abstractSWCommand; this.abstractSWCommand = abstractSWCommand;
this.method = method; this.method = method;
try {
this.method.setAccessible(true);
} catch (SecurityException e) {
throw new SecurityException(e.getMessage(), e);
}
this.subCommand = subCommand; this.subCommand = subCommand;
this.description = description; this.description = description;
this.noTabComplete = noTabComplete; this.noTabComplete = noTabComplete;
@ -78,7 +83,6 @@ public class SubCommand<T> {
return false; return false;
} }
} }
method.setAccessible(true);
method.invoke(abstractSWCommand, senderFunction.apply(sender)); method.invoke(abstractSWCommand, senderFunction.apply(sender));
} else { } else {
List<Object> objects = new ArrayList<>(); List<Object> objects = new ArrayList<>();
@ -91,10 +95,9 @@ public class SubCommand<T> {
} }
} }
objects.add(0, senderFunction.apply(sender)); objects.add(0, senderFunction.apply(sender));
method.setAccessible(true);
method.invoke(abstractSWCommand, objects.toArray()); method.invoke(abstractSWCommand, objects.toArray());
} }
} catch (CommandNoHelpException e) { } catch (CommandNoHelpException | CommandFrameworkException e) {
throw e; throw e;
} catch (CommandParseException e) { } catch (CommandParseException e) {
return false; return false;

Datei anzeigen

@ -0,0 +1,57 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2022 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.command;
import de.steamwar.command.dto.TestSWCommand;
import de.steamwar.command.dto.TestTypeMapper;
import java.util.Collection;
public class BetterExceptionCommand extends TestSWCommand {
public BetterExceptionCommand() {
super("betterexception");
}
@Register
public void exceptionOnTabComplete(String s, @Mapper("exception") @Validator("exception") String s1) {
}
@Mapper("exception")
@Validator("exception")
public TestTypeMapper<String> tabCompleteException() {
return new TestTypeMapper<String>() {
@Override
public String map(String sender, String[] previousArguments, String s) {
return null;
}
@Override
public boolean validate(String sender, String value, MessageSender messageSender) {
throw new SecurityException();
}
@Override
public Collection<String> tabCompletes(String sender, String[] previousArguments, String s) {
throw new SecurityException();
}
};
}
}

Datei anzeigen

@ -0,0 +1,53 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2022 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.command;
import org.junit.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
public class BetterExceptionCommandTest {
@Test
public void testTabCompleteException() {
BetterExceptionCommand cmd = new BetterExceptionCommand();
try {
cmd.tabComplete("test", "", new String[]{""});
assert false;
} catch (Exception e) {
assertThat(e, is(instanceOf(CommandFrameworkException.class)));
assertThat(e.getMessage(), is("de.steamwar.command.CommandFrameworkException: Error while tabcompleting () to type java.lang.String with parameter index 1"));
}
}
@Test
public void testValidationException() {
BetterExceptionCommand cmd = new BetterExceptionCommand();
try {
cmd.execute("test", "", new String[]{""});
assert false;
} catch (Exception e) {
assertThat(e, is(instanceOf(CommandFrameworkException.class)));
assertThat(e.getMessage(), is("de.steamwar.command.CommandFrameworkException: Error while validating () to type java.lang.String with parameter index 1"));
}
}
}

Datei anzeigen

@ -19,6 +19,7 @@
package de.steamwar.command; package de.steamwar.command;
import de.steamwar.command.AbstractSWCommand.Register;
import de.steamwar.command.dto.ExecutionIdentifier; import de.steamwar.command.dto.ExecutionIdentifier;
import de.steamwar.command.dto.TestSWCommand; import de.steamwar.command.dto.TestSWCommand;
@ -28,6 +29,11 @@ public class SimpleCommand extends TestSWCommand {
super("simple"); super("simple");
} }
@Register(value = "a", help = true)
public void test(String s, String... varargs) {
throw new ExecutionIdentifier("RunSimple with Varargs");
}
@Register @Register
public void simple(String s) { public void simple(String s) {
throw new ExecutionIdentifier("RunSimple with noArgs"); throw new ExecutionIdentifier("RunSimple with noArgs");

Datei anzeigen

@ -39,6 +39,16 @@ public class SimpleCommandTest {
} }
} }
@Test
public void testVarArgs() {
SimpleCommand cmd = new SimpleCommand();
try {
cmd.execute("test", "", new String[] {"a", "b", "c"});
} catch (Exception e) {
assertCMDFramework(e, ExecutionIdentifier.class, "RunSimple with Varargs");
}
}
@Test @Test
public void testSimpleParsingNoResult() { public void testSimpleParsingNoResult() {
SimpleCommand cmd = new SimpleCommand(); SimpleCommand cmd = new SimpleCommand();