From 5d53ede5e8e7b707f10144f86d419fe4d6cf3366 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 18 Feb 2023 12:05:03 +0100 Subject: [PATCH 1/3] Add PartOf annotation for splitting big commands into multiple files --- .../steamwar/command/AbstractSWCommand.java | 24 +++++++ .../de/steamwar/command/PartOfCommand.java | 70 +++++++++++++++++++ .../steamwar/command/PartOfCommandTest.java | 43 ++++++++++++ 3 files changed, 137 insertions(+) create mode 100644 testsrc/de/steamwar/command/PartOfCommand.java create mode 100644 testsrc/de/steamwar/command/PartOfCommandTest.java diff --git a/src/de/steamwar/command/AbstractSWCommand.java b/src/de/steamwar/command/AbstractSWCommand.java index 84ae620..7ba5e62 100644 --- a/src/de/steamwar/command/AbstractSWCommand.java +++ b/src/de/steamwar/command/AbstractSWCommand.java @@ -34,6 +34,8 @@ import java.util.stream.Collectors; public abstract class AbstractSWCommand { + private static Map>, List>> dependencyMap = new HashMap<>(); + private Class clazz; // This is used in createMappings() private boolean initialized = false; @@ -48,6 +50,13 @@ public abstract class AbstractSWCommand { protected AbstractSWCommand(Class clazz, String command, String... aliases) { this.clazz = clazz; + + PartOf partOf = this.getClass().getAnnotation(PartOf.class); + if (partOf != null) { + dependencyMap.computeIfAbsent((Class>) partOf.value(), k -> new ArrayList<>()).add(this); + return; + } + createAndSafeCommand(command, aliases); unregister(); register(); @@ -139,6 +148,15 @@ public abstract class AbstractSWCommand { }); } + 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 { return methods; } + @Retention(RetentionPolicy.RUNTIME) + @Target({ElementType.TYPE}) + public @interface PartOf { + Class value(); + } + // --- Annotation for the command --- /** diff --git a/testsrc/de/steamwar/command/PartOfCommand.java b/testsrc/de/steamwar/command/PartOfCommand.java new file mode 100644 index 0000000..4f2fe9a --- /dev/null +++ b/testsrc/de/steamwar/command/PartOfCommand.java @@ -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 . + */ + +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 typeMapper() { + return new TestTypeMapper() { + @Override + public Integer map(String sender, PreviousArguments previousArguments, String s) { + return Integer.parseInt(s); + } + + @Override + public Collection 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"); + } + } +} diff --git a/testsrc/de/steamwar/command/PartOfCommandTest.java b/testsrc/de/steamwar/command/PartOfCommandTest.java new file mode 100644 index 0000000..b02614a --- /dev/null +++ b/testsrc/de/steamwar/command/PartOfCommandTest.java @@ -0,0 +1,43 @@ +/* + * 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 . + */ + +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) { + e.printStackTrace(); + assertCMDFramework(e, ExecutionIdentifier.class, "SubCommand with int"); + } + } +} -- 2.39.5 From 241117a6a65b95bfcee9bd4b784d344f635a78f9 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 18 Feb 2023 12:13:20 +0100 Subject: [PATCH 2/3] Fix test --- testsrc/de/steamwar/command/PartOfCommand.java | 4 ++-- testsrc/de/steamwar/command/PartOfCommandTest.java | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/testsrc/de/steamwar/command/PartOfCommand.java b/testsrc/de/steamwar/command/PartOfCommand.java index 4f2fe9a..9c66a4b 100644 --- a/testsrc/de/steamwar/command/PartOfCommand.java +++ b/testsrc/de/steamwar/command/PartOfCommand.java @@ -44,7 +44,7 @@ public class PartOfCommand { return new TestTypeMapper() { @Override public Integer map(String sender, PreviousArguments previousArguments, String s) { - return Integer.parseInt(s); + return -1; } @Override @@ -64,7 +64,7 @@ public class PartOfCommand { @Register public void execute(String s, @Mapper("test") int i) { - throw new ExecutionIdentifier("SubCommand with int"); + throw new ExecutionIdentifier("SubCommand with int " + i); } } } diff --git a/testsrc/de/steamwar/command/PartOfCommandTest.java b/testsrc/de/steamwar/command/PartOfCommandTest.java index b02614a..726384e 100644 --- a/testsrc/de/steamwar/command/PartOfCommandTest.java +++ b/testsrc/de/steamwar/command/PartOfCommandTest.java @@ -36,8 +36,7 @@ public class PartOfCommandTest { command.execute("test", "", new String[]{"0"}); assertThat(true, is(false)); } catch (Exception e) { - e.printStackTrace(); - assertCMDFramework(e, ExecutionIdentifier.class, "SubCommand with int"); + assertCMDFramework(e, ExecutionIdentifier.class, "SubCommand with int -1"); } } } -- 2.39.5 From 69cff99801238984fd463de390b581f91f18a5d1 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 18 Feb 2023 14:01:14 +0100 Subject: [PATCH 3/3] Make Map final --- src/de/steamwar/command/AbstractSWCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/de/steamwar/command/AbstractSWCommand.java b/src/de/steamwar/command/AbstractSWCommand.java index 7ba5e62..b8dc078 100644 --- a/src/de/steamwar/command/AbstractSWCommand.java +++ b/src/de/steamwar/command/AbstractSWCommand.java @@ -34,7 +34,7 @@ import java.util.stream.Collectors; public abstract class AbstractSWCommand { - private static Map>, List>> dependencyMap = new HashMap<>(); + private static final Map>, List>> dependencyMap = new HashMap<>(); private Class clazz; // This is used in createMappings() -- 2.39.5