Compare commits
No commits in common. "2cd14d4a5dc9ff32d9847344fd8205de30bce491" and "6d3f077c5da8630e3e508656ead1e705233a6a75" have entirely different histories.
2cd14d4a5d
...
6d3f077c5d
@ -6,7 +6,7 @@ plugins {
|
||||
|
||||
allprojects {
|
||||
group = "net.tomatentum.cutin"
|
||||
version = "0.1.0" + (if (!project.hasProperty("release")) ("-" + getGitHash()) else "")
|
||||
version = "0.1.0-RC1" + (if (!project.hasProperty("release")) ("-" + getGitHash()) else "")
|
||||
description = "A lightweight Reflection abstraction specifically but not exclusively made for tueem/Marinara."
|
||||
plugins.withType<JavaPlugin> {
|
||||
tasks.withType<Jar>().configureEach {
|
||||
|
@ -1,53 +0,0 @@
|
||||
package net.tomatentum.cutin;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import net.tomatentum.cutin.util.ReflectionUtil;
|
||||
|
||||
public class BestCandidateMethod<I> extends ReflectedMethod<I> {
|
||||
|
||||
private String methodName;
|
||||
private I identifier;
|
||||
private List<Object> additionalParameters;
|
||||
|
||||
protected BestCandidateMethod(String methodName, Object containingObject, I identifer, Object... additionalParameters) {
|
||||
super(getMethod(containingObject, methodName), containingObject);
|
||||
this.methodName = methodName;
|
||||
this.identifier = identifer;
|
||||
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
|
||||
public Object run(Object context) {
|
||||
Method[] methods = Arrays.stream(containingObject.getClass().getDeclaredMethods())
|
||||
.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);
|
||||
super.method = ReflectionUtil.getMostSpecificMethod(methods, parameters);
|
||||
return super.run(context);
|
||||
}
|
||||
|
||||
private static Method getMethod(Object containingMethod, String methodName) {
|
||||
return Arrays.stream(containingMethod.getClass().getDeclaredMethods())
|
||||
.filter(m -> m.getName().equals(methodName))
|
||||
.findFirst().orElse(null);
|
||||
}
|
||||
|
||||
}
|
@ -1,9 +1,7 @@
|
||||
package net.tomatentum.cutin;
|
||||
|
||||
import net.tomatentum.cutin.container.MethodContainer;
|
||||
public interface MethodProcessor {
|
||||
|
||||
public interface MethodProcessor<I extends Object> {
|
||||
|
||||
void process(Object context, MethodContainer<I> methodContainer);
|
||||
void process(Object context);
|
||||
|
||||
}
|
||||
|
@ -1,11 +1,7 @@
|
||||
package net.tomatentum.cutin;
|
||||
|
||||
import java.util.Collection;
|
||||
public interface ProcessorContainer {
|
||||
|
||||
public interface ProcessorContainer<I> {
|
||||
|
||||
ProcessorContainer<I> addProcessor(MethodProcessor<I> processor);
|
||||
|
||||
Collection<MethodProcessor<I>> processor();
|
||||
ProcessorContainer addProcessor(MethodProcessor processor);
|
||||
|
||||
}
|
||||
|
@ -1,41 +1,31 @@
|
||||
package net.tomatentum.cutin;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import net.tomatentum.cutin.container.MethodContainer;
|
||||
|
||||
public class ProcessorMethodExecutor<I extends Object> implements MethodExecutor, ProcessorContainer<I> {
|
||||
public class ProcessorMethodExecutor implements MethodExecutor, ProcessorContainer {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
private MethodContainer<I> methodContainer;
|
||||
private Set<MethodProcessor<I>> processors;
|
||||
private Set<MethodProcessor> processors;
|
||||
|
||||
public ProcessorMethodExecutor(MethodContainer<I> methodContainer) {
|
||||
this.methodContainer = methodContainer;
|
||||
public ProcessorMethodExecutor() {
|
||||
this.processors = new HashSet<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ProcessorContainer<I> addProcessor(MethodProcessor<I> processor) {
|
||||
public ProcessorContainer addProcessor(MethodProcessor processor) {
|
||||
processors.add(processor);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<MethodProcessor<I>> processor() {
|
||||
return this.processors;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(Object context) {
|
||||
logger.debug("Handling {} context ", context);
|
||||
processors.forEach(x -> x.process(context, methodContainer));
|
||||
logger.debug("Received {} interaction ", context);
|
||||
processors.forEach(x -> x.process(context));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -12,15 +12,15 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
import net.tomatentum.cutin.util.ReflectionUtil;
|
||||
|
||||
public abstract class ReflectedMethod<I extends Object> {
|
||||
public abstract class ReflectedMethod {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
protected Method method;
|
||||
protected Object containingObject;
|
||||
private Method method;
|
||||
private Object containingObject;
|
||||
|
||||
protected ReflectedMethod(Method method, Object containingObject) {
|
||||
if (!Arrays.asList(containingObject.getClass().getDeclaredMethods()).contains(method))
|
||||
public ReflectedMethod(Method method, Object containingObject) {
|
||||
if (!Arrays.asList(containingObject.getClass().getMethods()).contains(method))
|
||||
throw new InvalidParameterException("Method does not apply to specified handler");
|
||||
this.method = method;
|
||||
this.containingObject = containingObject;
|
||||
@ -28,8 +28,6 @@ public abstract class ReflectedMethod<I extends Object> {
|
||||
|
||||
public abstract Object getParameter(Object context, int index);
|
||||
|
||||
public abstract I identifier();
|
||||
|
||||
public Object run(Object context) {
|
||||
method.setAccessible(true);
|
||||
try {
|
||||
|
@ -27,17 +27,17 @@ public class ReflectedMethodFactoryImpl implements ReflectedMethodFactory {
|
||||
|
||||
@Override
|
||||
public Optional<ReflectedMethod> produce(Method method, Object containingClass) {
|
||||
Optional<ReflectedMethod> rmethod = this.factories.stream()
|
||||
Optional<ReflectedMethod> imethod = this.factories.stream()
|
||||
.map(f -> factoryProduce(f, method, containingClass))
|
||||
.filter(Optional::isPresent)
|
||||
.map(Optional::get)
|
||||
.findFirst();
|
||||
|
||||
if (rmethod.isEmpty()) {
|
||||
if (imethod.isEmpty()) {
|
||||
logger.debug("Could not produce a ReflectedMethod for Method {}", ReflectionUtil.getFullMethodName(method));
|
||||
}
|
||||
|
||||
return rmethod;
|
||||
return imethod;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -1,50 +0,0 @@
|
||||
package net.tomatentum.cutin.container;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import net.tomatentum.cutin.ReflectedMethod;
|
||||
import net.tomatentum.cutin.ReflectedMethodFactory;
|
||||
|
||||
public class LoneMethodContainer<I extends Object> implements MethodContainer<I> {
|
||||
|
||||
private Map<I, ReflectedMethod<I>> methodStore;
|
||||
private ReflectedMethodFactory factory;
|
||||
|
||||
public LoneMethodContainer(ReflectedMethodFactory factory) {
|
||||
this.methodStore = new HashMap<>();
|
||||
this.factory = factory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addMethods(ReflectedMethod<I>... methods) {
|
||||
for (ReflectedMethod<I> reflectedMethod : methods)
|
||||
this.methodStore.put(reflectedMethod.identifier(), reflectedMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addMethods(Object containingObject, Method... methods) {
|
||||
for (Method method : methods)
|
||||
this.addMethods(this.factory.produce(method, containingObject));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<ReflectedMethod<I>> methods() {
|
||||
return this.methodStore.values();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<ReflectedMethod<I>> findFor(I identifier) {
|
||||
return Arrays.asList(this.methodStore.get(identifier));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<ReflectedMethod<I>> findFirstFor(I identifier) {
|
||||
return findFor(identifier).stream().findFirst();
|
||||
}
|
||||
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
package net.tomatentum.cutin.container;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Collection;
|
||||
import java.util.Optional;
|
||||
|
||||
import net.tomatentum.cutin.ReflectedMethod;
|
||||
|
||||
public interface MethodContainer<I extends Object> {
|
||||
|
||||
void addMethods(ReflectedMethod<I>... methods);
|
||||
void addMethods(Object containingObject, Method... methods);
|
||||
default void addAllMethods(Object containingObject) {
|
||||
this.addMethods(containingObject, containingObject.getClass().getDeclaredMethods());
|
||||
};
|
||||
|
||||
Collection<ReflectedMethod<I>> methods();
|
||||
Collection<ReflectedMethod<I>> findFor(I identifier);
|
||||
Optional<ReflectedMethod<I>> findFirstFor(I identifier);
|
||||
|
||||
}
|
@ -1,115 +0,0 @@
|
||||
package net.tomatentum.cutin.container;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import net.tomatentum.cutin.ReflectedMethod;
|
||||
import net.tomatentum.cutin.ReflectedMethodFactory;
|
||||
import net.tomatentum.cutin.util.ReflectionUtil;
|
||||
|
||||
public class MultiMethodContainer<I extends Object> implements MethodContainer<I> {
|
||||
|
||||
private Set<Entry<I>> entries;
|
||||
private ReflectedMethodFactory factory;
|
||||
|
||||
public MultiMethodContainer(ReflectedMethodFactory factory) {
|
||||
this.entries = new HashSet<>();
|
||||
this.factory = factory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addMethods(ReflectedMethod<I>... methods) {
|
||||
for (ReflectedMethod<I> rMethod : methods) {
|
||||
Optional<Entry<I>> oentry = this.entries.stream()
|
||||
.filter(e -> rMethod.identifier().equals(e.identifier()))
|
||||
.findFirst();
|
||||
Entry<I> entry = oentry.orElse(new Entry<>(rMethod.identifier())).addMethod(rMethod);
|
||||
if (oentry.isEmpty()) this.entries.add(entry);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addMethods(Object containingObject, Method... methods) {
|
||||
for (Method method : methods)
|
||||
this.addMethods(this.factory.produce(method, containingObject));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<ReflectedMethod<I>> methods() {
|
||||
return this.entries.stream()
|
||||
.flatMap(e -> e.methods.stream())
|
||||
.toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<ReflectedMethod<I>> findFor(I identifier) {
|
||||
return this.entries.stream()
|
||||
.filter(e -> e.identifier().equals(identifier))
|
||||
.flatMap(e -> e.methods.stream())
|
||||
.toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<ReflectedMethod<I>> findFirstFor(I identifier) {
|
||||
return this.entries.stream()
|
||||
.filter(e -> e.identifier().equals(identifier))
|
||||
.flatMap(e -> e.methods.stream())
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
public static record Entry<I extends Object>(I identifier, Set<ReflectedMethod<I>> methods) {
|
||||
|
||||
public Entry(I identifier) {
|
||||
this(identifier, new HashSet<>());
|
||||
}
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(Entry.class);
|
||||
|
||||
public Entry<I> addMethod(ReflectedMethod<I> method) {
|
||||
I midentifier = method.identifier();
|
||||
|
||||
if (!this.identifier().equals(midentifier))
|
||||
throw new IllegalArgumentException("Method's identifier did not equal the entry's identifier");
|
||||
|
||||
this.methods.add(method);
|
||||
logger.debug("Added method {} to entry {}", method.method().getName(), this);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Object[] runAll(Object 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
|
||||
public boolean equals(Object obj) {
|
||||
if (!(obj instanceof Entry))
|
||||
return false;
|
||||
Entry<?> other = (Entry<?>) obj;
|
||||
return other.identifier().equals(identifier());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return this.identifier().hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Content(%s)".formatted(identifier().toString());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -4,34 +4,16 @@ import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public final class ReflectionUtil {
|
||||
|
||||
private ReflectionUtil() {}
|
||||
|
||||
private static final Map<Class<?>, Class<?>> PRIMITIVE_TO_BOXED = Map.ofEntries(
|
||||
Map.entry(boolean.class, Boolean.class),
|
||||
Map.entry(byte.class, Byte.class),
|
||||
Map.entry(char.class, Character.class),
|
||||
Map.entry(double.class, Double.class),
|
||||
Map.entry(float.class, Float.class),
|
||||
Map.entry(int.class, Integer.class),
|
||||
Map.entry(long.class, Long.class),
|
||||
Map.entry(short.class, Short.class),
|
||||
Map.entry(void.class, Void.class)
|
||||
);
|
||||
|
||||
public static Class<?> box(Class<?> type) {
|
||||
return type.isPrimitive() ? PRIMITIVE_TO_BOXED.get(type) : type;
|
||||
}
|
||||
|
||||
public static <T extends Annotation> boolean isAnnotationPresent(Method method, Class<T> annotationClass) {
|
||||
return method.isAnnotationPresent(annotationClass) || method.getDeclaringClass().isAnnotationPresent(annotationClass);
|
||||
if (method.isAnnotationPresent(annotationClass) || method.getDeclaringClass().isAnnotationPresent(annotationClass))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static <T extends Annotation> T getAnnotation(Method method, Class<T> annotationClass) {
|
||||
@ -40,14 +22,6 @@ public final class ReflectionUtil {
|
||||
method.getDeclaringClass().getAnnotation(annotationClass);
|
||||
}
|
||||
|
||||
public static Stream<Object> getReturnAsStream(Object ret) {
|
||||
if (ret instanceof Object[] array)
|
||||
return Stream.of(array);
|
||||
if (ret instanceof Collection<?> coll)
|
||||
return coll.stream().map(o -> (Object)o);
|
||||
return Stream.of(ret);
|
||||
}
|
||||
|
||||
public static int getCastDepth(Class<?> child, Class<?> parent) {
|
||||
|
||||
if (parent.equals(Object.class))
|
||||
@ -83,20 +57,19 @@ public final class ReflectionUtil {
|
||||
.filter(x -> isMethodCallable(x, parameters))
|
||||
.toList();
|
||||
|
||||
if (compatibleMethods.isEmpty())
|
||||
if (compatibleMethods.size() == 0)
|
||||
throw new IllegalArgumentException("There are no compatible Methods provided");
|
||||
|
||||
|
||||
for (int i = 0; i < parameters.length; i++) {
|
||||
final int currI = i;
|
||||
Class<?>[] parameterTypes = compatibleMethods.stream()
|
||||
.map(x -> getBoxedParameterTypes(x)[currI])
|
||||
.map(x -> x.getParameterTypes()[currI])
|
||||
.toArray(x -> new Class[x]);
|
||||
|
||||
Class<?> mostSpecific = getMostSpecificClass(parameterTypes, parameters[i]);
|
||||
|
||||
compatibleMethods = compatibleMethods.stream()
|
||||
.filter(x -> Objects.equals(getBoxedParameterTypes(x)[currI], mostSpecific))
|
||||
.filter(x -> Objects.equals(x.getParameterTypes()[currI], mostSpecific))
|
||||
.toList();
|
||||
}
|
||||
|
||||
@ -120,7 +93,7 @@ public final class ReflectionUtil {
|
||||
if (!Objects.equals(method.getParameterCount(), parameters.length))
|
||||
return false;
|
||||
|
||||
Class<?>[] methodParams = getBoxedParameterTypes(method);
|
||||
Class<?>[] methodParams = method.getParameterTypes();
|
||||
for (int i = 0; i < parameters.length; i++) {
|
||||
if (!methodParams[i].isAssignableFrom(parameters[i]))
|
||||
return false;
|
||||
@ -128,12 +101,6 @@ public final class ReflectionUtil {
|
||||
return true;
|
||||
}
|
||||
|
||||
public static Class<?>[] getBoxedParameterTypes(Method method) {
|
||||
return Arrays.stream(method.getParameterTypes())
|
||||
.map(ReflectionUtil::box)
|
||||
.toArray(Class<?>[]::new);
|
||||
}
|
||||
|
||||
public static String getFullMethodName(Method method) {
|
||||
return method.getDeclaringClass().getName() + "." + method.getName();
|
||||
}
|
||||
|
@ -1,26 +0,0 @@
|
||||
package net.tomatentum.cutin;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class ReflectedMethodTest {
|
||||
|
||||
@Test
|
||||
void methodTest() {
|
||||
ReflectedMethod<String> method = new TestReflectedMethod(new TestMethodClass());
|
||||
Object result = method.run("testContext");
|
||||
assertTrue((boolean)result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testBCMethod() {
|
||||
ReflectedMethod<?> method = new BestCandidateMethod<String>(
|
||||
"test",
|
||||
new TestMethodClass(),
|
||||
"ident",
|
||||
"testString");
|
||||
Object result = method.run((double)4);
|
||||
assertTrue((boolean)result);
|
||||
}
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
package net.tomatentum.cutin;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class TestMethodClass {
|
||||
|
||||
boolean test(String context, int testNumber) {
|
||||
assertEquals("testContext", context);
|
||||
assertEquals(2, testNumber);
|
||||
return true;
|
||||
}
|
||||
|
||||
boolean test(double context, String testString) {
|
||||
assertEquals(4, context);
|
||||
assertEquals("testString", testString);
|
||||
return true;
|
||||
}
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
package net.tomatentum.cutin;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class TestReflectedMethod extends ReflectedMethod<String> {
|
||||
|
||||
protected TestReflectedMethod(Object containingObject) {
|
||||
super(getMethod(containingObject), containingObject);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getParameter(Object context, int index) {
|
||||
return 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String identifier() {
|
||||
return method().getName();
|
||||
}
|
||||
|
||||
private static Method getMethod(Object containingObject) {
|
||||
try {
|
||||
return containingObject.getClass().getDeclaredMethod("test", String.class, int.class);
|
||||
} catch (NoSuchMethodException | SecurityException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user