refactor(method): make use of optionals in ReflectedMethodFactory
This commit is contained in:
parent
450f1fdaa1
commit
0114cffcbd
@ -2,6 +2,7 @@ package net.tomatentum.marinara.interaction.components.methods;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import net.tomatentum.marinara.Marinara;
|
||||
import net.tomatentum.marinara.interaction.InteractionHandler;
|
||||
@ -39,13 +40,14 @@ public class ButtonInteractionMethod extends InteractionMethod {
|
||||
public static class Factory extends InteractionMethod.Factory {
|
||||
|
||||
@Override
|
||||
public ReflectedMethod produce(Marinara marinara, Method method, Object containingObject) {
|
||||
if (!method.isAnnotationPresent(Button.class) ||
|
||||
!(containingObject instanceof InteractionHandler)
|
||||
public Optional<ReflectedMethod> produce(Marinara marinara, Method method, Object containingObject) {
|
||||
ReflectedMethod rMethod = null;
|
||||
if (method.isAnnotationPresent(Button.class) &&
|
||||
(containingObject instanceof InteractionHandler)
|
||||
)
|
||||
return null;
|
||||
rMethod = new ButtonInteractionMethod(method, (InteractionHandler) containingObject, marinara);
|
||||
|
||||
return new ButtonInteractionMethod(method, (InteractionHandler) containingObject, marinara);
|
||||
return Optional.ofNullable(rMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -2,6 +2,7 @@ package net.tomatentum.marinara.interaction.methods;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import net.tomatentum.marinara.Marinara;
|
||||
import net.tomatentum.marinara.interaction.InteractionHandler;
|
||||
@ -47,14 +48,15 @@ public class AutoCompleteInteractionMethod extends InteractionMethod {
|
||||
public static class Factory extends InteractionMethod.Factory {
|
||||
|
||||
@Override
|
||||
public ReflectedMethod produce(Marinara marinara, Method method, Object containingObject) {
|
||||
if (!(containingObject instanceof InteractionHandler) ||
|
||||
!method.isAnnotationPresent(AutoComplete.class) ||
|
||||
(method.isAnnotationPresent(SlashCommand.class) ||
|
||||
public Optional<ReflectedMethod> produce(Marinara marinara, Method method, Object containingObject) {
|
||||
ReflectedMethod rMethod = null;
|
||||
if ((containingObject instanceof InteractionHandler) &&
|
||||
method.isAnnotationPresent(AutoComplete.class) &&
|
||||
!(method.isAnnotationPresent(SlashCommand.class) ||
|
||||
method.isAnnotationPresent(SubCommand.class)))
|
||||
return null;
|
||||
rMethod = new AutoCompleteInteractionMethod(method, (InteractionHandler) containingObject, marinara);
|
||||
|
||||
return new AutoCompleteInteractionMethod(method, (InteractionHandler) containingObject, marinara);
|
||||
return Optional.ofNullable(rMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -2,6 +2,7 @@ package net.tomatentum.marinara.interaction.methods;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import net.tomatentum.marinara.Marinara;
|
||||
import net.tomatentum.marinara.interaction.InteractionHandler;
|
||||
@ -35,13 +36,13 @@ public class SlashCommandInteractionMethod extends InteractionMethod {
|
||||
public static class Factory extends InteractionMethod.Factory {
|
||||
|
||||
@Override
|
||||
public ReflectedMethod produce(Marinara marinara, Method method, Object containingObject) {
|
||||
if (!(containingObject instanceof InteractionHandler) ||
|
||||
!(method.isAnnotationPresent(SlashCommand.class) ||
|
||||
public Optional<ReflectedMethod> produce(Marinara marinara, Method method, Object containingObject) {
|
||||
ReflectedMethod rMethod = null;
|
||||
if ((containingObject instanceof InteractionHandler) &&
|
||||
(method.isAnnotationPresent(SlashCommand.class) ||
|
||||
method.isAnnotationPresent(SubCommand.class)))
|
||||
return null;
|
||||
|
||||
return new SlashCommandInteractionMethod(method, (InteractionHandler) containingObject, marinara);
|
||||
rMethod = new SlashCommandInteractionMethod(method, (InteractionHandler) containingObject, marinara);
|
||||
return Optional.ofNullable(rMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -2,17 +2,18 @@ package net.tomatentum.marinara.reflection;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import net.tomatentum.marinara.Marinara;
|
||||
import net.tomatentum.marinara.parser.AnnotationParser;
|
||||
|
||||
public interface ReflectedMethodFactory {
|
||||
ReflectedMethod produce(Method method, Object containingClass);
|
||||
Optional<ReflectedMethod> produce(Method method, Object containingClass);
|
||||
ReflectedMethodFactory addFactory(Factory factory);
|
||||
|
||||
public interface Factory {
|
||||
|
||||
ReflectedMethod produce(Marinara marinara, Method method, Object containingObject);
|
||||
Optional<ReflectedMethod> produce(Marinara marinara, Method method, Object containingObject);
|
||||
void addParser(ReflectedMethod method, List<AnnotationParser> parser);
|
||||
|
||||
}
|
||||
|
@ -3,7 +3,6 @@ package net.tomatentum.marinara.reflection;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
@ -30,18 +29,18 @@ public class ReflectedMethodFactoryImpl implements ReflectedMethodFactory {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReflectedMethod produce(Method method, Object containingClass) {
|
||||
public Optional<ReflectedMethod> produce(Method method, Object containingClass) {
|
||||
Optional<ReflectedMethod> imethod = this.factories.stream()
|
||||
.map(f -> factoryProduce(f, method, containingClass))
|
||||
.filter(Objects::nonNull)
|
||||
.filter(Optional::isPresent)
|
||||
.map(Optional::get)
|
||||
.findFirst();
|
||||
|
||||
if (imethod.isEmpty()) {
|
||||
logger.debug("Could not produce a ReflectedMethod for Method {}", ReflectionUtil.getFullMethodName(method));
|
||||
return null;
|
||||
}
|
||||
|
||||
return imethod.get();
|
||||
return imethod;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -50,13 +49,13 @@ public class ReflectedMethodFactoryImpl implements ReflectedMethodFactory {
|
||||
return this;
|
||||
}
|
||||
|
||||
private ReflectedMethod factoryProduce(Factory factory, Method method, Object containingClass) {
|
||||
private Optional<ReflectedMethod> factoryProduce(Factory factory, Method method, Object containingClass) {
|
||||
List<AnnotationParser> parser = new ArrayList<>();
|
||||
ReflectedMethod m = factory.produce(this.marinara, method, containingClass);
|
||||
if (m != null) {
|
||||
factory.addParser(m, parser);
|
||||
Optional<ReflectedMethod> m = factory.produce(this.marinara, method, containingClass);
|
||||
m.ifPresent(x -> {
|
||||
factory.addParser(x, parser);
|
||||
parser.forEach(AnnotationParser::parse);
|
||||
}
|
||||
});
|
||||
return m;
|
||||
}
|
||||
|
||||
|
@ -44,12 +44,14 @@ public class InteractionRegistry {
|
||||
|
||||
public void addInteractions(InteractionHandler interactionHandler) {
|
||||
for (Method method : interactionHandler.getClass().getDeclaredMethods()) {
|
||||
ReflectedMethod rMethod = this.marinara.getReflectedMethodFactory().produce(method, interactionHandler);
|
||||
if (rMethod != null && rMethod instanceof InteractionMethod) {
|
||||
InteractionMethod iMethod = (InteractionMethod) rMethod;
|
||||
InteractionEntry.findEntry(interactions, iMethod.identifier()).addMethod(iMethod);
|
||||
logger.debug("Added {} method from {}", iMethod.method().getName(), interactionHandler.getClass().getSimpleName());
|
||||
}
|
||||
Optional<ReflectedMethod> rMethod = this.marinara.getReflectedMethodFactory().produce(method, interactionHandler);
|
||||
rMethod.ifPresent(x -> {
|
||||
if (x instanceof InteractionMethod) {
|
||||
InteractionMethod iMethod = (InteractionMethod) x;
|
||||
InteractionEntry.findEntry(interactions, iMethod.identifier()).addMethod(iMethod);
|
||||
logger.debug("Added {} method from {}", iMethod.method().getName(), interactionHandler.getClass().getSimpleName());
|
||||
}
|
||||
});
|
||||
}
|
||||
logger.info("Added all Interactions from {}", interactionHandler.getClass().getSimpleName());
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user