Add PartOf annotation for splitting big commands into multiple files #44
@ -34,6 +34,8 @@ import java.util.stream.Collectors;
|
||||
|
||||
public abstract class AbstractSWCommand<T> {
|
||||
|
||||
private static final Map<Class<AbstractSWCommand<?>>, List<AbstractSWCommand<?>>> dependencyMap = new HashMap<>();
|
||||
|
||||
private Class<?> clazz; // This is used in createMappings()
|
||||
|
||||
private boolean initialized = false;
|
||||
@ -48,6 +50,13 @@ public abstract class AbstractSWCommand<T> {
|
||||
|
||||
protected AbstractSWCommand(Class<T> clazz, String command, String... aliases) {
|
||||
this.clazz = clazz;
|
||||
|
||||
PartOf partOf = this.getClass().getAnnotation(PartOf.class);
|
||||
if (partOf != null) {
|
||||
dependencyMap.computeIfAbsent((Class<AbstractSWCommand<?>>) partOf.value(), k -> new ArrayList<>()).add(this);
|
||||
return;
|
||||
}
|
||||
|
||||
createAndSafeCommand(command, aliases);
|
||||
unregister();
|
||||
register();
|
||||
@ -139,6 +148,15 @@ public abstract class AbstractSWCommand<T> {
|
||||
});
|
||||
}
|
||||
|
||||
if (dependencyMap.containsKey(this.getClass())) {
|
||||
dependencyMap.get(this.getClass()).forEach(abstractSWCommand -> {
|
||||
abstractSWCommand.localTypeMapper.putAll((Map) localTypeMapper);
|
||||
abstractSWCommand.localValidators.putAll((Map) localValidators);
|
||||
abstractSWCommand.initialize();
|
||||
commandList.addAll((Collection) abstractSWCommand.commandList);
|
||||
});
|
||||
}
|
||||
|
||||
Collections.sort(commandList);
|
||||
initialized = true;
|
||||
}
|
||||
@ -237,6 +255,12 @@ public abstract class AbstractSWCommand<T> {
|
||||
return methods;
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.TYPE})
|
||||
public @interface PartOf {
|
||||
Class<?> value();
|
||||
}
|
||||
|
||||
// --- Annotation for the command ---
|
||||
|
||||
/**
|
||||
|
70
testsrc/de/steamwar/command/PartOfCommand.java
Normale Datei
70
testsrc/de/steamwar/command/PartOfCommand.java
Normale Datei
@ -0,0 +1,70 @@
|
||||
/*
|
||||
* 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.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
public class PartOfCommand {
|
||||
|
||||
public static class ParentCommand extends TestSWCommand {
|
||||
|
||||
public ParentCommand() {
|
||||
super("parent");
|
||||
}
|
||||
|
||||
@Register
|
||||
public void execute(String s, String... args) {
|
||||
throw new ExecutionIdentifier("ParentCommand with String...");
|
||||
}
|
||||
|
||||
@Mapper("test")
|
||||
public TestTypeMapper<Integer> typeMapper() {
|
||||
return new TestTypeMapper<Integer>() {
|
||||
@Override
|
||||
public Integer map(String sender, PreviousArguments previousArguments, String s) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<String> tabCompletes(String sender, PreviousArguments previousArguments, String s) {
|
||||
return Arrays.asList(s);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@AbstractSWCommand.PartOf(ParentCommand.class)
|
||||
public static class SubCommand extends TestSWCommand {
|
||||
|
||||
public SubCommand() {
|
||||
super(null);
|
||||
}
|
||||
|
||||
@Register
|
||||
public void execute(String s, @Mapper("test") int i) {
|
||||
throw new ExecutionIdentifier("SubCommand with int " + i);
|
||||
}
|
||||
}
|
||||
}
|
42
testsrc/de/steamwar/command/PartOfCommandTest.java
Normale Datei
42
testsrc/de/steamwar/command/PartOfCommandTest.java
Normale Datei
@ -0,0 +1,42 @@
|
||||
/*
|
||||
* 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 PartOfCommandTest {
|
||||
|
||||
@Test
|
||||
public void testMerging() {
|
||||
PartOfCommand.ParentCommand command = new PartOfCommand.ParentCommand();
|
||||
new PartOfCommand.SubCommand();
|
||||
try {
|
||||
command.execute("test", "", new String[]{"0"});
|
||||
assertThat(true, is(false));
|
||||
} catch (Exception e) {
|
||||
assertCMDFramework(e, ExecutionIdentifier.class, "SubCommand with int -1");
|
||||
}
|
||||
}
|
||||
}
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren