refactor(interaction): seperate InteractionMethod to ReflectedMethod
This commit is contained in:
parent
0590789359
commit
0973016a74
@ -1,4 +1,4 @@
|
||||
package net.tomatentum.marinara.interaction.methods;
|
||||
package net.tomatentum.marinara.interaction.components.methods;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
@ -6,6 +6,7 @@ import net.tomatentum.marinara.Marinara;
|
||||
import net.tomatentum.marinara.interaction.InteractionHandler;
|
||||
import net.tomatentum.marinara.interaction.InteractionType;
|
||||
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||
import net.tomatentum.marinara.interaction.methods.InteractionMethod;
|
||||
import net.tomatentum.marinara.parser.AnnotationParser;
|
||||
import net.tomatentum.marinara.parser.ButtonParser;
|
||||
|
||||
@ -13,14 +14,14 @@ public class ButtonInteractionMethod extends InteractionMethod {
|
||||
|
||||
private String customId;
|
||||
|
||||
ButtonInteractionMethod(Method method, InteractionHandler handler, Marinara marinara) {
|
||||
public ButtonInteractionMethod(Method method, InteractionHandler handler, Marinara marinara) {
|
||||
super(method, handler, marinara);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AnnotationParser[] parsers() {
|
||||
public AnnotationParser[] provideParsers() {
|
||||
return new AnnotationParser[] {
|
||||
new ButtonParser(method, (x) -> { this.customId = x; } )
|
||||
new ButtonParser(method(), (x) -> { this.customId = x; } )
|
||||
};
|
||||
}
|
||||
|
@ -20,9 +20,9 @@ public class AutoCompleteInteractionMethod extends InteractionMethod {
|
||||
}
|
||||
|
||||
@Override
|
||||
public AnnotationParser[] parsers() {
|
||||
public AnnotationParser[] provideParsers() {
|
||||
return new AnnotationParser[] {
|
||||
new SlashCommandParser(method, true, (x) -> { this.interactionIdentifier = x; } )
|
||||
new SlashCommandParser(method(), true, (x) -> { this.interactionIdentifier = x; } )
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -1,14 +1,9 @@
|
||||
package net.tomatentum.marinara.interaction.methods;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.security.InvalidParameterException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import net.tomatentum.marinara.Marinara;
|
||||
import net.tomatentum.marinara.checks.AppliedCheck;
|
||||
import net.tomatentum.marinara.interaction.InteractionHandler;
|
||||
@ -16,13 +11,13 @@ import net.tomatentum.marinara.interaction.annotation.AutoComplete;
|
||||
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.interaction.components.methods.ButtonInteractionMethod;
|
||||
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||
import net.tomatentum.marinara.parser.AnnotationParser;
|
||||
import net.tomatentum.marinara.parser.InteractionCheckParser;
|
||||
import net.tomatentum.marinara.util.LoggerUtil;
|
||||
import net.tomatentum.marinara.util.ReflectionUtil;
|
||||
import net.tomatentum.marinara.util.ReflectedMethod;
|
||||
|
||||
public abstract class InteractionMethod {
|
||||
public abstract class InteractionMethod extends ReflectedMethod {
|
||||
|
||||
public static InteractionMethod create(Method method, InteractionHandler handler, Marinara marinara) {
|
||||
if (method.isAnnotationPresent(AutoComplete.class))
|
||||
@ -34,72 +29,39 @@ public abstract class InteractionMethod {
|
||||
return null;
|
||||
}
|
||||
|
||||
protected Method method;
|
||||
protected InteractionHandler handler;
|
||||
protected Marinara marinara;
|
||||
protected List<AnnotationParser> parsers;
|
||||
protected List<AppliedCheck> appliedChecks;
|
||||
|
||||
private Logger logger = LoggerUtil.getLogger(getClass());
|
||||
|
||||
protected InteractionMethod(
|
||||
Method method,
|
||||
InteractionHandler handler,
|
||||
Marinara marinara
|
||||
) {
|
||||
if (!Arrays.asList(handler.getClass().getMethods()).contains(method))
|
||||
throw new InvalidParameterException("Method does not apply to specified handler");
|
||||
|
||||
this.method = method;
|
||||
this.handler = handler;
|
||||
super(method, handler);
|
||||
this.marinara = marinara;
|
||||
this.parsers = new ArrayList<>(Arrays.asList(parsers()));
|
||||
this.appliedChecks = new ArrayList<>();
|
||||
|
||||
parsers.add(new InteractionCheckParser(method, appliedChecks::add, marinara.getCheckRegistry()));
|
||||
|
||||
parsers.stream().forEach(AnnotationParser::parse);
|
||||
}
|
||||
|
||||
public void run(Object context) {
|
||||
@Override
|
||||
public AnnotationParser[] provideParsers() {
|
||||
return new AnnotationParser[] {
|
||||
new InteractionCheckParser(method(), appliedChecks::add, marinara.getCheckRegistry())
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object run(Object context) {
|
||||
Object result = null;
|
||||
if (this.appliedChecks.stream().filter(x -> !x.pre(context)).count() > 0)
|
||||
return;
|
||||
return null;
|
||||
|
||||
method.setAccessible(true);
|
||||
try {
|
||||
method.invoke(handler, getParameters(context));
|
||||
}catch (IllegalAccessException | InvocationTargetException ex) {
|
||||
logger.error("InteractionMethod failed to run", ex);
|
||||
}
|
||||
result = super.run(context);
|
||||
|
||||
this.appliedChecks.forEach(x -> x.post(context));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public abstract AnnotationParser[] parsers();
|
||||
|
||||
public abstract Object getParameter(Object context, int index);
|
||||
|
||||
public abstract InteractionIdentifier identifier();
|
||||
|
||||
public Method method() {
|
||||
return method;
|
||||
}
|
||||
|
||||
private Object[] getParameters(Object context) {
|
||||
int parameterCount = method.getParameterCount();
|
||||
List<Object> parameters = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < parameterCount; i++) {
|
||||
Object parameter;
|
||||
if (i == 0) {
|
||||
parameter = context;
|
||||
}else
|
||||
parameter = getParameter(context, i-1);
|
||||
|
||||
logger.trace("Found parameter {}={} for method {}", parameter != null ? parameter.getClass().toString() : " ", parameter, ReflectionUtil.getFullMethodName(method));
|
||||
parameters.add(parameter);
|
||||
}
|
||||
return parameters.toArray();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -18,9 +18,9 @@ public class SlashCommandInteractionMethod extends InteractionMethod {
|
||||
}
|
||||
|
||||
@Override
|
||||
public AnnotationParser[] parsers() {
|
||||
public AnnotationParser[] provideParsers() {
|
||||
return new AnnotationParser[] {
|
||||
new SlashCommandParser(method, false, (x) -> { this.interactionIdentifier = x; } )
|
||||
new SlashCommandParser(method(), false, (x) -> { this.interactionIdentifier = x; } )
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,75 @@
|
||||
package net.tomatentum.marinara.util;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.security.InvalidParameterException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import net.tomatentum.marinara.parser.AnnotationParser;
|
||||
|
||||
public abstract class ReflectedMethod {
|
||||
|
||||
private Logger logger = LoggerUtil.getLogger(getClass());
|
||||
|
||||
private Method method;
|
||||
private Object containingObject;
|
||||
protected List<AnnotationParser> parsers;
|
||||
|
||||
public ReflectedMethod(Method method, Object containingObject) {
|
||||
if (!Arrays.asList(containingObject.getClass().getMethods()).contains(method))
|
||||
throw new InvalidParameterException("Method does not apply to specified handler");
|
||||
this.method = method;
|
||||
this.containingObject = containingObject;
|
||||
this.parsers = new ArrayList<>(Arrays.asList(provideParsers()));
|
||||
|
||||
this.parsers.stream().forEach(AnnotationParser::parse);
|
||||
}
|
||||
|
||||
public abstract Object getParameter(Object context, int index);
|
||||
|
||||
public abstract AnnotationParser[] provideParsers();
|
||||
|
||||
public Object run(Object context) {
|
||||
method.setAccessible(true);
|
||||
try {
|
||||
return method.invoke(containingObject, getParameters(context));
|
||||
}catch (IllegalAccessException | InvocationTargetException ex) {
|
||||
logger.error("ReflectedMethod failed to run", ex);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public Method method() {
|
||||
return this.method;
|
||||
}
|
||||
|
||||
public Object containingObject() {
|
||||
return this.containingObject;
|
||||
}
|
||||
|
||||
public List<AnnotationParser> parsers() {
|
||||
return this.parsers;
|
||||
}
|
||||
|
||||
private Object[] getParameters(Object context) {
|
||||
int parameterCount = method.getParameterCount();
|
||||
List<Object> parameters = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < parameterCount; i++) {
|
||||
Object parameter;
|
||||
if (i == 0) {
|
||||
parameter = context;
|
||||
}else
|
||||
parameter = getParameter(context, i-1);
|
||||
|
||||
logger.trace("Found parameter {}={} for method {}", parameter != null ? parameter.getClass().toString() : " ", parameter, ReflectionUtil.getFullMethodName(method));
|
||||
parameters.add(parameter);
|
||||
}
|
||||
return parameters.toArray();
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user