Compare commits
No commits in common. "404f221ccf8538d6ef38cf96b5e4d2e0c4157033" and "76ab779ab2edd626fb8b7251288ad4ac9bc4d13c" have entirely different histories.
404f221ccf
...
76ab779ab2
@ -1,15 +1,10 @@
|
|||||||
package net.tomatentum.marinara;
|
package net.tomatentum.marinara;
|
||||||
|
|
||||||
import org.apache.logging.log4j.Logger;
|
|
||||||
|
|
||||||
import net.tomatentum.marinara.registry.InteractionCheckRegistry;
|
import net.tomatentum.marinara.registry.InteractionCheckRegistry;
|
||||||
import net.tomatentum.marinara.registry.InteractionRegistry;
|
import net.tomatentum.marinara.registry.InteractionRegistry;
|
||||||
import net.tomatentum.marinara.util.LoggerUtil;
|
|
||||||
import net.tomatentum.marinara.wrapper.LibraryWrapper;
|
import net.tomatentum.marinara.wrapper.LibraryWrapper;
|
||||||
|
|
||||||
public class Marinara {
|
public class Marinara {
|
||||||
|
|
||||||
private Logger logger = LoggerUtil.getLogger(getClass());
|
|
||||||
|
|
||||||
public static <T extends LibraryWrapper> Marinara load(LibraryWrapper wrapper) {
|
public static <T extends LibraryWrapper> Marinara load(LibraryWrapper wrapper) {
|
||||||
return new Marinara(wrapper);
|
return new Marinara(wrapper);
|
||||||
@ -23,7 +18,6 @@ public class Marinara {
|
|||||||
this.wrapper = wrapper;
|
this.wrapper = wrapper;
|
||||||
this.registry = new InteractionRegistry(this);
|
this.registry = new InteractionRegistry(this);
|
||||||
this.checkRegistry = new InteractionCheckRegistry();
|
this.checkRegistry = new InteractionCheckRegistry();
|
||||||
logger.info("Marinara loaded successfully!");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public InteractionRegistry getRegistry() {
|
public InteractionRegistry getRegistry() {
|
||||||
|
@ -5,14 +5,9 @@ import java.lang.reflect.InvocationTargetException;
|
|||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
import org.apache.logging.log4j.Logger;
|
|
||||||
|
|
||||||
import net.tomatentum.marinara.util.LoggerUtil;
|
|
||||||
import net.tomatentum.marinara.util.ReflectionUtil;
|
import net.tomatentum.marinara.util.ReflectionUtil;
|
||||||
|
|
||||||
public record AppliedCheck(InteractionCheck<?> check, Annotation annotation) {
|
public record AppliedCheck(InteractionCheck<?> check, Annotation annotation) {
|
||||||
|
|
||||||
private static Logger logger = LoggerUtil.getLogger(AppliedCheck.class);
|
|
||||||
|
|
||||||
public boolean pre(Object context) {
|
public boolean pre(Object context) {
|
||||||
Method[] methods = Arrays.stream(check.getClass().getMethods())
|
Method[] methods = Arrays.stream(check.getClass().getMethods())
|
||||||
@ -22,10 +17,9 @@ public record AppliedCheck(InteractionCheck<?> check, Annotation annotation) {
|
|||||||
Method method = ReflectionUtil.getMostSpecificMethod(methods, context.getClass(), annotation.annotationType());
|
Method method = ReflectionUtil.getMostSpecificMethod(methods, context.getClass(), annotation.annotationType());
|
||||||
method.setAccessible(true);
|
method.setAccessible(true);
|
||||||
try {
|
try {
|
||||||
logger.debug("Executing pre check {} with context {}", check.getClass().getName(), context.toString());
|
|
||||||
return (boolean) method.invoke(check, context, annotation);
|
return (boolean) method.invoke(check, context, annotation);
|
||||||
} catch (IllegalAccessException | InvocationTargetException | SecurityException e) {
|
} catch (IllegalAccessException | InvocationTargetException | SecurityException e) {
|
||||||
logger.fatal(e);
|
e.printStackTrace();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -38,10 +32,9 @@ public record AppliedCheck(InteractionCheck<?> check, Annotation annotation) {
|
|||||||
Method method = ReflectionUtil.getMostSpecificMethod(methods, context.getClass(), annotation.annotationType());
|
Method method = ReflectionUtil.getMostSpecificMethod(methods, context.getClass(), annotation.annotationType());
|
||||||
method.setAccessible(true);
|
method.setAccessible(true);
|
||||||
try {
|
try {
|
||||||
logger.debug("Executing pre check {} with context {}", check.getClass().getName(), context.toString());
|
|
||||||
method.invoke(check, context, annotation);
|
method.invoke(check, context, annotation);
|
||||||
} catch (IllegalAccessException | InvocationTargetException | SecurityException e) {
|
} catch (IllegalAccessException | InvocationTargetException | SecurityException e) {
|
||||||
logger.fatal(e);
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,8 +7,6 @@ import java.util.ArrayList;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.apache.logging.log4j.Logger;
|
|
||||||
|
|
||||||
import net.tomatentum.marinara.Marinara;
|
import net.tomatentum.marinara.Marinara;
|
||||||
import net.tomatentum.marinara.checks.AppliedCheck;
|
import net.tomatentum.marinara.checks.AppliedCheck;
|
||||||
import net.tomatentum.marinara.interaction.InteractionHandler;
|
import net.tomatentum.marinara.interaction.InteractionHandler;
|
||||||
@ -18,8 +16,6 @@ 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.parser.AnnotationParser;
|
||||||
import net.tomatentum.marinara.parser.InteractionCheckParser;
|
import net.tomatentum.marinara.parser.InteractionCheckParser;
|
||||||
import net.tomatentum.marinara.util.LoggerUtil;
|
|
||||||
import net.tomatentum.marinara.util.ReflectionUtil;
|
|
||||||
|
|
||||||
public abstract class InteractionMethod {
|
public abstract class InteractionMethod {
|
||||||
|
|
||||||
@ -37,8 +33,6 @@ public abstract class InteractionMethod {
|
|||||||
protected List<AnnotationParser> parsers;
|
protected List<AnnotationParser> parsers;
|
||||||
protected List<AppliedCheck> appliedChecks;
|
protected List<AppliedCheck> appliedChecks;
|
||||||
|
|
||||||
private Logger logger = LoggerUtil.getLogger(getClass());
|
|
||||||
|
|
||||||
protected InteractionMethod(Method method,
|
protected InteractionMethod(Method method,
|
||||||
InteractionHandler handler,
|
InteractionHandler handler,
|
||||||
Marinara marinara
|
Marinara marinara
|
||||||
@ -88,14 +82,11 @@ public abstract class InteractionMethod {
|
|||||||
List<Object> parameters = new ArrayList<>();
|
List<Object> parameters = new ArrayList<>();
|
||||||
|
|
||||||
for (int i = 0; i < parameterCount; i++) {
|
for (int i = 0; i < parameterCount; i++) {
|
||||||
Object parameter;
|
|
||||||
if (i == 0) {
|
if (i == 0) {
|
||||||
parameter = context;
|
parameters.add(context);
|
||||||
}else
|
continue;
|
||||||
parameter = getParameter(context, i-1);
|
}
|
||||||
|
parameters.add(getParameter(context, i-1));
|
||||||
logger.trace("Found parameter {}={} for method {}", parameter.getClass().toString(), parameter, ReflectionUtil.getFullMethodName(method));
|
|
||||||
parameters.add(parameter);
|
|
||||||
}
|
}
|
||||||
return parameters.toArray();
|
return parameters.toArray();
|
||||||
}
|
}
|
||||||
|
@ -3,19 +3,13 @@ package net.tomatentum.marinara.parser;
|
|||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
import org.apache.logging.log4j.Logger;
|
|
||||||
|
|
||||||
import net.tomatentum.marinara.interaction.annotation.Button;
|
import net.tomatentum.marinara.interaction.annotation.Button;
|
||||||
import net.tomatentum.marinara.util.LoggerUtil;
|
|
||||||
import net.tomatentum.marinara.util.ReflectionUtil;
|
|
||||||
|
|
||||||
public class ButtonParser implements AnnotationParser {
|
public class ButtonParser implements AnnotationParser {
|
||||||
|
|
||||||
private Method method;
|
private Method method;
|
||||||
private Consumer<String> consumer;
|
private Consumer<String> consumer;
|
||||||
|
|
||||||
private Logger logger = LoggerUtil.getLogger(getClass());
|
|
||||||
|
|
||||||
public ButtonParser(Method method, Consumer<String> consumer) {
|
public ButtonParser(Method method, Consumer<String> consumer) {
|
||||||
this.method = method;
|
this.method = method;
|
||||||
this.consumer = consumer;
|
this.consumer = consumer;
|
||||||
@ -24,7 +18,6 @@ public class ButtonParser implements AnnotationParser {
|
|||||||
@Override
|
@Override
|
||||||
public void parse() {
|
public void parse() {
|
||||||
Button button = getMethod().getAnnotation(Button.class);
|
Button button = getMethod().getAnnotation(Button.class);
|
||||||
logger.trace("Parsed Button annotation {} for method {}", button.toString(), ReflectionUtil.getFullMethodName(method));
|
|
||||||
this.consumer.accept(button.value());
|
this.consumer.accept(button.value());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,13 +6,9 @@ import java.util.Arrays;
|
|||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
import org.apache.logging.log4j.Logger;
|
|
||||||
|
|
||||||
import net.tomatentum.marinara.checks.AppliedCheck;
|
import net.tomatentum.marinara.checks.AppliedCheck;
|
||||||
import net.tomatentum.marinara.checks.InteractionCheck;
|
import net.tomatentum.marinara.checks.InteractionCheck;
|
||||||
import net.tomatentum.marinara.registry.InteractionCheckRegistry;
|
import net.tomatentum.marinara.registry.InteractionCheckRegistry;
|
||||||
import net.tomatentum.marinara.util.LoggerUtil;
|
|
||||||
import net.tomatentum.marinara.util.ReflectionUtil;
|
|
||||||
|
|
||||||
public class InteractionCheckParser implements AnnotationParser {
|
public class InteractionCheckParser implements AnnotationParser {
|
||||||
|
|
||||||
@ -20,8 +16,6 @@ public class InteractionCheckParser implements AnnotationParser {
|
|||||||
private Method method;
|
private Method method;
|
||||||
private Consumer<AppliedCheck> consumer;
|
private Consumer<AppliedCheck> consumer;
|
||||||
|
|
||||||
private Logger logger = LoggerUtil.getLogger(getClass());
|
|
||||||
|
|
||||||
public InteractionCheckParser(Method method, Consumer<AppliedCheck> consumer, InteractionCheckRegistry checkRegistry) {
|
public InteractionCheckParser(Method method, Consumer<AppliedCheck> consumer, InteractionCheckRegistry checkRegistry) {
|
||||||
this.checkRegistry = checkRegistry;
|
this.checkRegistry = checkRegistry;
|
||||||
this.method = method;
|
this.method = method;
|
||||||
@ -36,11 +30,8 @@ public class InteractionCheckParser implements AnnotationParser {
|
|||||||
|
|
||||||
private void convertAnnotation(Annotation annotation) {
|
private void convertAnnotation(Annotation annotation) {
|
||||||
Optional<InteractionCheck<?>> check = this.checkRegistry.getCheckFromAnnotation(annotation.annotationType());
|
Optional<InteractionCheck<?>> check = this.checkRegistry.getCheckFromAnnotation(annotation.annotationType());
|
||||||
if (check.isPresent()) {
|
if (check.isPresent())
|
||||||
AppliedCheck appliedCheck = new AppliedCheck(check.get(), annotation);
|
consumer.accept(new AppliedCheck(check.get(), annotation));
|
||||||
logger.trace("Parsed InteractionCheck {} for annotation {} for method {}", check.getClass().getName(), annotation.toString(), ReflectionUtil.getFullMethodName(method));
|
|
||||||
consumer.accept(appliedCheck);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -3,13 +3,10 @@ package net.tomatentum.marinara.parser;
|
|||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
import org.apache.logging.log4j.Logger;
|
|
||||||
|
|
||||||
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.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.util.LoggerUtil;
|
|
||||||
import net.tomatentum.marinara.util.ReflectionUtil;
|
import net.tomatentum.marinara.util.ReflectionUtil;
|
||||||
|
|
||||||
public class SlashCommandParser implements AnnotationParser {
|
public class SlashCommandParser implements AnnotationParser {
|
||||||
@ -17,8 +14,6 @@ public class SlashCommandParser implements AnnotationParser {
|
|||||||
private Method method;
|
private Method method;
|
||||||
private Consumer<ExecutableSlashCommandDefinition> consumer;
|
private Consumer<ExecutableSlashCommandDefinition> consumer;
|
||||||
|
|
||||||
private Logger logger = LoggerUtil.getLogger(getClass());
|
|
||||||
|
|
||||||
public SlashCommandParser(Method method, Consumer<ExecutableSlashCommandDefinition> consumer) {
|
public SlashCommandParser(Method method, Consumer<ExecutableSlashCommandDefinition> consumer) {
|
||||||
this.method = method;
|
this.method = method;
|
||||||
this.consumer = consumer;
|
this.consumer = consumer;
|
||||||
@ -42,9 +37,6 @@ public class SlashCommandParser implements AnnotationParser {
|
|||||||
builder.setSubCommand(subCmd);
|
builder.setSubCommand(subCmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
ExecutableSlashCommandDefinition def = builder.build();
|
|
||||||
|
|
||||||
logger.trace("Parsed using SlashCommandParser for method {} with the result:\n{}", ReflectionUtil.getFullMethodName(method), def.toString());
|
|
||||||
consumer.accept(builder.build());
|
consumer.accept(builder.build());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,25 +6,19 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import org.apache.logging.log4j.Logger;
|
|
||||||
|
|
||||||
import io.leangen.geantyref.GenericTypeReflector;
|
import io.leangen.geantyref.GenericTypeReflector;
|
||||||
import net.tomatentum.marinara.checks.InteractionCheck;
|
import net.tomatentum.marinara.checks.InteractionCheck;
|
||||||
import net.tomatentum.marinara.util.LoggerUtil;
|
|
||||||
|
|
||||||
public class InteractionCheckRegistry {
|
public class InteractionCheckRegistry {
|
||||||
|
|
||||||
private List<InteractionCheck<?>> checks;
|
private List<InteractionCheck<?>> checks;
|
||||||
|
|
||||||
private Logger logger = LoggerUtil.getLogger(getClass());
|
|
||||||
|
|
||||||
public InteractionCheckRegistry() {
|
public InteractionCheckRegistry() {
|
||||||
this.checks = new ArrayList<>();
|
this.checks = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addCheck(InteractionCheck<?> check) {
|
public void addCheck(InteractionCheck<?> check) {
|
||||||
checks.add(check);
|
checks.add(check);
|
||||||
logger.info("Registered Check {}", check.getClass().getName());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<InteractionCheck<?>> getCheckFromAnnotation(Type annotation) {
|
public Optional<InteractionCheck<?>> getCheckFromAnnotation(Type annotation) {
|
||||||
|
@ -100,8 +100,4 @@ public final class ReflectionUtil {
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getFullMethodName(Method method) {
|
|
||||||
return method.getClass().getName() + "." + method.getName();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user