feat(core): add ReflectedMethod and Processors
This commit is contained in:
parent
acbe6b23ef
commit
075f0e13c9
@ -1,7 +1,9 @@
|
||||
package net.tomatentum.cutin;
|
||||
|
||||
public interface MethodProcessor {
|
||||
import net.tomatentum.cutin.container.MethodContainer;
|
||||
|
||||
void process(Object context);
|
||||
public interface MethodProcessor<I extends Object> {
|
||||
|
||||
void process(Object context, MethodContainer<I> methodContainer);
|
||||
|
||||
}
|
||||
|
@ -1,7 +1,11 @@
|
||||
package net.tomatentum.cutin;
|
||||
|
||||
public interface ProcessorContainer {
|
||||
import java.util.Collection;
|
||||
|
||||
ProcessorContainer addProcessor(MethodProcessor processor);
|
||||
public interface ProcessorContainer<I> {
|
||||
|
||||
ProcessorContainer<I> addProcessor(MethodProcessor<I> processor);
|
||||
|
||||
Collection<MethodProcessor<I>> processor();
|
||||
|
||||
}
|
||||
|
@ -1,31 +1,41 @@
|
||||
package net.tomatentum.cutin;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class ProcessorMethodExecutor implements MethodExecutor, ProcessorContainer {
|
||||
import net.tomatentum.cutin.container.MethodContainer;
|
||||
|
||||
public class ProcessorMethodExecutor<I extends Object> implements MethodExecutor, ProcessorContainer<I> {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
private Set<MethodProcessor> processors;
|
||||
private MethodContainer<I> methodContainer;
|
||||
private Set<MethodProcessor<I>> processors;
|
||||
|
||||
public ProcessorMethodExecutor() {
|
||||
public ProcessorMethodExecutor(MethodContainer<I> methodContainer) {
|
||||
this.methodContainer = methodContainer;
|
||||
this.processors = new HashSet<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ProcessorContainer addProcessor(MethodProcessor processor) {
|
||||
public ProcessorContainer<I> addProcessor(MethodProcessor<I> processor) {
|
||||
processors.add(processor);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<MethodProcessor<I>> processor() {
|
||||
return this.processors;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(Object context) {
|
||||
logger.debug("Received {} interaction ", context);
|
||||
processors.forEach(x -> x.process(context));
|
||||
logger.debug("Handling {} context ", context);
|
||||
processors.forEach(x -> x.process(context, methodContainer));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -12,15 +12,15 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
import net.tomatentum.cutin.util.ReflectionUtil;
|
||||
|
||||
public abstract class ReflectedMethod {
|
||||
public abstract class ReflectedMethod<I extends Object> {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
private Method method;
|
||||
private Object containingObject;
|
||||
protected Method method;
|
||||
protected Object containingObject;
|
||||
|
||||
public ReflectedMethod(Method method, Object containingObject) {
|
||||
if (!Arrays.asList(containingObject.getClass().getMethods()).contains(method))
|
||||
protected ReflectedMethod(Method method, Object containingObject) {
|
||||
if (!Arrays.asList(containingObject.getClass().getDeclaredMethods()).contains(method))
|
||||
throw new InvalidParameterException("Method does not apply to specified handler");
|
||||
this.method = method;
|
||||
this.containingObject = containingObject;
|
||||
@ -28,6 +28,8 @@ public abstract class ReflectedMethod {
|
||||
|
||||
public abstract Object getParameter(Object context, int index);
|
||||
|
||||
public abstract I identifier();
|
||||
|
||||
public Object run(Object context) {
|
||||
method.setAccessible(true);
|
||||
try {
|
||||
|
@ -27,17 +27,17 @@ public class ReflectedMethodFactoryImpl implements ReflectedMethodFactory {
|
||||
|
||||
@Override
|
||||
public Optional<ReflectedMethod> produce(Method method, Object containingClass) {
|
||||
Optional<ReflectedMethod> imethod = this.factories.stream()
|
||||
Optional<ReflectedMethod> rmethod = this.factories.stream()
|
||||
.map(f -> factoryProduce(f, method, containingClass))
|
||||
.filter(Optional::isPresent)
|
||||
.map(Optional::get)
|
||||
.findFirst();
|
||||
|
||||
if (imethod.isEmpty()) {
|
||||
if (rmethod.isEmpty()) {
|
||||
logger.debug("Could not produce a ReflectedMethod for Method {}", ReflectionUtil.getFullMethodName(method));
|
||||
}
|
||||
|
||||
return imethod;
|
||||
return rmethod;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
x
Reference in New Issue
Block a user