Compare commits
16 Commits
7ec05d4e8a
...
dev
Author | SHA1 | Date | |
---|---|---|---|
dba1a366d9 | |||
eaf109e74e | |||
a2bdb549f9
|
|||
ad2c4c00f6
|
|||
4dd7b70f40
|
|||
d76db0ba61
|
|||
8d24604707
|
|||
28d991f17c
|
|||
4c800cfd89
|
|||
e425989106
|
|||
eccad71837
|
|||
cad019e44a
|
|||
79dcc25afc
|
|||
dd4e048ce5
|
|||
deabaaf22f
|
|||
7f5c242173
|
@@ -6,7 +6,7 @@ plugins {
|
|||||||
|
|
||||||
allprojects {
|
allprojects {
|
||||||
group = "net.tomatentum.cutin"
|
group = "net.tomatentum.cutin"
|
||||||
version = "0.1.1" + (if (!project.hasProperty("release")) ("-" + getGitHash()) else "")
|
version = "0.2.0" + (if (!project.hasProperty("release")) ("-" + getGitHash()) else "")
|
||||||
description = "A lightweight Reflection abstraction specifically but not exclusively made for tueem/Marinara."
|
description = "A lightweight Reflection abstraction specifically but not exclusively made for tueem/Marinara."
|
||||||
plugins.withType<JavaPlugin> {
|
plugins.withType<JavaPlugin> {
|
||||||
tasks.withType<Jar>().configureEach {
|
tasks.withType<Jar>().configureEach {
|
||||||
|
@@ -8,3 +8,4 @@ slf4j = "2.0.17"
|
|||||||
[libraries]
|
[libraries]
|
||||||
junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit-jupiter" }
|
junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit-jupiter" }
|
||||||
slf4j = { module = "org.slf4j:slf4j-api", version.ref = "slf4j"}
|
slf4j = { module = "org.slf4j:slf4j-api", version.ref = "slf4j"}
|
||||||
|
slf4j-simple = { module = "org.slf4j:slf4j-simple", version.ref = "slf4j"}
|
||||||
|
@@ -20,6 +20,8 @@ dependencies {
|
|||||||
testImplementation(libs.junit.jupiter)
|
testImplementation(libs.junit.jupiter)
|
||||||
|
|
||||||
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
|
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
|
||||||
|
testImplementation(libs.slf4j.simple)
|
||||||
|
|
||||||
implementation(libs.slf4j)
|
implementation(libs.slf4j)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,5 +1,7 @@
|
|||||||
package net.tomatentum.cutin;
|
package net.tomatentum.cutin;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
public interface MethodParser {
|
public interface MethodParser {
|
||||||
void parse();
|
Object parse(Method method, Object containingObject);
|
||||||
}
|
}
|
||||||
|
@@ -1,20 +0,0 @@
|
|||||||
package net.tomatentum.cutin;
|
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
import net.tomatentum.cutin.method.ReflectedMethod;
|
|
||||||
|
|
||||||
|
|
||||||
public interface ReflectedMethodFactory<I extends Object, C extends Object> {
|
|
||||||
Optional<ReflectedMethod<I, C>> produce(Method method, Object containingClass);
|
|
||||||
ReflectedMethodFactory<I, C> addFactory(Factory<I, C> factory);
|
|
||||||
|
|
||||||
public interface Factory<I extends Object, C extends Object> {
|
|
||||||
|
|
||||||
Optional<ReflectedMethod<I, C>> produce(Method method, Object containingObject);
|
|
||||||
void addParser(ReflectedMethod<I, C> method, List<MethodParser> parser);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
@@ -3,16 +3,22 @@ package net.tomatentum.cutin.container;
|
|||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import net.tomatentum.cutin.ReflectedMethodFactory;
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import net.tomatentum.cutin.factory.ReflectedMethodFactory;
|
||||||
import net.tomatentum.cutin.method.ReflectedMethod;
|
import net.tomatentum.cutin.method.ReflectedMethod;
|
||||||
|
|
||||||
public class LoneMethodContainer<I extends Object, C extends Object> implements MethodContainer<I, C> {
|
public class LoneMethodContainer<I extends Object, C extends Object> implements MethodContainer<I, C> {
|
||||||
|
|
||||||
|
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||||
|
|
||||||
private Map<I, ReflectedMethod<I, C>> methodStore;
|
private Map<I, ReflectedMethod<I, C>> methodStore;
|
||||||
private ReflectedMethodFactory<I, C> factory;
|
private ReflectedMethodFactory<I, C> factory;
|
||||||
|
|
||||||
@@ -23,7 +29,12 @@ public class LoneMethodContainer<I extends Object, C extends Object> implements
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public MethodContainer<I, C> addMethod(ReflectedMethod<I, C> method) {
|
public MethodContainer<I, C> addMethod(ReflectedMethod<I, C> method) {
|
||||||
|
if (this.methodStore.keySet().contains(method.identifier())) {
|
||||||
|
logger.warn("Could not add {} to container because the same identifier was already present.", method);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
this.methodStore.put(method.identifier(), method);
|
this.methodStore.put(method.identifier(), method);
|
||||||
|
logger.debug("Added {} to container", method);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,7 +58,8 @@ public class LoneMethodContainer<I extends Object, C extends Object> implements
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<ReflectedMethod<I, C>> findFor(I identifier) {
|
public Collection<ReflectedMethod<I, C>> findFor(I identifier) {
|
||||||
return Arrays.asList(this.methodStore.get(identifier));
|
ReflectedMethod<I, C> result = this.methodStore.get(identifier);
|
||||||
|
return result != null ? Arrays.asList(result) : Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@@ -10,7 +10,7 @@ import java.util.stream.Collectors;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import net.tomatentum.cutin.ReflectedMethodFactory;
|
import net.tomatentum.cutin.factory.ReflectedMethodFactory;
|
||||||
import net.tomatentum.cutin.method.ReflectedMethod;
|
import net.tomatentum.cutin.method.ReflectedMethod;
|
||||||
import net.tomatentum.cutin.util.ReflectionUtil;
|
import net.tomatentum.cutin.util.ReflectionUtil;
|
||||||
|
|
||||||
@@ -95,21 +95,10 @@ public class MultiMethodContainer<I extends Object, C extends Object> implements
|
|||||||
throw new IllegalArgumentException("Method's identifier did not equal the entry's identifier");
|
throw new IllegalArgumentException("Method's identifier did not equal the entry's identifier");
|
||||||
|
|
||||||
this.methods.add(method);
|
this.methods.add(method);
|
||||||
logger.debug("Added method {} to entry {}", method.method().getName(), this);
|
logger.debug("Added method {} to entry {}", ReflectionUtil.getFullMethodName(method.method()), this);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object[] runAll(C context) {
|
|
||||||
logger.trace("Running all Methods from {} with context {}", this, context);
|
|
||||||
return this.methods.stream()
|
|
||||||
.map(x -> {
|
|
||||||
logger.debug("Running Method {} from {} with context {}", x, this, context);
|
|
||||||
return x.run(context);
|
|
||||||
})
|
|
||||||
.flatMap(ReflectionUtil::getReturnAsStream)
|
|
||||||
.toArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if (!(obj instanceof Entry))
|
if (!(obj instanceof Entry))
|
||||||
@@ -125,7 +114,7 @@ public class MultiMethodContainer<I extends Object, C extends Object> implements
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Content(%s)".formatted(identifier().toString());
|
return "Ident(%s)".formatted(identifier().toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -0,0 +1,20 @@
|
|||||||
|
package net.tomatentum.cutin.factory;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import net.tomatentum.cutin.MethodParser;
|
||||||
|
|
||||||
|
public abstract class ImmutableMethodFactory<I extends Object, C extends Object> implements ReflectedMethodFactory.Factory<I, C> {
|
||||||
|
|
||||||
|
protected Set<MethodParser> parser;
|
||||||
|
|
||||||
|
protected ImmutableMethodFactory(MethodParser... parser) {
|
||||||
|
this.parser = Set.of(parser);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<MethodParser> parser() {
|
||||||
|
return this.parser;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -0,0 +1,17 @@
|
|||||||
|
package net.tomatentum.cutin.factory;
|
||||||
|
|
||||||
|
import net.tomatentum.cutin.MethodParser;
|
||||||
|
|
||||||
|
public abstract class MutableMethodFactory<I extends Object, C extends Object> extends ImmutableMethodFactory<I, C> {
|
||||||
|
|
||||||
|
public MutableMethodFactory<I, C> addParser(MethodParser parser) {
|
||||||
|
super.parser.add(parser);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public MutableMethodFactory<I, C> removeParser(MethodParser parser) {
|
||||||
|
super.parser.remove(parser);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -0,0 +1,42 @@
|
|||||||
|
package net.tomatentum.cutin.factory;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import net.tomatentum.cutin.MethodParser;
|
||||||
|
import net.tomatentum.cutin.method.ReflectedMethod;
|
||||||
|
|
||||||
|
|
||||||
|
public interface ReflectedMethodFactory<I extends Object, C extends Object> {
|
||||||
|
Optional<ReflectedMethod<I, C>> produce(Method method, Object containingClass);
|
||||||
|
ReflectedMethodFactory<I, C> addFactory(Factory<I, C> factory);
|
||||||
|
Set<ReflectedMethodFactory.Factory<I, C>> factories();
|
||||||
|
|
||||||
|
public interface Factory<I extends Object, C extends Object> {
|
||||||
|
|
||||||
|
Optional<ReflectedMethod<I, C>> produce(Method method, Object containingObject, ParserResults parserResults);
|
||||||
|
Set<MethodParser> parser();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class ParserResults extends HashMap<Class<? extends MethodParser>, Object> {
|
||||||
|
|
||||||
|
public static ParserResults create(Set<MethodParser> parser, Method method, Object containingObject) {
|
||||||
|
ParserResults results = new ParserResults();
|
||||||
|
for (MethodParser p : parser) {
|
||||||
|
results.put(p.getClass(), p.parse(method, containingObject));
|
||||||
|
}
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
|
private ParserResults() {}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public <T extends Object> T get(Class<? extends MethodParser> key) {
|
||||||
|
return (T) super.get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@@ -1,28 +1,28 @@
|
|||||||
package net.tomatentum.cutin;
|
package net.tomatentum.cutin.factory;
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.ArrayList;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import net.tomatentum.cutin.MethodParser;
|
||||||
import net.tomatentum.cutin.method.ReflectedMethod;
|
import net.tomatentum.cutin.method.ReflectedMethod;
|
||||||
import net.tomatentum.cutin.util.ReflectionUtil;
|
import net.tomatentum.cutin.util.ReflectionUtil;
|
||||||
|
|
||||||
|
|
||||||
public class ReflectedMethodFactoryImpl<I extends Object, C extends Object> implements ReflectedMethodFactory<I, C> {
|
public class ReflectedMethodFactoryImpl<I extends Object, C extends Object> implements ReflectedMethodFactory<I, C> {
|
||||||
|
|
||||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||||
|
|
||||||
private List<Factory<I, C>> factories;
|
private Set<Factory<I, C>> factories;
|
||||||
|
|
||||||
public ReflectedMethodFactoryImpl() {
|
public ReflectedMethodFactoryImpl() {
|
||||||
this(new ArrayList<>());
|
this(new HashSet<>());
|
||||||
}
|
}
|
||||||
|
|
||||||
public ReflectedMethodFactoryImpl(List<Factory<I, C>> factories) {
|
public ReflectedMethodFactoryImpl(Set<Factory<I, C>> factories) {
|
||||||
this.factories = factories;
|
this.factories = factories;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,9 +34,10 @@ public class ReflectedMethodFactoryImpl<I extends Object, C extends Object> impl
|
|||||||
.map(Optional::get)
|
.map(Optional::get)
|
||||||
.findFirst();
|
.findFirst();
|
||||||
|
|
||||||
if (rmethod.isEmpty()) {
|
if (rmethod.isEmpty())
|
||||||
logger.debug("Could not produce a ReflectedMethod for Method {}", ReflectionUtil.getFullMethodName(method));
|
logger.warn("Could not produce a ReflectedMethod for Method {} in {}", ReflectionUtil.getFullMethodName(method), this);
|
||||||
}
|
else
|
||||||
|
logger.debug("Produced {} for Method {} in {}", rmethod.get(), ReflectionUtil.getFullMethodName(method), this);
|
||||||
|
|
||||||
return rmethod;
|
return rmethod;
|
||||||
}
|
}
|
||||||
@@ -44,17 +45,19 @@ public class ReflectedMethodFactoryImpl<I extends Object, C extends Object> impl
|
|||||||
@Override
|
@Override
|
||||||
public ReflectedMethodFactory<I, C> addFactory(Factory<I, C> factory) {
|
public ReflectedMethodFactory<I, C> addFactory(Factory<I, C> factory) {
|
||||||
this.factories.add(factory);
|
this.factories.add(factory);
|
||||||
|
logger.trace("Added Factory {} to {}", factory, this);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<Factory<I, C>> factories() {
|
||||||
|
return this.factories;
|
||||||
|
}
|
||||||
|
|
||||||
private Optional<ReflectedMethod<I, C>> factoryProduce(Factory<I, C> factory, Method method, Object containingClass) {
|
private Optional<ReflectedMethod<I, C>> factoryProduce(Factory<I, C> factory, Method method, Object containingClass) {
|
||||||
List<MethodParser> parser = new ArrayList<>();
|
Set<MethodParser> parser = factory.parser();
|
||||||
Optional<ReflectedMethod<I, C>> m = factory.produce(method, containingClass);
|
ParserResults results = ParserResults.create(parser, method, containingClass);
|
||||||
m.ifPresent(x -> {
|
return factory.produce(method, containingClass, results);
|
||||||
factory.addParser(x, parser);
|
|
||||||
parser.forEach(MethodParser::parse);
|
|
||||||
});
|
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@@ -1,44 +1,25 @@
|
|||||||
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.stream.Stream;
|
|
||||||
|
|
||||||
import net.tomatentum.cutin.ReflectedMethodFactory;
|
import org.slf4j.Logger;
|
||||||
import net.tomatentum.cutin.container.MethodContainer;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
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 Logger logger = LoggerFactory.getLogger(getClass());
|
||||||
|
|
||||||
private String methodName;
|
private String methodName;
|
||||||
private I identifier;
|
|
||||||
private List<Object> additionalParameters;
|
|
||||||
|
|
||||||
public BestCandidateMethod(String methodName, Object containingObject, I identifer, 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 = 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
|
||||||
@@ -47,11 +28,11 @@ 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);
|
||||||
|
logger.trace("Found {} for {}({}) in {}", ReflectionUtil.getFullMethodName(method()), this.methodName, String.join(", ", Arrays.stream(parameters).map(Object::toString).toList()), this);
|
||||||
return super.run(context);
|
return super.run(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,11 +42,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 +56,15 @@ public class BestCandidateMethod<I extends Object, C extends Object> extends Ref
|
|||||||
return Objects.hash(containingObject.getClass(), methodName);
|
return Objects.hash(containingObject.getClass(), methodName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract static class Factory<I extends Object, C extends Object> implements ReflectedMethodFactory.Factory<I, C> {
|
private List<Object> getCurrentParameterList(C context) {
|
||||||
|
List<Object> parameters = new ArrayList<>();
|
||||||
private MethodContainer<I, C> methodContainer;
|
int c = 0;
|
||||||
private String methodName;
|
Object last;
|
||||||
private Object[] additionalParameters;
|
while ((last = getParameter(context, c)) != null) {
|
||||||
|
parameters.add(last);
|
||||||
protected Factory(MethodContainer<I, C> methodContainer, String methodName, Object... additionalParameters) {
|
c++;
|
||||||
this.methodContainer = methodContainer;
|
|
||||||
this.methodName = methodName;
|
|
||||||
this.additionalParameters = additionalParameters;
|
|
||||||
}
|
}
|
||||||
|
return parameters;
|
||||||
@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))
|
|
||||||
return Optional.empty();
|
|
||||||
return Optional.of(bcMethod);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -33,9 +33,10 @@ public abstract class ReflectedMethod<I extends Object, C extends Object> {
|
|||||||
public Object run(C context) {
|
public Object run(C context) {
|
||||||
method.setAccessible(true);
|
method.setAccessible(true);
|
||||||
try {
|
try {
|
||||||
|
logger.debug("Invoking method {} from {}", ReflectionUtil.getFullMethodName(method), this);
|
||||||
return method.invoke(containingObject, getParameters(context));
|
return method.invoke(containingObject, getParameters(context));
|
||||||
}catch (IllegalAccessException | InvocationTargetException ex) {
|
}catch (IllegalAccessException | InvocationTargetException ex) {
|
||||||
logger.error("ReflectedMethod failed to run", ex);
|
logger.error("ReflectedMethod %s failed to run".formatted(this), ex);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -48,18 +49,18 @@ public abstract class ReflectedMethod<I extends Object, C extends Object> {
|
|||||||
return this.containingObject;
|
return this.containingObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "ReflectedMethod(%s)".formatted(identifier());
|
||||||
|
}
|
||||||
|
|
||||||
private Object[] getParameters(C context) {
|
private Object[] getParameters(C context) {
|
||||||
int parameterCount = method.getParameterCount();
|
int parameterCount = method.getParameterCount();
|
||||||
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;
|
Object parameter = getParameter(context, i);
|
||||||
if (i == 0) {
|
logger.trace("Found parameter {}={} for method {} in {}", parameter != null ? parameter.getClass().toString() : " ", parameter, ReflectionUtil.getFullMethodName(method), this);
|
||||||
parameter = context;
|
|
||||||
}else
|
|
||||||
parameter = getParameter(context, i-1);
|
|
||||||
|
|
||||||
logger.trace("Found parameter {}={} for method {}", parameter != null ? parameter.getClass().toString() : " ", parameter, ReflectionUtil.getFullMethodName(method));
|
|
||||||
parameters.add(parameter);
|
parameters.add(parameter);
|
||||||
}
|
}
|
||||||
return parameters.toArray();
|
return parameters.toArray();
|
||||||
|
@@ -135,6 +135,7 @@ public final class ReflectionUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static String getFullMethodName(Method method) {
|
public static String getFullMethodName(Method method) {
|
||||||
return method.getDeclaringClass().getName() + "." + method.getName();
|
List<String> parameters = Arrays.stream(method.getParameterTypes()).map(Object::toString).toList();
|
||||||
|
return String.format("%s.%s(%s)", method.getDeclaringClass().getName(), method.getName(), String.join(", ", parameters));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -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");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -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
|
@Override
|
||||||
public Object getParameter(String context, int index) {
|
public Object getParameter(String context, int index) {
|
||||||
return 2;
|
switch (index) {
|
||||||
|
case 0:
|
||||||
|
return context;
|
||||||
|
case 1:
|
||||||
|
return 2;
|
||||||
|
default:
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
7
lib/src/test/resources/simplelogger.properties
Normal file
7
lib/src/test/resources/simplelogger.properties
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# SLF4J's SimpleLogger configuration file
|
||||||
|
# Simple implementation of Logger that sends all enabled log messages, for all defined loggers, to System.err.
|
||||||
|
|
||||||
|
# Default logging detail level for all instances of SimpleLogger.
|
||||||
|
# Must be one of ("trace", "debug", "info", "warn", or "error").
|
||||||
|
# If not specified, defaults to "info".
|
||||||
|
org.slf4j.simpleLogger.defaultLogLevel=trace
|
Reference in New Issue
Block a user