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