fix(bcmethod): make bcmethod abstract
This commit is contained in:
parent
deabaaf22f
commit
dd4e048ce5
@ -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<I extends Object, C extends Object> extends ReflectedMethod<I, C> {
|
||||
public abstract class BestCandidateMethod<I extends Object, C extends Object> extends ReflectedMethod<I, C> {
|
||||
|
||||
private String methodName;
|
||||
private I identifier;
|
||||
private List<Object> 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<I extends Object, C extends Object> 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<I extends Object, C extends Object> 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<I extends Object, C extends Object> extends Ref
|
||||
return Objects.hash(containingObject.getClass(), methodName);
|
||||
}
|
||||
|
||||
private List<Object> getCurrentParameterList(C context) {
|
||||
List<Object> 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<I extends Object, C extends Object> implements ReflectedMethodFactory.Factory<I, C> {
|
||||
|
||||
private MethodContainer<I, C> methodContainer;
|
||||
private String methodName;
|
||||
private Object[] additionalParameters;
|
||||
|
||||
protected Factory(MethodContainer<I, C> methodContainer, String methodName, Object... additionalParameters) {
|
||||
protected Factory(MethodContainer<I, C> methodContainer, String methodName) {
|
||||
this.methodContainer = methodContainer;
|
||||
this.methodName = methodName;
|
||||
this.additionalParameters = additionalParameters;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<ReflectedMethod<I, C>> produce(Method method, Object containingObject) {
|
||||
BestCandidateMethod<I, C> bcMethod = new BestCandidateMethod<>(
|
||||
methodName, containingObject, additionalParameters);
|
||||
if (methodContainer.methods().contains(bcMethod))
|
||||
Optional<BestCandidateMethod<I, C>> 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<BestCandidateMethod<I, C>> bcProduce(String methodName, Object containingObject);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -53,7 +53,7 @@ public abstract class ReflectedMethod<I extends Object, C extends Object> {
|
||||
List<Object> 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);
|
||||
}
|
||||
|
@ -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<String, String> method = new TestReflectedMethod(new TestMethodClass());
|
||||
Object result = method.run("testContext");
|
||||
assertTrue((boolean)result);
|
||||
System.out.println("Success");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testBCMethod() {
|
||||
ReflectedMethod<String, Double> method = new BestCandidateMethod<String, Double>(
|
||||
ReflectedMethod<String, Double> method = new TestBestCandidateMethod(
|
||||
"test",
|
||||
new TestMethodClass(),
|
||||
"ident",
|
||||
"testString");
|
||||
new TestMethodClass());
|
||||
Object result = method.run((double)4);
|
||||
assertTrue((boolean)result);
|
||||
System.out.println("Success");
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,28 @@
|
||||
package net.tomatentum.cutin;
|
||||
|
||||
import net.tomatentum.cutin.method.BestCandidateMethod;
|
||||
|
||||
public class TestBestCandidateMethod extends BestCandidateMethod<String, Double> {
|
||||
|
||||
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";
|
||||
}
|
||||
|
||||
}
|
@ -13,7 +13,14 @@ public class TestReflectedMethod extends ReflectedMethod<String, String> {
|
||||
|
||||
@Override
|
||||
public Object getParameter(String context, int index) {
|
||||
return 2;
|
||||
switch (index) {
|
||||
case 0:
|
||||
return context;
|
||||
case 1:
|
||||
return 2;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
x
Reference in New Issue
Block a user