Compare commits
2 Commits
1891037ed7
...
c44b874b41
Author | SHA1 | Date | |
---|---|---|---|
c44b874b41 | |||
8842e9d8e1 |
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -4,6 +4,8 @@ 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> {
|
||||
Optional<ReflectedMethod<I>> produce(Method method, Object containingClass);
|
||||
|
@ -8,6 +8,7 @@ import java.util.Optional;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import net.tomatentum.cutin.method.ReflectedMethod;
|
||||
import net.tomatentum.cutin.util.ReflectionUtil;
|
||||
|
||||
|
||||
|
@ -8,8 +8,8 @@ import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import net.tomatentum.cutin.ReflectedMethod;
|
||||
import net.tomatentum.cutin.ReflectedMethodFactory;
|
||||
import net.tomatentum.cutin.method.ReflectedMethod;
|
||||
|
||||
public class LoneMethodContainer<I extends Object> implements MethodContainer<I> {
|
||||
|
||||
|
@ -5,7 +5,7 @@ import java.util.Collection;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import net.tomatentum.cutin.ReflectedMethod;
|
||||
import net.tomatentum.cutin.method.ReflectedMethod;
|
||||
|
||||
public interface MethodContainer<I extends Object> {
|
||||
|
||||
|
@ -10,8 +10,8 @@ import java.util.stream.Collectors;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import net.tomatentum.cutin.ReflectedMethod;
|
||||
import net.tomatentum.cutin.ReflectedMethodFactory;
|
||||
import net.tomatentum.cutin.method.ReflectedMethod;
|
||||
import net.tomatentum.cutin.util.ReflectionUtil;
|
||||
|
||||
public class MultiMethodContainer<I extends Object> implements MethodContainer<I> {
|
||||
|
@ -0,0 +1,101 @@
|
||||
package net.tomatentum.cutin.method;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
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 ReflectedMethod<I> {
|
||||
|
||||
private String methodName;
|
||||
private I identifier;
|
||||
private List<Object> additionalParameters;
|
||||
|
||||
public 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);
|
||||
}
|
||||
|
||||
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
|
||||
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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj instanceof BestCandidateMethod<?> bcMethod) {
|
||||
return this.containingObject().getClass().equals(bcMethod.containingObject().getClass()) &&
|
||||
this.methodName.equals(bcMethod.methodName);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(containingObject.getClass(), methodName);
|
||||
}
|
||||
|
||||
public abstract static class Factory<I extends Object> implements ReflectedMethodFactory.Factory<I> {
|
||||
|
||||
private MethodContainer<I> methodContainer;
|
||||
private String methodName;
|
||||
private Object[] additionalParameters;
|
||||
|
||||
protected Factory(MethodContainer<I> methodContainer, String methodName, Object... additionalParameters) {
|
||||
this.methodContainer = methodContainer;
|
||||
this.methodName = methodName;
|
||||
this.additionalParameters = additionalParameters;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<ReflectedMethod<I>> produce(Method method, Object containingObject) {
|
||||
BestCandidateMethod<I> bcMethod = new BestCandidateMethod<>(
|
||||
methodName, containingObject, additionalParameters);
|
||||
if (methodContainer.methods().contains(bcMethod))
|
||||
return Optional.empty();
|
||||
return Optional.of(bcMethod);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package net.tomatentum.cutin;
|
||||
package net.tomatentum.cutin.method;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
@ -4,6 +4,9 @@ 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 {
|
||||
|
||||
@Test
|
||||
|
@ -2,6 +2,8 @@ package net.tomatentum.cutin;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import net.tomatentum.cutin.method.ReflectedMethod;
|
||||
|
||||
public class TestReflectedMethod extends ReflectedMethod<String> {
|
||||
|
||||
protected TestReflectedMethod(Object containingObject) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user