Compare commits

..

No commits in common. "dd4e048ce53c3f9dd5e624ace3e4e450c5a03c7e" and "7ec05d4e8a4a460277b9ef464f2f4cfc7c27d2a5" have entirely different histories.

5 changed files with 52 additions and 64 deletions

View File

@ -1,23 +1,44 @@
package net.tomatentum.cutin.method; package net.tomatentum.cutin.method;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Stream;
import net.tomatentum.cutin.ReflectedMethodFactory; import net.tomatentum.cutin.ReflectedMethodFactory;
import net.tomatentum.cutin.container.MethodContainer; import net.tomatentum.cutin.container.MethodContainer;
import net.tomatentum.cutin.util.ReflectionUtil; import net.tomatentum.cutin.util.ReflectionUtil;
public abstract class BestCandidateMethod<I extends Object, C extends Object> extends ReflectedMethod<I, C> { public class BestCandidateMethod<I extends Object, C extends Object> extends ReflectedMethod<I, C> {
private String methodName; private String methodName;
private I identifier;
private List<Object> additionalParameters;
protected BestCandidateMethod(String methodName, Object containingObject) { public BestCandidateMethod(String methodName, Object containingObject, I identifer, Object... additionalParameters) {
super(getMethod(containingObject, methodName), containingObject); super(getMethod(containingObject, methodName), containingObject);
this.methodName = methodName; this.methodName = methodName;
this.identifier = identifer;
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 @Override
@ -26,9 +47,10 @@ public abstract class BestCandidateMethod<I extends Object, C extends Object> ex
.filter(x -> x.getName().equals(methodName)) .filter(x -> x.getName().equals(methodName))
.filter(x -> !x.isBridge()) .filter(x -> !x.isBridge())
.toArray(Method[]::new); .toArray(Method[]::new);
Class<?>[] parameters = getCurrentParameterList(context).stream() Class<?>[] parameters = Stream.concat(
.map(Object::getClass) Stream.of(context.getClass()),
.toArray(Class<?>[]::new); additionalParameters.stream().map(Object::getClass)
).toArray(Class<?>[]::new);
super.method = ReflectionUtil.getMostSpecificMethod(methods, parameters); super.method = ReflectionUtil.getMostSpecificMethod(methods, parameters);
return super.run(context); return super.run(context);
} }
@ -39,6 +61,11 @@ public abstract class BestCandidateMethod<I extends Object, C extends Object> ex
.findFirst().orElse(null); .findFirst().orElse(null);
} }
//TODO add result Doc based Parser (which run before produce)
public void identifier(I identifier) {
this.identifier = identifier;
}
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (obj instanceof BestCandidateMethod<?, ?> bcMethod) { if (obj instanceof BestCandidateMethod<?, ?> bcMethod) {
@ -53,37 +80,27 @@ public abstract class BestCandidateMethod<I extends Object, C extends Object> ex
return Objects.hash(containingObject.getClass(), methodName); 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> { public abstract static class Factory<I extends Object, C extends Object> implements ReflectedMethodFactory.Factory<I, C> {
private MethodContainer<I, C> methodContainer; private MethodContainer<I, C> methodContainer;
private String methodName; private String methodName;
private Object[] additionalParameters;
protected Factory(MethodContainer<I, C> methodContainer, String methodName) { protected Factory(MethodContainer<I, C> methodContainer, String methodName, Object... additionalParameters) {
this.methodContainer = methodContainer; this.methodContainer = methodContainer;
this.methodName = methodName; this.methodName = methodName;
this.additionalParameters = additionalParameters;
} }
@Override @Override
public Optional<ReflectedMethod<I, C>> produce(Method method, Object containingObject) { public Optional<ReflectedMethod<I, C>> produce(Method method, Object containingObject) {
Optional<BestCandidateMethod<I, C>> bcMethod = bcProduce(methodName, containingObject); BestCandidateMethod<I, C> bcMethod = new BestCandidateMethod<>(
if (bcMethod.isEmpty() || methodContainer.methods().contains(bcMethod.get())) methodName, containingObject, additionalParameters);
if (methodContainer.methods().contains(bcMethod))
return Optional.empty(); return Optional.empty();
return Optional.of(bcMethod.get()); return Optional.of(bcMethod);
} }
protected abstract Optional<BestCandidateMethod<I, C>> bcProduce(String methodName, Object containingObject);
} }
} }

View File

@ -53,7 +53,12 @@ public abstract class ReflectedMethod<I extends Object, C extends Object> {
List<Object> parameters = new ArrayList<>(); List<Object> parameters = new ArrayList<>();
for (int i = 0; i < parameterCount; i++) { for (int i = 0; i < parameterCount; i++) {
Object parameter = getParameter(context, i); Object parameter;
if (i == 0) {
parameter = context;
}else
parameter = getParameter(context, i-1);
logger.trace("Found parameter {}={} for method {}", parameter != null ? parameter.getClass().toString() : " ", parameter, ReflectionUtil.getFullMethodName(method)); logger.trace("Found parameter {}={} for method {}", parameter != null ? parameter.getClass().toString() : " ", parameter, ReflectionUtil.getFullMethodName(method));
parameters.add(parameter); parameters.add(parameter);
} }

View File

@ -4,6 +4,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import net.tomatentum.cutin.method.BestCandidateMethod;
import net.tomatentum.cutin.method.ReflectedMethod; import net.tomatentum.cutin.method.ReflectedMethod;
class ReflectedMethodTest { class ReflectedMethodTest {
@ -13,16 +14,16 @@ class ReflectedMethodTest {
ReflectedMethod<String, String> method = new TestReflectedMethod(new TestMethodClass()); ReflectedMethod<String, String> method = new TestReflectedMethod(new TestMethodClass());
Object result = method.run("testContext"); Object result = method.run("testContext");
assertTrue((boolean)result); assertTrue((boolean)result);
System.out.println("Success");
} }
@Test @Test
void testBCMethod() { void testBCMethod() {
ReflectedMethod<String, Double> method = new TestBestCandidateMethod( ReflectedMethod<String, Double> method = new BestCandidateMethod<String, Double>(
"test", "test",
new TestMethodClass()); new TestMethodClass(),
"ident",
"testString");
Object result = method.run((double)4); Object result = method.run((double)4);
assertTrue((boolean)result); assertTrue((boolean)result);
System.out.println("Success");
} }
} }

View File

@ -1,28 +0,0 @@
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";
}
}

View File

@ -13,14 +13,7 @@ public class TestReflectedMethod extends ReflectedMethod<String, String> {
@Override @Override
public Object getParameter(String context, int index) { public Object getParameter(String context, int index) {
switch (index) { return 2;
case 0:
return context;
case 1:
return 2;
default:
return null;
}
} }
@Override @Override