From 582e0f0bae80d46d54a5ea6b4113346ba88d76cc Mon Sep 17 00:00:00 2001 From: tueem Date: Sun, 24 Nov 2024 00:02:19 +0100 Subject: [PATCH] implement AnnotationParser system --- .../methods/ButtonInteractionMethod.java | 17 ++--- .../methods/InteractionMethod.java | 6 ++ .../SlashCommandInteractionMethod.java | 34 ++++------ .../marinara/parser/AnnotationParser.java | 8 +++ .../marinara/parser/ButtonParser.java | 29 +++++++++ .../marinara/parser/SlashCommandParser.java | 63 +++++++++++++++++++ .../marinara/util/ReflectionUtil.java | 18 ------ 7 files changed, 126 insertions(+), 49 deletions(-) create mode 100644 lib/src/main/java/net/tomatentum/marinara/parser/AnnotationParser.java create mode 100644 lib/src/main/java/net/tomatentum/marinara/parser/ButtonParser.java create mode 100644 lib/src/main/java/net/tomatentum/marinara/parser/SlashCommandParser.java diff --git a/lib/src/main/java/net/tomatentum/marinara/interaction/methods/ButtonInteractionMethod.java b/lib/src/main/java/net/tomatentum/marinara/interaction/methods/ButtonInteractionMethod.java index baf8f4e..c366b48 100644 --- a/lib/src/main/java/net/tomatentum/marinara/interaction/methods/ButtonInteractionMethod.java +++ b/lib/src/main/java/net/tomatentum/marinara/interaction/methods/ButtonInteractionMethod.java @@ -4,7 +4,8 @@ import java.lang.reflect.Method; import net.tomatentum.marinara.interaction.InteractionHandler; import net.tomatentum.marinara.interaction.InteractionType; -import net.tomatentum.marinara.interaction.annotation.Button; +import net.tomatentum.marinara.parser.AnnotationParser; +import net.tomatentum.marinara.parser.ButtonParser; import net.tomatentum.marinara.wrapper.LibraryWrapper; public class ButtonInteractionMethod extends InteractionMethod { @@ -13,7 +14,13 @@ public class ButtonInteractionMethod extends InteractionMethod { ButtonInteractionMethod(Method method, InteractionHandler handler, LibraryWrapper wrapper) { super(method, handler, wrapper); - parseMethod(); + } + + @Override + public AnnotationParser[] getParsers() { + return new AnnotationParser[] { + new ButtonParser(method, (x) -> { this.customId = x; } ) + }; } @Override @@ -31,10 +38,4 @@ public class ButtonInteractionMethod extends InteractionMethod { public InteractionType getType() { return InteractionType.BUTTON; } - - private void parseMethod() { - Button button = getMethod().getAnnotation(Button.class); - this.customId = button.value(); - } - } diff --git a/lib/src/main/java/net/tomatentum/marinara/interaction/methods/InteractionMethod.java b/lib/src/main/java/net/tomatentum/marinara/interaction/methods/InteractionMethod.java index 0f6b09e..ca8d263 100644 --- a/lib/src/main/java/net/tomatentum/marinara/interaction/methods/InteractionMethod.java +++ b/lib/src/main/java/net/tomatentum/marinara/interaction/methods/InteractionMethod.java @@ -12,6 +12,7 @@ import net.tomatentum.marinara.interaction.InteractionType; import net.tomatentum.marinara.interaction.annotation.Button; import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand; import net.tomatentum.marinara.interaction.commands.annotation.SubCommand; +import net.tomatentum.marinara.parser.AnnotationParser; import net.tomatentum.marinara.wrapper.LibraryWrapper; public abstract class InteractionMethod { @@ -27,6 +28,7 @@ public abstract class InteractionMethod { protected Method method; protected InteractionHandler handler; protected LibraryWrapper wrapper; + protected AnnotationParser[] parsers; protected InteractionMethod(Method method, InteractionHandler handler, LibraryWrapper wrapper) { if (!Arrays.asList(handler.getClass().getMethods()).contains(method)) @@ -34,8 +36,12 @@ public abstract class InteractionMethod { this.method = method; this.handler = handler; this.wrapper = wrapper; + this.parsers = getParsers(); + Arrays.stream(parsers).forEach(AnnotationParser::parse); } + public abstract AnnotationParser[] getParsers(); + public abstract Object getParameter(Object parameter, int index); public abstract boolean canRun(Object context); diff --git a/lib/src/main/java/net/tomatentum/marinara/interaction/methods/SlashCommandInteractionMethod.java b/lib/src/main/java/net/tomatentum/marinara/interaction/methods/SlashCommandInteractionMethod.java index 6498078..cd63d69 100644 --- a/lib/src/main/java/net/tomatentum/marinara/interaction/methods/SlashCommandInteractionMethod.java +++ b/lib/src/main/java/net/tomatentum/marinara/interaction/methods/SlashCommandInteractionMethod.java @@ -5,10 +5,8 @@ import java.lang.reflect.Method; import net.tomatentum.marinara.interaction.InteractionHandler; import net.tomatentum.marinara.interaction.InteractionType; import net.tomatentum.marinara.interaction.commands.ExecutableSlashCommandDefinition; -import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand; -import net.tomatentum.marinara.interaction.commands.annotation.SubCommand; -import net.tomatentum.marinara.interaction.commands.annotation.SubCommandGroup; -import net.tomatentum.marinara.util.ReflectionUtil; +import net.tomatentum.marinara.parser.AnnotationParser; +import net.tomatentum.marinara.parser.SlashCommandParser; import net.tomatentum.marinara.wrapper.LibraryWrapper; public class SlashCommandInteractionMethod extends InteractionMethod { @@ -17,7 +15,13 @@ public class SlashCommandInteractionMethod extends InteractionMethod { SlashCommandInteractionMethod(Method method, InteractionHandler handler, LibraryWrapper wrapper) { super(method, handler, wrapper); - parseMethod(); + } + + @Override + public AnnotationParser[] getParsers() { + return new AnnotationParser[] { + new SlashCommandParser(method, (x) -> { this.commandDefinition = x; } ) + }; } @Override @@ -40,24 +44,8 @@ public class SlashCommandInteractionMethod extends InteractionMethod { return commandDefinition; } - private void parseMethod() { - ReflectionUtil.checkValidCommandMethod(method); - - SlashCommand cmd = ReflectionUtil.getAnnotation(method, SlashCommand.class); - ExecutableSlashCommandDefinition.Builder builder = new ExecutableSlashCommandDefinition.Builder(); - builder.setApplicationCommand(cmd); - - if (ReflectionUtil.isAnnotationPresent(method, SubCommandGroup.class)) { - SubCommandGroup cmdGroup = ReflectionUtil.getAnnotation(method, SubCommandGroup.class); - builder.setSubCommandGroup(cmdGroup); - } - - if (ReflectionUtil.isAnnotationPresent(method, SubCommand.class)) { - SubCommand subCmd = ReflectionUtil.getAnnotation(method, SubCommand.class); - builder.setSubCommand(subCmd); - } - - this.commandDefinition = builder.build(); + public void setCommandDefinition(ExecutableSlashCommandDefinition commandDefinition) { + this.commandDefinition = commandDefinition; } } diff --git a/lib/src/main/java/net/tomatentum/marinara/parser/AnnotationParser.java b/lib/src/main/java/net/tomatentum/marinara/parser/AnnotationParser.java new file mode 100644 index 0000000..187a4a4 --- /dev/null +++ b/lib/src/main/java/net/tomatentum/marinara/parser/AnnotationParser.java @@ -0,0 +1,8 @@ +package net.tomatentum.marinara.parser; + +import java.lang.reflect.Method; + +public interface AnnotationParser { + void parse(); + Method getMethod(); +} diff --git a/lib/src/main/java/net/tomatentum/marinara/parser/ButtonParser.java b/lib/src/main/java/net/tomatentum/marinara/parser/ButtonParser.java new file mode 100644 index 0000000..1b7dcbb --- /dev/null +++ b/lib/src/main/java/net/tomatentum/marinara/parser/ButtonParser.java @@ -0,0 +1,29 @@ +package net.tomatentum.marinara.parser; + +import java.lang.reflect.Method; +import java.util.function.Consumer; + +import net.tomatentum.marinara.interaction.annotation.Button; + +public class ButtonParser implements AnnotationParser { + + private Method method; + private Consumer consumer; + + public ButtonParser(Method method, Consumer consumer) { + this.method = method; + this.consumer = consumer; + } + + @Override + public void parse() { + Button button = getMethod().getAnnotation(Button.class); + this.consumer.accept(button.value()); + } + + @Override + public Method getMethod() { + return this.method; + } + +} diff --git a/lib/src/main/java/net/tomatentum/marinara/parser/SlashCommandParser.java b/lib/src/main/java/net/tomatentum/marinara/parser/SlashCommandParser.java new file mode 100644 index 0000000..8997d9f --- /dev/null +++ b/lib/src/main/java/net/tomatentum/marinara/parser/SlashCommandParser.java @@ -0,0 +1,63 @@ +package net.tomatentum.marinara.parser; + +import java.lang.reflect.Method; +import java.util.function.Consumer; + +import net.tomatentum.marinara.interaction.commands.ExecutableSlashCommandDefinition; +import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand; +import net.tomatentum.marinara.interaction.commands.annotation.SubCommand; +import net.tomatentum.marinara.interaction.commands.annotation.SubCommandGroup; +import net.tomatentum.marinara.util.ReflectionUtil; + +public class SlashCommandParser implements AnnotationParser { + + private Method method; + private Consumer consumer; + + public SlashCommandParser(Method method, Consumer consumer) { + this.method = method; + this.consumer = consumer; + } + + @Override + public void parse() { + this.checkValidCommandMethod(method); + + SlashCommand cmd = ReflectionUtil.getAnnotation(method, SlashCommand.class); + ExecutableSlashCommandDefinition.Builder builder = new ExecutableSlashCommandDefinition.Builder(); + builder.setApplicationCommand(cmd); + + if (ReflectionUtil.isAnnotationPresent(method, SubCommandGroup.class)) { + SubCommandGroup cmdGroup = ReflectionUtil.getAnnotation(method, SubCommandGroup.class); + builder.setSubCommandGroup(cmdGroup); + } + + if (ReflectionUtil.isAnnotationPresent(method, SubCommand.class)) { + SubCommand subCmd = ReflectionUtil.getAnnotation(method, SubCommand.class); + builder.setSubCommand(subCmd); + } + + consumer.accept(builder.build()); + } + + @Override + public Method getMethod() { + return this.method; + } + + private void checkValidCommandMethod(Method method) { + if (method.isAnnotationPresent(SlashCommand.class) && + method.getDeclaringClass().isAnnotationPresent(SlashCommand.class)) { + throw new RuntimeException(method.getName() + ": Can't have ApplicationCommand Annotation on Class and Method"); + } + + if (!ReflectionUtil.isAnnotationPresent(method, SlashCommand.class)) + throw new RuntimeException(method.getName() + ": Missing ApplicationCommand Annotation on either Class or Method"); + + if ((method.isAnnotationPresent(SubCommand.class) && + !ReflectionUtil.isAnnotationPresent(method, SlashCommand.class))) { + throw new RuntimeException(method.getName() + ": Missing ApplicationCommand Annotation on either Method or Class"); + } + } + +} diff --git a/lib/src/main/java/net/tomatentum/marinara/util/ReflectionUtil.java b/lib/src/main/java/net/tomatentum/marinara/util/ReflectionUtil.java index 63105bf..0c7f96e 100644 --- a/lib/src/main/java/net/tomatentum/marinara/util/ReflectionUtil.java +++ b/lib/src/main/java/net/tomatentum/marinara/util/ReflectionUtil.java @@ -3,9 +3,6 @@ package net.tomatentum.marinara.util; import java.lang.annotation.Annotation; import java.lang.reflect.Method; -import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand; -import net.tomatentum.marinara.interaction.commands.annotation.SubCommand; - public final class ReflectionUtil { public static boolean isAnnotationPresent(Method method, Class annotationClass) { @@ -20,20 +17,5 @@ public final class ReflectionUtil { method.getAnnotation(annotationClass) : method.getDeclaringClass().getAnnotation(annotationClass); } - - public static void checkValidCommandMethod(Method method) { - if (method.isAnnotationPresent(SlashCommand.class) && - method.getDeclaringClass().isAnnotationPresent(SlashCommand.class)) { - throw new RuntimeException(method.getName() + ": Can't have ApplicationCommand Annotation on Class and Method"); - } - - if (!isAnnotationPresent(method, SlashCommand.class)) - throw new RuntimeException(method.getName() + ": Missing ApplicationCommand Annotation on either Class or Method"); - - if ((method.isAnnotationPresent(SubCommand.class) && - !isAnnotationPresent(method, SlashCommand.class))) { - throw new RuntimeException(method.getName() + ": Missing ApplicationCommand Annotation on either Method or Class"); - } - } }