Add AbstractSWCommand.AllowNull #16

Zusammengeführt
YoyoNow hat 1 Commits von CMDAPIAnnotationAllowNull nach master 2022-08-28 21:03:07 +02:00 zusammengeführt
5 geänderte Dateien mit 124 neuen und 2 gelöschten Zeilen

Datei anzeigen

@ -76,7 +76,8 @@ public abstract class AbstractSWCommand<T> {
errors.forEach(Runnable::run);
return;
}
commandHelpList.stream().anyMatch(s -> s.invoke((ignore) -> {}, sender, alias, args));
commandHelpList.stream().anyMatch(s -> s.invoke((ignore) -> {
}, sender, alias, args));
}
} catch (CommandNoHelpException e) {
// Ignored
@ -361,4 +362,9 @@ public abstract class AbstractSWCommand<T> {
*/
boolean allowEAs() default true;
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
protected @interface AllowNull {
}
}

Datei anzeigen

@ -52,6 +52,9 @@ class CommandPart<T> {
@Setter
private boolean onlyUseIfNoneIsGiven = false;
@Setter
private boolean allowNullValues = false;
public CommandPart(AbstractSWCommand<T> command, AbstractTypeMapper<T, ?> typeMapper, AbstractValidator<T, Object> validator, Class<?> varArgType, String optional) {
this.command = command;
this.typeMapper = typeMapper;
@ -183,7 +186,7 @@ class CommandPart<T> {
return new CheckArgumentResult(false, null);
}
}
return new CheckArgumentResult(value != null, value);
return new CheckArgumentResult(allowNullValues || value != null, value);
} catch (Exception e) {
return new CheckArgumentResult(false, null);
}

Datei anzeigen

@ -94,9 +94,11 @@ public class SWCommandUtils {
AbstractValidator<T, Object> validator = (AbstractValidator<T, Object>) getValidator(parameter, localValidator);
Class<?> varArgType = parameter.isVarArgs() ? parameter.getType().getComponentType() : null;
AbstractSWCommand.OptionalValue optionalValue = parameter.getAnnotation(AbstractSWCommand.OptionalValue.class);
AbstractSWCommand.AllowNull allowNull = parameter.getAnnotation(AbstractSWCommand.AllowNull.class);
CommandPart<T> commandPart = new CommandPart<>(command, typeMapper, validator, varArgType, optionalValue != null ? optionalValue.value() : null);
commandPart.setOnlyUseIfNoneIsGiven(optionalValue != null && optionalValue.onlyUINIG());
commandPart.setAllowNullValues(allowNull != null);
if (current != null) {
current.setNext(commandPart);
}

Datei anzeigen

@ -0,0 +1,59 @@
/*
* 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.ExecutionIdentifier;
import de.steamwar.command.dto.TestSWCommand;
import de.steamwar.command.dto.TestTypeMapper;
import java.util.Collection;
public class NullMapperCommand extends TestSWCommand {
public NullMapperCommand() {
super("nullmapper");
}
@Register
public void test(String sender, @AllowNull String arg) {
if (arg == null) {
throw new ExecutionIdentifier("null");
}
throw new ExecutionIdentifier("notnull");
}
@ClassMapper(value = String.class, local = true)
public TestTypeMapper<String> typeMapper() {
return new TestTypeMapper<String>() {
@Override
public String map(String sender, String[] previousArguments, String s) {
if (s.equals("Hello World")) {
return null;
}
return s;
}
@Override
public Collection<String> tabCompletes(String sender, String[] previousArguments, String s) {
return null;
}
};
}
}

Datei anzeigen

@ -0,0 +1,52 @@
/*
* 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.ExecutionIdentifier;
import org.junit.Test;
import static de.steamwar.AssertionUtils.assertCMDFramework;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
public class NullMapperCommandTest {
@Test
public void testNull() {
NullMapperCommand command = new NullMapperCommand();
try {
command.execute("test", "", new String[] {"Hello World"});
assertThat(true, is(false));
} catch (Exception e) {
assertCMDFramework(e, ExecutionIdentifier.class, "null");
}
}
@Test
public void testNonNull() {
NullMapperCommand command = new NullMapperCommand();
try {
command.execute("test", "", new String[] {"Hello"});
assertThat(true, is(false));
} catch (Exception e) {
assertCMDFramework(e, ExecutionIdentifier.class, "notnull");
}
}
}