feat(method): implement MethodParser rework
This commit is contained in:
parent
eea1597b15
commit
42a675dc96
@ -8,7 +8,7 @@ javacord = "3.8.0"
|
||||
discord4j = "3.2.7"
|
||||
geantyref = "2.0.0"
|
||||
mockito = "5.15.2"
|
||||
cutin = "0.1.1-cad019e"
|
||||
cutin = "0.2.0"
|
||||
|
||||
[libraries]
|
||||
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"}
|
||||
geantyref = { module = "io.leangen.geantyref:geantyref", version.ref = "geantyref"}
|
||||
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"}
|
||||
|
@ -1,12 +1,12 @@
|
||||
package net.tomatentum.marinara.checks;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import net.tomatentum.cutin.MethodParser;
|
||||
import net.tomatentum.cutin.ReflectedMethodFactory.ParserResults;
|
||||
import net.tomatentum.cutin.container.MethodContainer;
|
||||
import net.tomatentum.cutin.method.BestCandidateMethod;
|
||||
import net.tomatentum.cutin.method.ReflectedMethod;
|
||||
import net.tomatentum.marinara.checks.CheckMethodIdentifier.CheckMethodType;
|
||||
import net.tomatentum.marinara.parser.InteractionCheckClassParser;
|
||||
|
||||
@ -14,8 +14,13 @@ public class InteractionCheckMethod extends BestCandidateMethod<CheckMethodIdent
|
||||
|
||||
private CheckMethodIdentifier identifier;
|
||||
|
||||
public InteractionCheckMethod(String methodName, Object containingObject) {
|
||||
public InteractionCheckMethod(
|
||||
String methodName,
|
||||
Object containingObject,
|
||||
CheckMethodIdentifier identifier
|
||||
) {
|
||||
super(methodName, containingObject);
|
||||
this.identifier = identifier;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -44,21 +49,24 @@ public class InteractionCheckMethod extends BestCandidateMethod<CheckMethodIdent
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public void addParser(ReflectedMethod<CheckMethodIdentifier, CheckExecutionContext> method, List<MethodParser> parsers) {
|
||||
public void addParser(Set<MethodParser> parsers) {
|
||||
parsers.add(
|
||||
new InteractionCheckClassParser((Class<InteractionCheck<?>>) method.containingObject().getClass(),
|
||||
a -> ((InteractionCheckMethod) method).identifier = new CheckMethodIdentifier(a, type))
|
||||
new InteractionCheckClassParser()
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Optional<BestCandidateMethod<CheckMethodIdentifier, CheckExecutionContext>> bcProduce(String methodName,
|
||||
Object containingObject) {
|
||||
if (!(containingObject instanceof InteractionCheck))
|
||||
return Optional.empty();
|
||||
return Optional.of(new InteractionCheckMethod(methodName, containingObject));
|
||||
protected Optional<BestCandidateMethod<CheckMethodIdentifier, CheckExecutionContext>> bcProduce(
|
||||
String methodName,
|
||||
Object containingObject,
|
||||
ParserResults parserResults
|
||||
) {
|
||||
|
||||
CheckMethodIdentifier identifier = new CheckMethodIdentifier(parserResults.get(InteractionCheckClassParser.class), type);
|
||||
if (identifier.annotationType() == null)
|
||||
return null;
|
||||
return Optional.of(new InteractionCheckMethod(methodName, containingObject, identifier));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -3,18 +3,21 @@ package net.tomatentum.marinara.interaction.components.methods;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import net.tomatentum.cutin.MethodParser;
|
||||
import net.tomatentum.cutin.ReflectedMethodFactory.ParserResults;
|
||||
import net.tomatentum.cutin.container.MethodContainer;
|
||||
import net.tomatentum.cutin.method.ReflectedMethod;
|
||||
import net.tomatentum.marinara.checks.AppliedCheck;
|
||||
import net.tomatentum.marinara.checks.CheckExecutionContext;
|
||||
import net.tomatentum.marinara.checks.CheckMethodIdentifier;
|
||||
import net.tomatentum.marinara.interaction.InteractionHandler;
|
||||
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.methods.InteractionMethod;
|
||||
import net.tomatentum.marinara.parser.ButtonParser;
|
||||
import net.tomatentum.marinara.parser.InteractionCheckParser;
|
||||
import net.tomatentum.marinara.wrapper.ContextObjectProvider;
|
||||
|
||||
public class ButtonInteractionMethod extends InteractionMethod {
|
||||
@ -22,8 +25,15 @@ public class ButtonInteractionMethod extends InteractionMethod {
|
||||
private String customId;
|
||||
private ContextObjectProvider cop;
|
||||
|
||||
private ButtonInteractionMethod(Method method, InteractionHandler handler, ContextObjectProvider cop) {
|
||||
super(method, handler);
|
||||
private ButtonInteractionMethod(
|
||||
Method method,
|
||||
InteractionHandler handler,
|
||||
List<AppliedCheck> appliedChecks,
|
||||
String customId,
|
||||
ContextObjectProvider cop
|
||||
) {
|
||||
super(method, handler, appliedChecks);
|
||||
this.customId = customId;
|
||||
this.cop = cop;
|
||||
}
|
||||
|
||||
@ -56,22 +66,25 @@ public class ButtonInteractionMethod extends InteractionMethod {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<ReflectedMethod<InteractionIdentifier, Object>> produce(Method method, Object containingObject) {
|
||||
ButtonInteractionMethod rMethod = null;
|
||||
if (method.isAnnotationPresent(Button.class) &&
|
||||
(containingObject instanceof InteractionHandler iHandler)
|
||||
)
|
||||
rMethod = new ButtonInteractionMethod(method, iHandler, this.cop);
|
||||
|
||||
return Optional.ofNullable(rMethod);
|
||||
public Optional<ReflectedMethod<InteractionIdentifier, Object>> produce(Method method, Object containingObject, ParserResults parserResults) {
|
||||
if (!(containingObject instanceof InteractionHandler)) return Optional.empty();
|
||||
String customId = parserResults.get(ButtonParser.class);
|
||||
if (customId == null) return Optional.empty();
|
||||
return Optional.of(new ButtonInteractionMethod(
|
||||
method,
|
||||
(InteractionHandler) containingObject,
|
||||
parserResults.get(InteractionCheckParser.class),
|
||||
customId,
|
||||
this.cop
|
||||
));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addParser(ReflectedMethod<InteractionIdentifier, Object> method, List<MethodParser> parser) {
|
||||
super.addParser(method, parser);
|
||||
public void addParser(Set<MethodParser> parser) {
|
||||
super.addParser(parser);
|
||||
|
||||
parser.add(
|
||||
new ButtonParser(method.method(), x -> ((ButtonInteractionMethod) method).customId = x)
|
||||
new ButtonParser()
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -3,19 +3,20 @@ package net.tomatentum.marinara.interaction.methods;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import net.tomatentum.cutin.MethodParser;
|
||||
import net.tomatentum.cutin.ReflectedMethodFactory.ParserResults;
|
||||
import net.tomatentum.cutin.container.MethodContainer;
|
||||
import net.tomatentum.cutin.method.ReflectedMethod;
|
||||
import net.tomatentum.marinara.checks.AppliedCheck;
|
||||
import net.tomatentum.marinara.checks.CheckExecutionContext;
|
||||
import net.tomatentum.marinara.checks.CheckMethodIdentifier;
|
||||
import net.tomatentum.marinara.interaction.InteractionHandler;
|
||||
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.parser.AutocompleteParser;
|
||||
import net.tomatentum.marinara.parser.InteractionCheckParser;
|
||||
import net.tomatentum.marinara.wrapper.ContextObjectProvider;
|
||||
|
||||
public class AutoCompleteInteractionMethod extends InteractionMethod {
|
||||
@ -23,11 +24,15 @@ public class AutoCompleteInteractionMethod extends InteractionMethod {
|
||||
private String autocompleteRef;
|
||||
private ContextObjectProvider cop;
|
||||
|
||||
private AutoCompleteInteractionMethod(Method method,
|
||||
private AutoCompleteInteractionMethod(
|
||||
Method method,
|
||||
InteractionHandler handler,
|
||||
List<AppliedCheck> appliedChecks,
|
||||
String autocompleteRef,
|
||||
ContextObjectProvider cop
|
||||
) {
|
||||
super(method, handler);
|
||||
super(method, handler, appliedChecks);
|
||||
this.autocompleteRef = autocompleteRef;
|
||||
this.cop = cop;
|
||||
}
|
||||
|
||||
@ -64,23 +69,27 @@ public class AutoCompleteInteractionMethod extends InteractionMethod {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<ReflectedMethod<InteractionIdentifier, Object>> produce(Method method, Object containingObject) {
|
||||
AutoCompleteInteractionMethod rMethod = null;
|
||||
if ((containingObject instanceof InteractionHandler iHandler) &&
|
||||
method.isAnnotationPresent(AutoComplete.class) &&
|
||||
!(method.isAnnotationPresent(SlashCommand.class) ||
|
||||
method.isAnnotationPresent(SubCommand.class)))
|
||||
rMethod = new AutoCompleteInteractionMethod(method, iHandler, cop);
|
||||
public Optional<ReflectedMethod<InteractionIdentifier, Object>> produce(Method method, Object containingObject, ParserResults parserResults) {
|
||||
if (!(containingObject instanceof InteractionHandler)) return Optional.empty();
|
||||
String[] autocompletes = parserResults.get(AutocompleteParser.class);
|
||||
if (autocompletes.length <= 0) return Optional.empty();
|
||||
|
||||
return Optional.of(new AutoCompleteInteractionMethod(
|
||||
method,
|
||||
(InteractionHandler) containingObject,
|
||||
parserResults.get(InteractionCheckParser.class),
|
||||
autocompletes[0],
|
||||
cop
|
||||
));
|
||||
|
||||
return Optional.ofNullable(rMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addParser(ReflectedMethod<InteractionIdentifier, Object> method, List<MethodParser> parser) {
|
||||
super.addParser(method, parser);
|
||||
public void addParser(Set<MethodParser> parser) {
|
||||
super.addParser(parser);
|
||||
|
||||
parser.add(
|
||||
new AutocompleteParser(method.method(), x -> ((AutoCompleteInteractionMethod) method).autocompleteRef = x[0])
|
||||
new AutocompleteParser()
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -1,8 +1,8 @@
|
||||
package net.tomatentum.marinara.interaction.methods;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import net.tomatentum.cutin.MethodParser;
|
||||
import net.tomatentum.cutin.ReflectedMethodFactory;
|
||||
@ -21,10 +21,11 @@ public abstract class InteractionMethod extends ReflectedMethod<InteractionIdent
|
||||
|
||||
protected InteractionMethod(
|
||||
Method method,
|
||||
InteractionHandler handler
|
||||
InteractionHandler handler,
|
||||
List<AppliedCheck> appliedChecks
|
||||
) {
|
||||
super(method, handler);
|
||||
this.appliedChecks = new ArrayList<>();
|
||||
this.appliedChecks = appliedChecks;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -60,10 +61,9 @@ public abstract class InteractionMethod extends ReflectedMethod<InteractionIdent
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addParser(ReflectedMethod<InteractionIdentifier, Object> method, List<MethodParser> parser) {
|
||||
InteractionMethod imethod = (InteractionMethod) method;
|
||||
public void addParser(Set<MethodParser> parser) {
|
||||
parser.add(
|
||||
new InteractionCheckParser(method.method(), imethod.appliedChecks::add, this.checkContainer)
|
||||
new InteractionCheckParser(this.checkContainer)
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -3,27 +3,36 @@ package net.tomatentum.marinara.interaction.methods;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import net.tomatentum.cutin.MethodParser;
|
||||
import net.tomatentum.cutin.ReflectedMethodFactory.ParserResults;
|
||||
import net.tomatentum.cutin.container.MethodContainer;
|
||||
import net.tomatentum.cutin.method.ReflectedMethod;
|
||||
import net.tomatentum.marinara.checks.AppliedCheck;
|
||||
import net.tomatentum.marinara.checks.CheckExecutionContext;
|
||||
import net.tomatentum.marinara.checks.CheckMethodIdentifier;
|
||||
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.SlashCommandIdentifier;
|
||||
import net.tomatentum.marinara.parser.InteractionCheckParser;
|
||||
import net.tomatentum.marinara.parser.SlashCommandParser;
|
||||
import net.tomatentum.marinara.wrapper.ContextObjectProvider;
|
||||
|
||||
public class SlashCommandInteractionMethod extends InteractionMethod {
|
||||
|
||||
private SlashCommandIdentifier interactionIdentifier;
|
||||
private SlashCommandIdentifier identifier;
|
||||
private ContextObjectProvider cop;
|
||||
|
||||
private SlashCommandInteractionMethod(Method method, InteractionHandler handler, ContextObjectProvider cop) {
|
||||
super(method, handler);
|
||||
private SlashCommandInteractionMethod(
|
||||
Method method,
|
||||
InteractionHandler handler,
|
||||
List<AppliedCheck> appliedChecks,
|
||||
SlashCommandIdentifier identifier,
|
||||
ContextObjectProvider cop
|
||||
) {
|
||||
super(method, handler, appliedChecks);
|
||||
this.identifier = identifier;
|
||||
this.cop = cop;
|
||||
}
|
||||
|
||||
@ -31,14 +40,14 @@ public class SlashCommandInteractionMethod extends InteractionMethod {
|
||||
public Object getParameter(Object context, int index) {
|
||||
Object superResult = super.getParameter(context, index);
|
||||
if (superResult == null)
|
||||
return this.cop.convertCommandOption(context, interactionIdentifier.options()[index-1].name());
|
||||
return this.cop.convertCommandOption(context, identifier.options()[index-1].name());
|
||||
else
|
||||
return superResult;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InteractionIdentifier identifier() {
|
||||
return interactionIdentifier;
|
||||
return identifier;
|
||||
}
|
||||
|
||||
public static class Factory extends InteractionMethod.Factory {
|
||||
@ -51,21 +60,25 @@ public class SlashCommandInteractionMethod extends InteractionMethod {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<ReflectedMethod<InteractionIdentifier, Object>> produce(Method method, Object containingObject) {
|
||||
SlashCommandInteractionMethod rMethod = null;
|
||||
if ((containingObject instanceof InteractionHandler iHandler) &&
|
||||
(method.isAnnotationPresent(SlashCommand.class) ||
|
||||
method.isAnnotationPresent(SubCommand.class)))
|
||||
rMethod = new SlashCommandInteractionMethod(method, iHandler, cop);
|
||||
return Optional.ofNullable(rMethod);
|
||||
public Optional<ReflectedMethod<InteractionIdentifier, Object>> produce(Method method, Object containingObject, ParserResults parserResults) {
|
||||
if (!(containingObject instanceof InteractionHandler)) return Optional.empty();
|
||||
SlashCommandIdentifier ident = parserResults.get(SlashCommandParser.class);
|
||||
if (ident == null) return Optional.empty();
|
||||
return Optional.of(new SlashCommandInteractionMethod(
|
||||
method,
|
||||
(InteractionHandler) containingObject,
|
||||
parserResults.get(InteractionCheckParser.class),
|
||||
ident,
|
||||
cop
|
||||
));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addParser(ReflectedMethod<InteractionIdentifier, Object> method, List<MethodParser> parser) {
|
||||
super.addParser(method, parser);
|
||||
public void addParser(Set<MethodParser> parser) {
|
||||
super.addParser(parser);
|
||||
|
||||
parser.add(
|
||||
new SlashCommandParser(method.method(), x -> ((SlashCommandInteractionMethod) method).interactionIdentifier = x)
|
||||
new SlashCommandParser()
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -2,7 +2,6 @@ package net.tomatentum.marinara.parser;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -15,21 +14,14 @@ public class AutocompleteParser implements MethodParser {
|
||||
|
||||
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
|
||||
public void parse() {
|
||||
String[] autocompletes = Arrays.stream(this.method.getAnnotationsByType(AutoComplete.class))
|
||||
public Object parse(Method method, Object containingObject) {
|
||||
String[] autoCompletes = Arrays.stream(method.getAnnotationsByType(AutoComplete.class))
|
||||
.map(AutoComplete::value)
|
||||
.toArray(String[]::new);
|
||||
logger.trace("Parsed AutoComplete annotation {} for method {}", autocompletes, ReflectionUtil.getFullMethodName(method));
|
||||
this.consumer.accept(autocompletes);
|
||||
if (autoCompletes.length > 0)
|
||||
logger.trace("Parsed AutoComplete annotations {} for method {}", autoCompletes, ReflectionUtil.getFullMethodName(method));
|
||||
return autoCompletes;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
package net.tomatentum.marinara.parser;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -12,21 +11,17 @@ import net.tomatentum.marinara.interaction.annotation.Button;
|
||||
|
||||
public class ButtonParser implements MethodParser {
|
||||
|
||||
private Method method;
|
||||
private Consumer<String> consumer;
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
public ButtonParser(Method method, Consumer<String> consumer) {
|
||||
this.method = method;
|
||||
this.consumer = consumer;
|
||||
public ButtonParser() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void parse() {
|
||||
Button button = this.method.getAnnotation(Button.class);
|
||||
public Object parse(Method method, Object containingObject) {
|
||||
Button button = method.getAnnotation(Button.class);
|
||||
if (button == null) return null;
|
||||
logger.trace("Parsed Button annotation {} for method {}", button, ReflectionUtil.getFullMethodName(method));
|
||||
this.consumer.accept(button.value());
|
||||
return button.value();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,8 +1,8 @@
|
||||
package net.tomatentum.marinara.parser;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -15,20 +15,14 @@ public class InteractionCheckClassParser implements MethodParser {
|
||||
|
||||
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
|
||||
public void parse() {
|
||||
ParameterizedType type = (ParameterizedType) GenericTypeReflector.getExactSuperType(interactionCheckType, InteractionCheck.class);
|
||||
Type typeParam = type.getActualTypeArguments()[0];
|
||||
public Object parse(Method method, Object containingObject) {
|
||||
ParameterizedType type = (ParameterizedType) GenericTypeReflector.getExactSuperType(containingObject.getClass(), InteractionCheck.class);
|
||||
if (type == null) return null;
|
||||
Type typeParam = type.getActualTypeArguments().length == 1 ? type.getActualTypeArguments()[0] : null;
|
||||
if (typeParam != null)
|
||||
logger.trace("Parsed InteractionCheck Annotation {}", typeParam);
|
||||
this.annotationTypeConsumer.accept(typeParam);
|
||||
return typeParam;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ package net.tomatentum.marinara.parser;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.Objects;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -19,31 +19,31 @@ import net.tomatentum.marinara.checks.CheckMethodIdentifier.CheckMethodType;
|
||||
public class InteractionCheckParser implements MethodParser {
|
||||
|
||||
private MethodContainer<CheckMethodIdentifier, CheckExecutionContext> checkContainer;
|
||||
private Method method;
|
||||
private Consumer<AppliedCheck> consumer;
|
||||
|
||||
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.method = method;
|
||||
this.consumer = consumer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void parse() {
|
||||
public Object parse(Method method, Object containingObject) {
|
||||
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 postExec = this.checkContainer.findFirstFor(new CheckMethodIdentifier(annotation.annotationType(), CheckMethodType.POST));
|
||||
if (preExec.isPresent() && postExec.isPresent()) {
|
||||
AppliedCheck appliedCheck = new AppliedCheck(annotation, preExec.get(), postExec.get());
|
||||
logger.trace("Parsed InteractionCheck {} for annotation {} for method {}", preExec.get().containingObject(), annotation, ReflectionUtil.getFullMethodName(method));
|
||||
consumer.accept(appliedCheck);
|
||||
return appliedCheck;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
package net.tomatentum.marinara.parser;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
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.SubCommandGroup;
|
||||
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||
import net.tomatentum.marinara.interaction.ident.SlashCommandIdentifier;
|
||||
|
||||
public class SlashCommandParser implements MethodParser {
|
||||
|
||||
private Method method;
|
||||
private Consumer<SlashCommandIdentifier> consumer;
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
public SlashCommandParser(Method method, Consumer<SlashCommandIdentifier> consumer) {
|
||||
this.method = method;
|
||||
this.consumer = consumer;
|
||||
public SlashCommandParser() {
|
||||
}
|
||||
|
||||
@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);
|
||||
|
||||
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);
|
||||
consumer.accept((SlashCommandIdentifier) lastIdentifier);
|
||||
return lastIdentifier;
|
||||
}
|
||||
|
||||
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");
|
||||
throw new RuntimeException(method.getName() + ": Can't have SlashCommand Annotation on Class and Method");
|
||||
}
|
||||
|
||||
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) &&
|
||||
!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");
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user