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