implement AnnotationParser system
This commit is contained in:
parent
0ea330d48b
commit
582e0f0bae
@ -4,7 +4,8 @@ import java.lang.reflect.Method;
|
|||||||
|
|
||||||
import net.tomatentum.marinara.interaction.InteractionHandler;
|
import net.tomatentum.marinara.interaction.InteractionHandler;
|
||||||
import net.tomatentum.marinara.interaction.InteractionType;
|
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;
|
import net.tomatentum.marinara.wrapper.LibraryWrapper;
|
||||||
|
|
||||||
public class ButtonInteractionMethod extends InteractionMethod {
|
public class ButtonInteractionMethod extends InteractionMethod {
|
||||||
@ -13,7 +14,13 @@ public class ButtonInteractionMethod extends InteractionMethod {
|
|||||||
|
|
||||||
ButtonInteractionMethod(Method method, InteractionHandler handler, LibraryWrapper wrapper) {
|
ButtonInteractionMethod(Method method, InteractionHandler handler, LibraryWrapper wrapper) {
|
||||||
super(method, handler, wrapper);
|
super(method, handler, wrapper);
|
||||||
parseMethod();
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public AnnotationParser[] getParsers() {
|
||||||
|
return new AnnotationParser[] {
|
||||||
|
new ButtonParser(method, (x) -> { this.customId = x; } )
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -31,10 +38,4 @@ public class ButtonInteractionMethod extends InteractionMethod {
|
|||||||
public InteractionType getType() {
|
public InteractionType getType() {
|
||||||
return InteractionType.BUTTON;
|
return InteractionType.BUTTON;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void parseMethod() {
|
|
||||||
Button button = getMethod().getAnnotation(Button.class);
|
|
||||||
this.customId = button.value();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,7 @@ import net.tomatentum.marinara.interaction.InteractionType;
|
|||||||
import net.tomatentum.marinara.interaction.annotation.Button;
|
import net.tomatentum.marinara.interaction.annotation.Button;
|
||||||
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand;
|
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand;
|
||||||
import net.tomatentum.marinara.interaction.commands.annotation.SubCommand;
|
import net.tomatentum.marinara.interaction.commands.annotation.SubCommand;
|
||||||
|
import net.tomatentum.marinara.parser.AnnotationParser;
|
||||||
import net.tomatentum.marinara.wrapper.LibraryWrapper;
|
import net.tomatentum.marinara.wrapper.LibraryWrapper;
|
||||||
|
|
||||||
public abstract class InteractionMethod {
|
public abstract class InteractionMethod {
|
||||||
@ -27,6 +28,7 @@ public abstract class InteractionMethod {
|
|||||||
protected Method method;
|
protected Method method;
|
||||||
protected InteractionHandler handler;
|
protected InteractionHandler handler;
|
||||||
protected LibraryWrapper wrapper;
|
protected LibraryWrapper wrapper;
|
||||||
|
protected AnnotationParser[] parsers;
|
||||||
|
|
||||||
protected InteractionMethod(Method method, InteractionHandler handler, LibraryWrapper wrapper) {
|
protected InteractionMethod(Method method, InteractionHandler handler, LibraryWrapper wrapper) {
|
||||||
if (!Arrays.asList(handler.getClass().getMethods()).contains(method))
|
if (!Arrays.asList(handler.getClass().getMethods()).contains(method))
|
||||||
@ -34,8 +36,12 @@ public abstract class InteractionMethod {
|
|||||||
this.method = method;
|
this.method = method;
|
||||||
this.handler = handler;
|
this.handler = handler;
|
||||||
this.wrapper = wrapper;
|
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 Object getParameter(Object parameter, int index);
|
||||||
|
|
||||||
public abstract boolean canRun(Object context);
|
public abstract boolean canRun(Object context);
|
||||||
|
@ -5,10 +5,8 @@ import java.lang.reflect.Method;
|
|||||||
import net.tomatentum.marinara.interaction.InteractionHandler;
|
import net.tomatentum.marinara.interaction.InteractionHandler;
|
||||||
import net.tomatentum.marinara.interaction.InteractionType;
|
import net.tomatentum.marinara.interaction.InteractionType;
|
||||||
import net.tomatentum.marinara.interaction.commands.ExecutableSlashCommandDefinition;
|
import net.tomatentum.marinara.interaction.commands.ExecutableSlashCommandDefinition;
|
||||||
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand;
|
import net.tomatentum.marinara.parser.AnnotationParser;
|
||||||
import net.tomatentum.marinara.interaction.commands.annotation.SubCommand;
|
import net.tomatentum.marinara.parser.SlashCommandParser;
|
||||||
import net.tomatentum.marinara.interaction.commands.annotation.SubCommandGroup;
|
|
||||||
import net.tomatentum.marinara.util.ReflectionUtil;
|
|
||||||
import net.tomatentum.marinara.wrapper.LibraryWrapper;
|
import net.tomatentum.marinara.wrapper.LibraryWrapper;
|
||||||
|
|
||||||
public class SlashCommandInteractionMethod extends InteractionMethod {
|
public class SlashCommandInteractionMethod extends InteractionMethod {
|
||||||
@ -17,7 +15,13 @@ public class SlashCommandInteractionMethod extends InteractionMethod {
|
|||||||
|
|
||||||
SlashCommandInteractionMethod(Method method, InteractionHandler handler, LibraryWrapper wrapper) {
|
SlashCommandInteractionMethod(Method method, InteractionHandler handler, LibraryWrapper wrapper) {
|
||||||
super(method, handler, wrapper);
|
super(method, handler, wrapper);
|
||||||
parseMethod();
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public AnnotationParser[] getParsers() {
|
||||||
|
return new AnnotationParser[] {
|
||||||
|
new SlashCommandParser(method, (x) -> { this.commandDefinition = x; } )
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -40,24 +44,8 @@ public class SlashCommandInteractionMethod extends InteractionMethod {
|
|||||||
return commandDefinition;
|
return commandDefinition;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void parseMethod() {
|
public void setCommandDefinition(ExecutableSlashCommandDefinition commandDefinition) {
|
||||||
ReflectionUtil.checkValidCommandMethod(method);
|
this.commandDefinition = commandDefinition;
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,8 @@
|
|||||||
|
package net.tomatentum.marinara.parser;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
|
public interface AnnotationParser {
|
||||||
|
void parse();
|
||||||
|
Method getMethod();
|
||||||
|
}
|
@ -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<String> consumer;
|
||||||
|
|
||||||
|
public ButtonParser(Method method, Consumer<String> 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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<ExecutableSlashCommandDefinition> consumer;
|
||||||
|
|
||||||
|
public SlashCommandParser(Method method, Consumer<ExecutableSlashCommandDefinition> 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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -3,9 +3,6 @@ package net.tomatentum.marinara.util;
|
|||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
import java.lang.reflect.Method;
|
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 final class ReflectionUtil {
|
||||||
|
|
||||||
public static <T extends Annotation> boolean isAnnotationPresent(Method method, Class<T> annotationClass) {
|
public static <T extends Annotation> boolean isAnnotationPresent(Method method, Class<T> annotationClass) {
|
||||||
@ -21,19 +18,4 @@ public final class ReflectionUtil {
|
|||||||
method.getDeclaringClass().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");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user