fix(bcmethod): make bcmethod abstract
All checks were successful
Build / Gradle-Build (push) Successful in 1m38s
Publish / Gradle-Publish (push) Successful in 11s
Test / Gradle-Test (push) Successful in 13s

This commit is contained in:
tueem 2025-04-13 00:20:28 +02:00
parent deabaaf22f
commit dd4e048ce5
Signed by: tueem
GPG Key ID: 65C8667EC17A88FB
5 changed files with 64 additions and 47 deletions

View File

@ -1,44 +1,23 @@
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 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 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); super(getMethod(containingObject, methodName), containingObject);
this.methodName = methodName; 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 @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.getName().equals(methodName))
.filter(x -> !x.isBridge()) .filter(x -> !x.isBridge())
.toArray(Method[]::new); .toArray(Method[]::new);
Class<?>[] parameters = Stream.concat( Class<?>[] parameters = getCurrentParameterList(context).stream()
Stream.of(context.getClass()), .map(Object::getClass)
additionalParameters.stream().map(Object::getClass) .toArray(Class<?>[]::new);
).toArray(Class<?>[]::new);
super.method = ReflectionUtil.getMostSpecificMethod(methods, parameters); super.method = ReflectionUtil.getMostSpecificMethod(methods, parameters);
return super.run(context); return super.run(context);
} }
@ -61,11 +39,6 @@ public class BestCandidateMethod<I extends Object, C extends Object> extends Ref
.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) {
@ -80,27 +53,37 @@ public class BestCandidateMethod<I extends Object, C extends Object> extends Ref
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, Object... additionalParameters) { protected Factory(MethodContainer<I, C> methodContainer, String methodName) {
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) {
BestCandidateMethod<I, C> bcMethod = new BestCandidateMethod<>( Optional<BestCandidateMethod<I, C>> bcMethod = bcProduce(methodName, containingObject);
methodName, containingObject, additionalParameters); if (bcMethod.isEmpty() || methodContainer.methods().contains(bcMethod.get()))
if (methodContainer.methods().contains(bcMethod))
return Optional.empty(); return Optional.empty();
return Optional.of(bcMethod); return Optional.of(bcMethod.get());
} }
protected abstract Optional<BestCandidateMethod<I, C>> bcProduce(String methodName, Object containingObject);
} }
} }

View File

@ -53,7 +53,7 @@ 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-1); Object parameter = getParameter(context, i);
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,7 +4,6 @@ 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 {
@ -14,16 +13,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 BestCandidateMethod<String, Double>( ReflectedMethod<String, Double> method = new TestBestCandidateMethod(
"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

@ -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";
}
}

View File

@ -13,7 +13,14 @@ 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) {
case 0:
return context;
case 1:
return 2; return 2;
default:
return null;
}
} }
@Override @Override