diff --git a/lib/src/main/java/net/tomatentum/cutin/method/BestCandidateMethod.java b/lib/src/main/java/net/tomatentum/cutin/method/BestCandidateMethod.java index 618a7cd..4919782 100644 --- a/lib/src/main/java/net/tomatentum/cutin/method/BestCandidateMethod.java +++ b/lib/src/main/java/net/tomatentum/cutin/method/BestCandidateMethod.java @@ -1,44 +1,23 @@ package net.tomatentum.cutin.method; import java.lang.reflect.Method; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Objects; import java.util.Optional; -import java.util.stream.Stream; import net.tomatentum.cutin.ReflectedMethodFactory; import net.tomatentum.cutin.container.MethodContainer; import net.tomatentum.cutin.util.ReflectionUtil; -public class BestCandidateMethod extends ReflectedMethod { +public abstract class BestCandidateMethod extends ReflectedMethod { private String methodName; - private I identifier; - private List additionalParameters; - public BestCandidateMethod(String methodName, Object containingObject, I identifier, Object... additionalParameters) { + protected BestCandidateMethod(String methodName, Object containingObject) { super(getMethod(containingObject, methodName), containingObject); this.methodName = methodName; - this.identifier = identifier; - this.additionalParameters = Arrays.asList(additionalParameters); - } - - public BestCandidateMethod(String methodName, Object containingObject, Object... additionalParameters) { - super(getMethod(containingObject, methodName), containingObject); - this.methodName = methodName; - this.identifier = null; - this.additionalParameters = Arrays.asList(additionalParameters); - } - - @Override - public Object getParameter(Object context, int index) { - return additionalParameters.get(index); - } - - @Override - public I identifier() { - return this.identifier; } @Override @@ -47,10 +26,9 @@ public class BestCandidateMethod extends Ref .filter(x -> x.getName().equals(methodName)) .filter(x -> !x.isBridge()) .toArray(Method[]::new); - Class[] parameters = Stream.concat( - Stream.of(context.getClass()), - additionalParameters.stream().map(Object::getClass) - ).toArray(Class[]::new); + Class[] parameters = getCurrentParameterList(context).stream() + .map(Object::getClass) + .toArray(Class[]::new); super.method = ReflectionUtil.getMostSpecificMethod(methods, parameters); return super.run(context); } @@ -61,11 +39,6 @@ public class BestCandidateMethod extends Ref .findFirst().orElse(null); } - //TODO add result Doc based Parser (which run before produce) - public void identifier(I identifier) { - this.identifier = identifier; - } - @Override public boolean equals(Object obj) { if (obj instanceof BestCandidateMethod bcMethod) { @@ -80,27 +53,37 @@ public class BestCandidateMethod extends Ref return Objects.hash(containingObject.getClass(), methodName); } + private List getCurrentParameterList(C context) { + List parameters = new ArrayList<>(); + int c = 0; + Object last; + while ((last = getParameter(context, c)) != null) { + parameters.add(last); + c++; + } + return parameters; + } + public abstract static class Factory implements ReflectedMethodFactory.Factory { private MethodContainer methodContainer; private String methodName; - private Object[] additionalParameters; - protected Factory(MethodContainer methodContainer, String methodName, Object... additionalParameters) { + protected Factory(MethodContainer methodContainer, String methodName) { this.methodContainer = methodContainer; this.methodName = methodName; - this.additionalParameters = additionalParameters; } @Override public Optional> produce(Method method, Object containingObject) { - BestCandidateMethod bcMethod = new BestCandidateMethod<>( - methodName, containingObject, additionalParameters); - if (methodContainer.methods().contains(bcMethod)) + Optional> bcMethod = bcProduce(methodName, containingObject); + if (bcMethod.isEmpty() || methodContainer.methods().contains(bcMethod.get())) return Optional.empty(); - return Optional.of(bcMethod); + return Optional.of(bcMethod.get()); } + protected abstract Optional> bcProduce(String methodName, Object containingObject); + } } diff --git a/lib/src/main/java/net/tomatentum/cutin/method/ReflectedMethod.java b/lib/src/main/java/net/tomatentum/cutin/method/ReflectedMethod.java index 58532f2..2988beb 100644 --- a/lib/src/main/java/net/tomatentum/cutin/method/ReflectedMethod.java +++ b/lib/src/main/java/net/tomatentum/cutin/method/ReflectedMethod.java @@ -53,7 +53,7 @@ public abstract class ReflectedMethod { List parameters = new ArrayList<>(); for (int i = 0; i < parameterCount; i++) { - Object parameter = getParameter(context, i-1); + Object parameter = getParameter(context, i); logger.trace("Found parameter {}={} for method {}", parameter != null ? parameter.getClass().toString() : " ", parameter, ReflectionUtil.getFullMethodName(method)); parameters.add(parameter); } diff --git a/lib/src/test/java/net/tomatentum/cutin/ReflectedMethodTest.java b/lib/src/test/java/net/tomatentum/cutin/ReflectedMethodTest.java index 3bb28a7..6b0438e 100644 --- a/lib/src/test/java/net/tomatentum/cutin/ReflectedMethodTest.java +++ b/lib/src/test/java/net/tomatentum/cutin/ReflectedMethodTest.java @@ -4,7 +4,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Test; -import net.tomatentum.cutin.method.BestCandidateMethod; import net.tomatentum.cutin.method.ReflectedMethod; class ReflectedMethodTest { @@ -14,16 +13,16 @@ class ReflectedMethodTest { ReflectedMethod method = new TestReflectedMethod(new TestMethodClass()); Object result = method.run("testContext"); assertTrue((boolean)result); + System.out.println("Success"); } @Test void testBCMethod() { - ReflectedMethod method = new BestCandidateMethod( + ReflectedMethod method = new TestBestCandidateMethod( "test", - new TestMethodClass(), - "ident", - "testString"); + new TestMethodClass()); Object result = method.run((double)4); assertTrue((boolean)result); + System.out.println("Success"); } } diff --git a/lib/src/test/java/net/tomatentum/cutin/TestBestCandidateMethod.java b/lib/src/test/java/net/tomatentum/cutin/TestBestCandidateMethod.java new file mode 100644 index 0000000..49074bf --- /dev/null +++ b/lib/src/test/java/net/tomatentum/cutin/TestBestCandidateMethod.java @@ -0,0 +1,28 @@ +package net.tomatentum.cutin; + +import net.tomatentum.cutin.method.BestCandidateMethod; + +public class TestBestCandidateMethod extends BestCandidateMethod { + + protected TestBestCandidateMethod(String methodName, Object containingObject) { + super(methodName, containingObject); + } + + @Override + public Object getParameter(Double context, int index) { + switch (index) { + case 0: + return context; + case 1: + return "testString"; + default: + return null; + } + } + + @Override + public String identifier() { + return "ident"; + } + +} diff --git a/lib/src/test/java/net/tomatentum/cutin/TestReflectedMethod.java b/lib/src/test/java/net/tomatentum/cutin/TestReflectedMethod.java index 08611d6..b1264e6 100644 --- a/lib/src/test/java/net/tomatentum/cutin/TestReflectedMethod.java +++ b/lib/src/test/java/net/tomatentum/cutin/TestReflectedMethod.java @@ -13,7 +13,14 @@ public class TestReflectedMethod extends ReflectedMethod { @Override public Object getParameter(String context, int index) { - return 2; + switch (index) { + case 0: + return context; + case 1: + return 2; + default: + return null; + } } @Override