feat(method): implement MethodParser rework
All checks were successful
github-mirror / push-github (push) Successful in 13s
Build / Gradle-Build (push) Successful in 14s
Test / Gradle-Test (push) Successful in 25s

This commit is contained in:
Tueem 2025-04-16 00:00:47 +02:00
parent eea1597b15
commit 42a675dc96
Signed by: tueem
GPG Key ID: F2CE0513D231AD7A
11 changed files with 147 additions and 129 deletions

View File

@ -8,7 +8,7 @@ javacord = "3.8.0"
discord4j = "3.2.7" discord4j = "3.2.7"
geantyref = "2.0.0" geantyref = "2.0.0"
mockito = "5.15.2" mockito = "5.15.2"
cutin = "0.1.1-cad019e" cutin = "0.2.0"
[libraries] [libraries]
junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit-jupiter" } junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit-jupiter" }
@ -18,4 +18,4 @@ javacord = { module = "org.javacord:javacord", version.ref = "javacord"}
discord4j = { module = "com.discord4j:discord4j-core", version.ref = "discord4j"} discord4j = { module = "com.discord4j:discord4j-core", version.ref = "discord4j"}
geantyref = { module = "io.leangen.geantyref:geantyref", version.ref = "geantyref"} geantyref = { module = "io.leangen.geantyref:geantyref", version.ref = "geantyref"}
mockito = {module = "org.mockito:mockito-core", version.ref = "mockito"} mockito = {module = "org.mockito:mockito-core", version.ref = "mockito"}
cutin = {module = "net.tomatentum.cutin:lib-dev", version.ref = "cutin"} cutin = {module = "net.tomatentum.cutin:lib", version.ref = "cutin"}

View File

@ -1,12 +1,12 @@
package net.tomatentum.marinara.checks; package net.tomatentum.marinara.checks;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.Set;
import net.tomatentum.cutin.MethodParser; import net.tomatentum.cutin.MethodParser;
import net.tomatentum.cutin.ReflectedMethodFactory.ParserResults;
import net.tomatentum.cutin.container.MethodContainer; import net.tomatentum.cutin.container.MethodContainer;
import net.tomatentum.cutin.method.BestCandidateMethod; import net.tomatentum.cutin.method.BestCandidateMethod;
import net.tomatentum.cutin.method.ReflectedMethod;
import net.tomatentum.marinara.checks.CheckMethodIdentifier.CheckMethodType; import net.tomatentum.marinara.checks.CheckMethodIdentifier.CheckMethodType;
import net.tomatentum.marinara.parser.InteractionCheckClassParser; import net.tomatentum.marinara.parser.InteractionCheckClassParser;
@ -14,8 +14,13 @@ public class InteractionCheckMethod extends BestCandidateMethod<CheckMethodIdent
private CheckMethodIdentifier identifier; private CheckMethodIdentifier identifier;
public InteractionCheckMethod(String methodName, Object containingObject) { public InteractionCheckMethod(
String methodName,
Object containingObject,
CheckMethodIdentifier identifier
) {
super(methodName, containingObject); super(methodName, containingObject);
this.identifier = identifier;
} }
@Override @Override
@ -44,21 +49,24 @@ public class InteractionCheckMethod extends BestCandidateMethod<CheckMethodIdent
this.type = type; this.type = type;
} }
@SuppressWarnings("unchecked")
@Override @Override
public void addParser(ReflectedMethod<CheckMethodIdentifier, CheckExecutionContext> method, List<MethodParser> parsers) { public void addParser(Set<MethodParser> parsers) {
parsers.add( parsers.add(
new InteractionCheckClassParser((Class<InteractionCheck<?>>) method.containingObject().getClass(), new InteractionCheckClassParser()
a -> ((InteractionCheckMethod) method).identifier = new CheckMethodIdentifier(a, type))
); );
} }
@Override @Override
protected Optional<BestCandidateMethod<CheckMethodIdentifier, CheckExecutionContext>> bcProduce(String methodName, protected Optional<BestCandidateMethod<CheckMethodIdentifier, CheckExecutionContext>> bcProduce(
Object containingObject) { String methodName,
if (!(containingObject instanceof InteractionCheck)) Object containingObject,
return Optional.empty(); ParserResults parserResults
return Optional.of(new InteractionCheckMethod(methodName, containingObject)); ) {
CheckMethodIdentifier identifier = new CheckMethodIdentifier(parserResults.get(InteractionCheckClassParser.class), type);
if (identifier.annotationType() == null)
return null;
return Optional.of(new InteractionCheckMethod(methodName, containingObject, identifier));
} }
} }

View File

@ -3,18 +3,21 @@ package net.tomatentum.marinara.interaction.components.methods;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.Set;
import net.tomatentum.cutin.MethodParser; import net.tomatentum.cutin.MethodParser;
import net.tomatentum.cutin.ReflectedMethodFactory.ParserResults;
import net.tomatentum.cutin.container.MethodContainer; import net.tomatentum.cutin.container.MethodContainer;
import net.tomatentum.cutin.method.ReflectedMethod; import net.tomatentum.cutin.method.ReflectedMethod;
import net.tomatentum.marinara.checks.AppliedCheck;
import net.tomatentum.marinara.checks.CheckExecutionContext; import net.tomatentum.marinara.checks.CheckExecutionContext;
import net.tomatentum.marinara.checks.CheckMethodIdentifier; import net.tomatentum.marinara.checks.CheckMethodIdentifier;
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.interaction.ident.InteractionIdentifier; import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
import net.tomatentum.marinara.interaction.methods.InteractionMethod; import net.tomatentum.marinara.interaction.methods.InteractionMethod;
import net.tomatentum.marinara.parser.ButtonParser; import net.tomatentum.marinara.parser.ButtonParser;
import net.tomatentum.marinara.parser.InteractionCheckParser;
import net.tomatentum.marinara.wrapper.ContextObjectProvider; import net.tomatentum.marinara.wrapper.ContextObjectProvider;
public class ButtonInteractionMethod extends InteractionMethod { public class ButtonInteractionMethod extends InteractionMethod {
@ -22,8 +25,15 @@ public class ButtonInteractionMethod extends InteractionMethod {
private String customId; private String customId;
private ContextObjectProvider cop; private ContextObjectProvider cop;
private ButtonInteractionMethod(Method method, InteractionHandler handler, ContextObjectProvider cop) { private ButtonInteractionMethod(
super(method, handler); Method method,
InteractionHandler handler,
List<AppliedCheck> appliedChecks,
String customId,
ContextObjectProvider cop
) {
super(method, handler, appliedChecks);
this.customId = customId;
this.cop = cop; this.cop = cop;
} }
@ -56,22 +66,25 @@ public class ButtonInteractionMethod extends InteractionMethod {
} }
@Override @Override
public Optional<ReflectedMethod<InteractionIdentifier, Object>> produce(Method method, Object containingObject) { public Optional<ReflectedMethod<InteractionIdentifier, Object>> produce(Method method, Object containingObject, ParserResults parserResults) {
ButtonInteractionMethod rMethod = null; if (!(containingObject instanceof InteractionHandler)) return Optional.empty();
if (method.isAnnotationPresent(Button.class) && String customId = parserResults.get(ButtonParser.class);
(containingObject instanceof InteractionHandler iHandler) if (customId == null) return Optional.empty();
) return Optional.of(new ButtonInteractionMethod(
rMethod = new ButtonInteractionMethod(method, iHandler, this.cop); method,
(InteractionHandler) containingObject,
return Optional.ofNullable(rMethod); parserResults.get(InteractionCheckParser.class),
customId,
this.cop
));
} }
@Override @Override
public void addParser(ReflectedMethod<InteractionIdentifier, Object> method, List<MethodParser> parser) { public void addParser(Set<MethodParser> parser) {
super.addParser(method, parser); super.addParser(parser);
parser.add( parser.add(
new ButtonParser(method.method(), x -> ((ButtonInteractionMethod) method).customId = x) new ButtonParser()
); );
} }

View File

@ -3,19 +3,20 @@ package net.tomatentum.marinara.interaction.methods;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.Set;
import net.tomatentum.cutin.MethodParser; import net.tomatentum.cutin.MethodParser;
import net.tomatentum.cutin.ReflectedMethodFactory.ParserResults;
import net.tomatentum.cutin.container.MethodContainer; import net.tomatentum.cutin.container.MethodContainer;
import net.tomatentum.cutin.method.ReflectedMethod; import net.tomatentum.cutin.method.ReflectedMethod;
import net.tomatentum.marinara.checks.AppliedCheck;
import net.tomatentum.marinara.checks.CheckExecutionContext; import net.tomatentum.marinara.checks.CheckExecutionContext;
import net.tomatentum.marinara.checks.CheckMethodIdentifier; import net.tomatentum.marinara.checks.CheckMethodIdentifier;
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.AutoComplete;
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand;
import net.tomatentum.marinara.interaction.commands.annotation.SubCommand;
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier; import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
import net.tomatentum.marinara.parser.AutocompleteParser; import net.tomatentum.marinara.parser.AutocompleteParser;
import net.tomatentum.marinara.parser.InteractionCheckParser;
import net.tomatentum.marinara.wrapper.ContextObjectProvider; import net.tomatentum.marinara.wrapper.ContextObjectProvider;
public class AutoCompleteInteractionMethod extends InteractionMethod { public class AutoCompleteInteractionMethod extends InteractionMethod {
@ -23,11 +24,15 @@ public class AutoCompleteInteractionMethod extends InteractionMethod {
private String autocompleteRef; private String autocompleteRef;
private ContextObjectProvider cop; private ContextObjectProvider cop;
private AutoCompleteInteractionMethod(Method method, private AutoCompleteInteractionMethod(
InteractionHandler handler, Method method,
ContextObjectProvider cop InteractionHandler handler,
List<AppliedCheck> appliedChecks,
String autocompleteRef,
ContextObjectProvider cop
) { ) {
super(method, handler); super(method, handler, appliedChecks);
this.autocompleteRef = autocompleteRef;
this.cop = cop; this.cop = cop;
} }
@ -64,23 +69,27 @@ public class AutoCompleteInteractionMethod extends InteractionMethod {
} }
@Override @Override
public Optional<ReflectedMethod<InteractionIdentifier, Object>> produce(Method method, Object containingObject) { public Optional<ReflectedMethod<InteractionIdentifier, Object>> produce(Method method, Object containingObject, ParserResults parserResults) {
AutoCompleteInteractionMethod rMethod = null; if (!(containingObject instanceof InteractionHandler)) return Optional.empty();
if ((containingObject instanceof InteractionHandler iHandler) && String[] autocompletes = parserResults.get(AutocompleteParser.class);
method.isAnnotationPresent(AutoComplete.class) && if (autocompletes.length <= 0) return Optional.empty();
!(method.isAnnotationPresent(SlashCommand.class) ||
method.isAnnotationPresent(SubCommand.class))) return Optional.of(new AutoCompleteInteractionMethod(
rMethod = new AutoCompleteInteractionMethod(method, iHandler, cop); method,
(InteractionHandler) containingObject,
parserResults.get(InteractionCheckParser.class),
autocompletes[0],
cop
));
return Optional.ofNullable(rMethod);
} }
@Override @Override
public void addParser(ReflectedMethod<InteractionIdentifier, Object> method, List<MethodParser> parser) { public void addParser(Set<MethodParser> parser) {
super.addParser(method, parser); super.addParser(parser);
parser.add( parser.add(
new AutocompleteParser(method.method(), x -> ((AutoCompleteInteractionMethod) method).autocompleteRef = x[0]) new AutocompleteParser()
); );
} }

View File

@ -1,8 +1,8 @@
package net.tomatentum.marinara.interaction.methods; package net.tomatentum.marinara.interaction.methods;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set;
import net.tomatentum.cutin.MethodParser; import net.tomatentum.cutin.MethodParser;
import net.tomatentum.cutin.ReflectedMethodFactory; import net.tomatentum.cutin.ReflectedMethodFactory;
@ -21,10 +21,11 @@ public abstract class InteractionMethod extends ReflectedMethod<InteractionIdent
protected InteractionMethod( protected InteractionMethod(
Method method, Method method,
InteractionHandler handler InteractionHandler handler,
List<AppliedCheck> appliedChecks
) { ) {
super(method, handler); super(method, handler);
this.appliedChecks = new ArrayList<>(); this.appliedChecks = appliedChecks;
} }
@Override @Override
@ -60,10 +61,9 @@ public abstract class InteractionMethod extends ReflectedMethod<InteractionIdent
} }
@Override @Override
public void addParser(ReflectedMethod<InteractionIdentifier, Object> method, List<MethodParser> parser) { public void addParser(Set<MethodParser> parser) {
InteractionMethod imethod = (InteractionMethod) method;
parser.add( parser.add(
new InteractionCheckParser(method.method(), imethod.appliedChecks::add, this.checkContainer) new InteractionCheckParser(this.checkContainer)
); );
} }

View File

@ -3,27 +3,36 @@ package net.tomatentum.marinara.interaction.methods;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.Set;
import net.tomatentum.cutin.MethodParser; import net.tomatentum.cutin.MethodParser;
import net.tomatentum.cutin.ReflectedMethodFactory.ParserResults;
import net.tomatentum.cutin.container.MethodContainer; import net.tomatentum.cutin.container.MethodContainer;
import net.tomatentum.cutin.method.ReflectedMethod; import net.tomatentum.cutin.method.ReflectedMethod;
import net.tomatentum.marinara.checks.AppliedCheck;
import net.tomatentum.marinara.checks.CheckExecutionContext; import net.tomatentum.marinara.checks.CheckExecutionContext;
import net.tomatentum.marinara.checks.CheckMethodIdentifier; import net.tomatentum.marinara.checks.CheckMethodIdentifier;
import net.tomatentum.marinara.interaction.InteractionHandler; import net.tomatentum.marinara.interaction.InteractionHandler;
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand;
import net.tomatentum.marinara.interaction.commands.annotation.SubCommand;
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier; import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
import net.tomatentum.marinara.interaction.ident.SlashCommandIdentifier; import net.tomatentum.marinara.interaction.ident.SlashCommandIdentifier;
import net.tomatentum.marinara.parser.InteractionCheckParser;
import net.tomatentum.marinara.parser.SlashCommandParser; import net.tomatentum.marinara.parser.SlashCommandParser;
import net.tomatentum.marinara.wrapper.ContextObjectProvider; import net.tomatentum.marinara.wrapper.ContextObjectProvider;
public class SlashCommandInteractionMethod extends InteractionMethod { public class SlashCommandInteractionMethod extends InteractionMethod {
private SlashCommandIdentifier interactionIdentifier; private SlashCommandIdentifier identifier;
private ContextObjectProvider cop; private ContextObjectProvider cop;
private SlashCommandInteractionMethod(Method method, InteractionHandler handler, ContextObjectProvider cop) { private SlashCommandInteractionMethod(
super(method, handler); Method method,
InteractionHandler handler,
List<AppliedCheck> appliedChecks,
SlashCommandIdentifier identifier,
ContextObjectProvider cop
) {
super(method, handler, appliedChecks);
this.identifier = identifier;
this.cop = cop; this.cop = cop;
} }
@ -31,14 +40,14 @@ public class SlashCommandInteractionMethod extends InteractionMethod {
public Object getParameter(Object context, int index) { public Object getParameter(Object context, int index) {
Object superResult = super.getParameter(context, index); Object superResult = super.getParameter(context, index);
if (superResult == null) if (superResult == null)
return this.cop.convertCommandOption(context, interactionIdentifier.options()[index-1].name()); return this.cop.convertCommandOption(context, identifier.options()[index-1].name());
else else
return superResult; return superResult;
} }
@Override @Override
public InteractionIdentifier identifier() { public InteractionIdentifier identifier() {
return interactionIdentifier; return identifier;
} }
public static class Factory extends InteractionMethod.Factory { public static class Factory extends InteractionMethod.Factory {
@ -51,21 +60,25 @@ public class SlashCommandInteractionMethod extends InteractionMethod {
} }
@Override @Override
public Optional<ReflectedMethod<InteractionIdentifier, Object>> produce(Method method, Object containingObject) { public Optional<ReflectedMethod<InteractionIdentifier, Object>> produce(Method method, Object containingObject, ParserResults parserResults) {
SlashCommandInteractionMethod rMethod = null; if (!(containingObject instanceof InteractionHandler)) return Optional.empty();
if ((containingObject instanceof InteractionHandler iHandler) && SlashCommandIdentifier ident = parserResults.get(SlashCommandParser.class);
(method.isAnnotationPresent(SlashCommand.class) || if (ident == null) return Optional.empty();
method.isAnnotationPresent(SubCommand.class))) return Optional.of(new SlashCommandInteractionMethod(
rMethod = new SlashCommandInteractionMethod(method, iHandler, cop); method,
return Optional.ofNullable(rMethod); (InteractionHandler) containingObject,
parserResults.get(InteractionCheckParser.class),
ident,
cop
));
} }
@Override @Override
public void addParser(ReflectedMethod<InteractionIdentifier, Object> method, List<MethodParser> parser) { public void addParser(Set<MethodParser> parser) {
super.addParser(method, parser); super.addParser(parser);
parser.add( parser.add(
new SlashCommandParser(method.method(), x -> ((SlashCommandInteractionMethod) method).interactionIdentifier = x) new SlashCommandParser()
); );
} }

View File

@ -2,7 +2,6 @@ package net.tomatentum.marinara.parser;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Arrays; import java.util.Arrays;
import java.util.function.Consumer;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -15,21 +14,14 @@ public class AutocompleteParser implements MethodParser {
private Logger logger = LoggerFactory.getLogger(getClass()); private Logger logger = LoggerFactory.getLogger(getClass());
private Method method;
private Consumer<String[]> consumer;
public AutocompleteParser(Method method, Consumer<String[]> consumer) {
this.method = method;
this.consumer = consumer;
}
@Override @Override
public void parse() { public Object parse(Method method, Object containingObject) {
String[] autocompletes = Arrays.stream(this.method.getAnnotationsByType(AutoComplete.class)) String[] autoCompletes = Arrays.stream(method.getAnnotationsByType(AutoComplete.class))
.map(AutoComplete::value) .map(AutoComplete::value)
.toArray(String[]::new); .toArray(String[]::new);
logger.trace("Parsed AutoComplete annotation {} for method {}", autocompletes, ReflectionUtil.getFullMethodName(method)); if (autoCompletes.length > 0)
this.consumer.accept(autocompletes); logger.trace("Parsed AutoComplete annotations {} for method {}", autoCompletes, ReflectionUtil.getFullMethodName(method));
return autoCompletes;
} }
} }

View File

@ -1,7 +1,6 @@
package net.tomatentum.marinara.parser; package net.tomatentum.marinara.parser;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.function.Consumer;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -12,21 +11,17 @@ import net.tomatentum.marinara.interaction.annotation.Button;
public class ButtonParser implements MethodParser { public class ButtonParser implements MethodParser {
private Method method;
private Consumer<String> consumer;
private Logger logger = LoggerFactory.getLogger(getClass()); private Logger logger = LoggerFactory.getLogger(getClass());
public ButtonParser(Method method, Consumer<String> consumer) { public ButtonParser() {
this.method = method;
this.consumer = consumer;
} }
@Override @Override
public void parse() { public Object parse(Method method, Object containingObject) {
Button button = this.method.getAnnotation(Button.class); Button button = method.getAnnotation(Button.class);
if (button == null) return null;
logger.trace("Parsed Button annotation {} for method {}", button, ReflectionUtil.getFullMethodName(method)); logger.trace("Parsed Button annotation {} for method {}", button, ReflectionUtil.getFullMethodName(method));
this.consumer.accept(button.value()); return button.value();
} }
} }

View File

@ -1,8 +1,8 @@
package net.tomatentum.marinara.parser; package net.tomatentum.marinara.parser;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType; import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.function.Consumer;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -15,20 +15,14 @@ public class InteractionCheckClassParser implements MethodParser {
private Logger logger = LoggerFactory.getLogger(getClass()); private Logger logger = LoggerFactory.getLogger(getClass());
private Class<? extends InteractionCheck<?>> interactionCheckType;
private Consumer<Type> annotationTypeConsumer;
public InteractionCheckClassParser(Class<? extends InteractionCheck<?>> interactionCheckType, Consumer<Type> annotationTypeConsumer) {
this.interactionCheckType = interactionCheckType;
this.annotationTypeConsumer = annotationTypeConsumer;
}
@Override @Override
public void parse() { public Object parse(Method method, Object containingObject) {
ParameterizedType type = (ParameterizedType) GenericTypeReflector.getExactSuperType(interactionCheckType, InteractionCheck.class); ParameterizedType type = (ParameterizedType) GenericTypeReflector.getExactSuperType(containingObject.getClass(), InteractionCheck.class);
Type typeParam = type.getActualTypeArguments()[0]; if (type == null) return null;
logger.trace("Parsed InteractionCheck Annotation {}", typeParam); Type typeParam = type.getActualTypeArguments().length == 1 ? type.getActualTypeArguments()[0] : null;
this.annotationTypeConsumer.accept(typeParam); if (typeParam != null)
logger.trace("Parsed InteractionCheck Annotation {}", typeParam);
return typeParam;
} }
} }

View File

@ -3,7 +3,7 @@ package net.tomatentum.marinara.parser;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Arrays; import java.util.Arrays;
import java.util.function.Consumer; import java.util.Objects;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -19,31 +19,31 @@ import net.tomatentum.marinara.checks.CheckMethodIdentifier.CheckMethodType;
public class InteractionCheckParser implements MethodParser { public class InteractionCheckParser implements MethodParser {
private MethodContainer<CheckMethodIdentifier, CheckExecutionContext> checkContainer; private MethodContainer<CheckMethodIdentifier, CheckExecutionContext> checkContainer;
private Method method;
private Consumer<AppliedCheck> consumer;
private Logger logger = LoggerFactory.getLogger(getClass()); private Logger logger = LoggerFactory.getLogger(getClass());
public InteractionCheckParser(Method method, Consumer<AppliedCheck> consumer, MethodContainer<CheckMethodIdentifier, CheckExecutionContext> checkContainer) { public InteractionCheckParser(MethodContainer<CheckMethodIdentifier, CheckExecutionContext> checkContainer) {
this.checkContainer = checkContainer; this.checkContainer = checkContainer;
this.method = method;
this.consumer = consumer;
} }
@Override @Override
public void parse() { public Object parse(Method method, Object containingObject) {
Annotation[] annotations = method.getAnnotations(); Annotation[] annotations = method.getAnnotations();
Arrays.stream(annotations).forEach(this::convertAnnotation); return Arrays.stream(annotations)
.map(a -> convertAnnotation(a, method))
.filter(Objects::nonNull)
.toList();
} }
private void convertAnnotation(Annotation annotation) { private AppliedCheck convertAnnotation(Annotation annotation, Method method) {
var preExec = this.checkContainer.findFirstFor(new CheckMethodIdentifier(annotation.annotationType(), CheckMethodType.PRE)); var preExec = this.checkContainer.findFirstFor(new CheckMethodIdentifier(annotation.annotationType(), CheckMethodType.PRE));
var postExec = this.checkContainer.findFirstFor(new CheckMethodIdentifier(annotation.annotationType(), CheckMethodType.POST)); var postExec = this.checkContainer.findFirstFor(new CheckMethodIdentifier(annotation.annotationType(), CheckMethodType.POST));
if (preExec.isPresent() && postExec.isPresent()) { if (preExec.isPresent() && postExec.isPresent()) {
AppliedCheck appliedCheck = new AppliedCheck(annotation, preExec.get(), postExec.get()); AppliedCheck appliedCheck = new AppliedCheck(annotation, preExec.get(), postExec.get());
logger.trace("Parsed InteractionCheck {} for annotation {} for method {}", preExec.get().containingObject(), annotation, ReflectionUtil.getFullMethodName(method)); logger.trace("Parsed InteractionCheck {} for annotation {} for method {}", preExec.get().containingObject(), annotation, ReflectionUtil.getFullMethodName(method));
consumer.accept(appliedCheck); return appliedCheck;
} }
return null;
} }
} }

View File

@ -1,7 +1,6 @@
package net.tomatentum.marinara.parser; package net.tomatentum.marinara.parser;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.function.Consumer;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -13,22 +12,17 @@ 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.interaction.commands.annotation.SubCommandGroup; import net.tomatentum.marinara.interaction.commands.annotation.SubCommandGroup;
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier; import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
import net.tomatentum.marinara.interaction.ident.SlashCommandIdentifier;
public class SlashCommandParser implements MethodParser { public class SlashCommandParser implements MethodParser {
private Method method;
private Consumer<SlashCommandIdentifier> consumer;
private Logger logger = LoggerFactory.getLogger(getClass()); private Logger logger = LoggerFactory.getLogger(getClass());
public SlashCommandParser(Method method, Consumer<SlashCommandIdentifier> consumer) { public SlashCommandParser() {
this.method = method;
this.consumer = consumer;
} }
@Override @Override
public void parse() { public Object parse(Method method, Object containingObject) {
if (!method.isAnnotationPresent(SlashCommand.class) && !method.isAnnotationPresent(SubCommand.class)) return null;
this.checkValidCommandMethod(method); this.checkValidCommandMethod(method);
SlashCommand cmd = ReflectionUtil.getAnnotation(method, SlashCommand.class); SlashCommand cmd = ReflectionUtil.getAnnotation(method, SlashCommand.class);
@ -59,21 +53,21 @@ public class SlashCommandParser implements MethodParser {
} }
logger.trace("Parsed using SlashCommandParser for method {} with the result: {}", ReflectionUtil.getFullMethodName(method), lastIdentifier); logger.trace("Parsed using SlashCommandParser for method {} with the result: {}", ReflectionUtil.getFullMethodName(method), lastIdentifier);
consumer.accept((SlashCommandIdentifier) lastIdentifier); return lastIdentifier;
} }
private void checkValidCommandMethod(Method method) { private void checkValidCommandMethod(Method method) {
if (method.isAnnotationPresent(SlashCommand.class) && if (method.isAnnotationPresent(SlashCommand.class) &&
method.getDeclaringClass().isAnnotationPresent(SlashCommand.class)) { method.getDeclaringClass().isAnnotationPresent(SlashCommand.class)) {
throw new RuntimeException(method.getName() + ": Can't have ApplicationCommand Annotation on Class and Method"); throw new RuntimeException(method.getName() + ": Can't have SlashCommand Annotation on Class and Method");
} }
if (!ReflectionUtil.isAnnotationPresent(method, SlashCommand.class)) if (!ReflectionUtil.isAnnotationPresent(method, SlashCommand.class))
throw new RuntimeException(method.getName() + ": Missing ApplicationCommand Annotation on either Class or Method"); throw new RuntimeException(method.getName() + ": Missing SlashCommand Annotation on either Class or Method");
if ((method.isAnnotationPresent(SubCommand.class) && if ((method.isAnnotationPresent(SubCommand.class) &&
!ReflectionUtil.isAnnotationPresent(method, SlashCommand.class))) { !ReflectionUtil.isAnnotationPresent(method, SlashCommand.class))) {
throw new RuntimeException(method.getName() + ": Missing ApplicationCommand Annotation on either Method or Class"); throw new RuntimeException(method.getName() + ": Missing SlashCommand Annotation on either Method or Class");
} }
} }