From c4a8ae4c9f5bf54a6f76360136e7cb4be5e42e9b Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sun, 4 Sep 2022 21:25:23 +0200 Subject: [PATCH] Update some stuff --- src/de/steamwar/command/CommandPart.java | 1 + src/de/steamwar/command/SubCommand.java | 7 +++++-- testsrc/de/steamwar/command/SimpleCommand.java | 6 ++++++ testsrc/de/steamwar/command/SimpleCommandTest.java | 10 ++++++++++ 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/de/steamwar/command/CommandPart.java b/src/de/steamwar/command/CommandPart.java index 80f0048..e3c7663 100644 --- a/src/de/steamwar/command/CommandPart.java +++ b/src/de/steamwar/command/CommandPart.java @@ -23,6 +23,7 @@ import lombok.AllArgsConstructor; import lombok.Setter; import java.lang.reflect.Array; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.List; diff --git a/src/de/steamwar/command/SubCommand.java b/src/de/steamwar/command/SubCommand.java index 9a497e5..71a4945 100644 --- a/src/de/steamwar/command/SubCommand.java +++ b/src/de/steamwar/command/SubCommand.java @@ -46,6 +46,11 @@ public class SubCommand { SubCommand(AbstractSWCommand abstractSWCommand, Method method, String[] subCommand, Map> localTypeMapper, Map> localValidator, boolean help, String[] description, boolean noTabComplete) { this.abstractSWCommand = abstractSWCommand; this.method = method; + try { + this.method.setAccessible(true); + } catch (SecurityException e) { + throw new SecurityException(e.getMessage(), e); + } this.subCommand = subCommand; this.description = description; this.noTabComplete = noTabComplete; @@ -78,7 +83,6 @@ public class SubCommand { return false; } } - method.setAccessible(true); method.invoke(abstractSWCommand, senderFunction.apply(sender)); } else { List objects = new ArrayList<>(); @@ -91,7 +95,6 @@ public class SubCommand { } } objects.add(0, senderFunction.apply(sender)); - method.setAccessible(true); method.invoke(abstractSWCommand, objects.toArray()); } } catch (CommandNoHelpException e) { diff --git a/testsrc/de/steamwar/command/SimpleCommand.java b/testsrc/de/steamwar/command/SimpleCommand.java index 16d565f..89cd016 100644 --- a/testsrc/de/steamwar/command/SimpleCommand.java +++ b/testsrc/de/steamwar/command/SimpleCommand.java @@ -19,6 +19,7 @@ package de.steamwar.command; +import de.steamwar.command.AbstractSWCommand.Register; import de.steamwar.command.dto.ExecutionIdentifier; import de.steamwar.command.dto.TestSWCommand; @@ -28,6 +29,11 @@ public class SimpleCommand extends TestSWCommand { super("simple"); } + @Register(help = true) + public void test(String s, String... varargs) { + throw new ExecutionIdentifier("RunSimple with Varargs"); + } + @Register public void simple(String s) { throw new ExecutionIdentifier("RunSimple with noArgs"); diff --git a/testsrc/de/steamwar/command/SimpleCommandTest.java b/testsrc/de/steamwar/command/SimpleCommandTest.java index dcb286c..aebd904 100644 --- a/testsrc/de/steamwar/command/SimpleCommandTest.java +++ b/testsrc/de/steamwar/command/SimpleCommandTest.java @@ -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 public void testSimpleParsingNoResult() { SimpleCommand cmd = new SimpleCommand();