Skip to content

Commit

Permalink
refactor: annotation -> qualifier
Browse files Browse the repository at this point in the history
  • Loading branch information
mimbrero committed Oct 6, 2023
1 parent 77838cc commit 1c91b10
Show file tree
Hide file tree
Showing 26 changed files with 288 additions and 287 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import st.networkers.rimor.params.InstructionParam;
import st.networkers.rimor.params.InstructionParams;
import st.networkers.rimor.params.parse.support.StringInstructionParamParser;
import st.networkers.rimor.reflect.CachedMethod;
import st.networkers.rimor.reflect.CachedParameter;
import st.networkers.rimor.qualify.reflect.QualifiedMethod;
import st.networkers.rimor.qualify.reflect.QualifiedParameter;

import java.lang.reflect.Type;
import java.util.ArrayList;
Expand Down Expand Up @@ -45,25 +45,25 @@ protected Object getParameter(Token<T> token, ExecutionContext context) {
}

protected static int getIndex(Token<?> token, ExecutionContext context) {
InstructionParam param = token.getAnnotation(InstructionParam.class);
InstructionParam param = token.getQualifier(InstructionParam.class);

// if index is given, just return it
if (param.index() > -1)
return param.index();

if (token instanceof ParameterToken) {
ParameterToken<?> parameterToken = (ParameterToken<?>) token;
return getPositionFromParameter(parameterToken.getMethod(), parameterToken.getParameter());
return getPositionFromParameter(parameterToken.getQualifiedMethod(), parameterToken.getQualifiedParameter());
}

return -1;
}

protected static int getPositionFromParameter(CachedMethod method, CachedParameter parameter) {
protected static int getPositionFromParameter(QualifiedMethod method, QualifiedParameter parameter) {
// TODO cache
List<CachedParameter> parameters = new ArrayList<>(method.getParameters());
parameters.removeIf(p -> !p.isAnnotationPresent(InstructionParam.class)
|| p.getAnnotation(InstructionParam.class).index() > -1);
List<QualifiedParameter> parameters = new ArrayList<>(method.getQualifiedParameters());
parameters.removeIf(p -> !p.isQualifierPresent(InstructionParam.class)
|| p.getQualifier(InstructionParam.class).index() > -1);

return parameters.indexOf(parameter);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private boolean parse(String parameter, Token<Boolean> token) {
if (Boolean.parseBoolean(parameter) || this.trueAliases.contains(parameter.toLowerCase()))
return true;

TrueValues trueValues = token.getAnnotation(TrueValues.class);
TrueValues trueValues = token.getQualifier(TrueValues.class);
return trueValues != null && ArrayUtils.contains(trueValues.value(), parameter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import st.networkers.rimor.params.InstructionParamImpl;
import st.networkers.rimor.params.InstructionParams;
import st.networkers.rimor.params.parse.support.DefaultInstructionParamParser;
import st.networkers.rimor.reflect.CachedMethod;
import st.networkers.rimor.reflect.CachedParameter;
import st.networkers.rimor.qualify.reflect.QualifiedMethod;
import st.networkers.rimor.qualify.reflect.QualifiedParameter;

import java.util.Arrays;
import java.util.List;
Expand All @@ -32,22 +32,22 @@ void foo(Object thisShouldNotBeCounted,
@InstructionParam Object param3) {
}

static CachedMethod fooMethod;
static QualifiedMethod fooMethod;
static ExecutionContext context;

static List<Object> params = Arrays.asList("bar", true, -1, null, "baz");

@BeforeAll
static void beforeAll() throws NoSuchMethodException {
fooMethod = CachedMethod.build(AbstractInstructionParamParserTest.class.getDeclaredMethod("foo", Object.class, String.class, boolean.class, int.class, String.class, Object.class));
fooMethod = QualifiedMethod.build(AbstractInstructionParamParserTest.class.getDeclaredMethod("foo", Object.class, String.class, boolean.class, int.class, String.class, Object.class));
context = ExecutionContext.builder()
.bind(new Token<List<Object>>() {}.annotatedWith(InstructionParams.class), params)
.build();
}

@Test
void whenGettingInstructionParamByProvidedIndex_returnsInstructionParamByProvidedIndex() {
Token<Object> token = (Token<Object>) ParameterToken.build(fooMethod, fooMethod.getParameters().get(4));
Token<Object> token = (Token<Object>) ParameterToken.build(fooMethod, fooMethod.getQualifiedParameters().get(4));
assertThat(new DefaultInstructionParamParser().get(token, context)).isEqualTo(params.get(4));
}

Expand All @@ -61,17 +61,17 @@ void whenGettingInstructionParamWithIndexOutOfBounds_returnsNull() {

@ParameterizedTest
@MethodSource
void whenGettingInstructionParamByMethodParameterOrder_returnsInstructionParamByParameterOrder(CachedParameter parameter, Object expectedParameter) {
void whenGettingInstructionParamByMethodParameterOrder_returnsInstructionParamByParameterOrder(QualifiedParameter parameter, Object expectedParameter) {
Token<Object> token = (Token<Object>) ParameterToken.build(fooMethod, parameter);
assertThat(new DefaultInstructionParamParser().get(token, context)).isEqualTo(expectedParameter);
}

public static Stream<Arguments> whenGettingInstructionParamByMethodParameterOrder_returnsInstructionParamByParameterOrder() {
return Stream.of(
Arguments.of(fooMethod.getParameters().get(1), params.get(0)),
Arguments.of(fooMethod.getParameters().get(2), params.get(1)),
Arguments.of(fooMethod.getParameters().get(3), params.get(2)),
Arguments.of(fooMethod.getParameters().get(5), params.get(3))
Arguments.of(fooMethod.getQualifiedParameters().get(1), params.get(0)),
Arguments.of(fooMethod.getQualifiedParameters().get(2), params.get(1)),
Arguments.of(fooMethod.getQualifiedParameters().get(3), params.get(2)),
Arguments.of(fooMethod.getQualifiedParameters().get(5), params.get(3))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
public class ExecutionContext {

public static Builder builder() {
return new Builder();
return new Builder(new MatchingMap<>());
}

public static Builder builder(ExecutionContext context) {
return new Builder(new MatchingMap<>(context.components));
}

// not a simple map because there may be components bound to tokens with required qualifier types,
Expand Down Expand Up @@ -43,8 +47,8 @@ public int hashCode() {
public static class Builder {
private final MatchingMap<Token<?>, Object> components;

private Builder() {
this.components = new MatchingMap<>();
private Builder(MatchingMap<Token<?>, Object> components) {
this.components = components;
}

public <T> Builder bind(Class<? super T> type, T object) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package st.networkers.rimor.context;

import st.networkers.rimor.context.provide.ExecutionContextProvider;
import st.networkers.rimor.reflect.CachedMethod;
import st.networkers.rimor.qualify.reflect.QualifiedMethod;

import java.util.Optional;

Expand All @@ -17,7 +17,7 @@ public interface ExecutionContextService {
* given {@code Token}.
*
* @param token the token to get its bound object
* @param context the context of a command execution
* @param context the context of the command execution
* @return an {@link Optional} wrapping the object bound to the token, or empty
*/
<T> Optional<T> get(Token<T> token, ExecutionContext context);
Expand All @@ -29,19 +29,19 @@ public interface ExecutionContextService {
* given {@code Token}.
*
* @param token the token to get its bound object
* @param context the context of a command execution
* @param bean the bean to use its local providers
* @param context the context of the command execution
* @return an {@link Optional} wrapping the object bound to the token, or empty
*/
<T> Optional<T> get(Token<T> token, ExecutionContext context, Object bean);
<T> Optional<T> get(Token<T> token, Object bean, ExecutionContext context);

/**
* Invokes the given method injecting all its parameters following {@link #get(Token, ExecutionContext)}
*
* @param cachedMethod the method to invoke
* @param instance an instance of the method's class to invoke it on, or {@code null} if static
* @param context the context of a command execution
* @param qualifiedMethod the method to invoke
* @param instance an instance of the method's class to invoke it on, or {@code null} if static
* @param context the context of the command execution
* @return the result of executing the method
*/
Object invokeMethod(CachedMethod cachedMethod, Object instance, ExecutionContext context);
Object invokeMethod(QualifiedMethod qualifiedMethod, Object instance, ExecutionContext context);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import st.networkers.rimor.bean.BeanManager;
import st.networkers.rimor.context.provide.ExecutionContextProviderRegistry;
import st.networkers.rimor.reflect.CachedMethod;
import st.networkers.rimor.reflect.CachedParameter;
import st.networkers.rimor.qualify.reflect.QualifiedMethod;
import st.networkers.rimor.qualify.reflect.QualifiedParameter;
import st.networkers.rimor.util.OptionalUtils;
import st.networkers.rimor.util.ReflectionUtils;

Expand Down Expand Up @@ -32,7 +32,7 @@ public <T> Optional<T> get(Token<T> token, ExecutionContext context) {
}

@Override
public <T> Optional<T> get(Token<T> token, ExecutionContext context, Object bean) {
public <T> Optional<T> get(Token<T> token, Object bean, ExecutionContext context) {
return OptionalUtils.firstPresent(
context.get(token),
() -> this.fromProviderRegistry(beanProviderRegistries.get(bean), token, context),
Expand All @@ -45,16 +45,16 @@ private <T> Optional<T> fromProviderRegistry(ExecutionContextProviderRegistry ex
}

@Override
public Object invokeMethod(CachedMethod method, Object instance, ExecutionContext context) {
return ReflectionUtils.invoke(method.getMethod(), instance, resolveParameters(method, context));
public Object invokeMethod(QualifiedMethod method, Object bean, ExecutionContext context) {
return ReflectionUtils.invoke(method.getMethod(), bean, resolveParameters(method, bean, context));
}

private Object[] resolveParameters(CachedMethod method, ExecutionContext context) {
Object[] parameters = new Object[method.getParameters().size()];
private Object[] resolveParameters(QualifiedMethod method, Object bean, ExecutionContext context) {
Object[] parameters = new Object[method.getQualifiedParameters().size()];

int i = 0;
for (CachedParameter parameter : method.getParameters()) {
parameters[i++] = this.get(ParameterToken.build(method, parameter), context).orElse(null);
for (QualifiedParameter parameter : method.getQualifiedParameters()) {
parameters[i++] = this.get(ParameterToken.build(method, parameter), bean, context).orElse(null);
}

return parameters;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package st.networkers.rimor.context;

import st.networkers.rimor.reflect.CachedMethod;
import st.networkers.rimor.reflect.CachedParameter;
import st.networkers.rimor.qualify.reflect.QualifiedMethod;
import st.networkers.rimor.qualify.reflect.QualifiedParameter;

import java.util.Objects;

Expand All @@ -10,24 +10,24 @@
*/
public class ParameterToken<T> extends Token<T> {

public static ParameterToken<?> build(CachedMethod method, CachedParameter parameter) {
public static ParameterToken<?> build(QualifiedMethod method, QualifiedParameter parameter) {
return new ParameterToken<>(method, parameter);
}

private final CachedMethod method;
private final CachedParameter parameter;
private final QualifiedMethod method;
private final QualifiedParameter parameter;

private ParameterToken(CachedMethod method, CachedParameter parameter) {
super(parameter.getType(), parameter.getAnnotationsMap(), parameter.getRequiredAnnotations());
private ParameterToken(QualifiedMethod method, QualifiedParameter parameter) {
super(parameter.getType(), parameter.getQualifiersMap(), parameter.getRequiredQualifiers());
this.method = method;
this.parameter = parameter;
}

public CachedMethod getMethod() {
public QualifiedMethod getQualifiedMethod() {
return method;
}

public CachedParameter getParameter() {
public QualifiedParameter getQualifiedParameter() {
return parameter;
}

Expand Down
4 changes: 2 additions & 2 deletions rimor/src/main/java/st/networkers/rimor/context/Token.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ public boolean matches(MatchingKey o) {

@Override
public int matchingHashCode() {
Set<Class<? extends Annotation>> annotationTypes = new HashSet<>(this.getRequiredAnnotations());
annotationTypes.addAll(this.getAnnotationsMap().keySet());
Set<Class<? extends Annotation>> annotationTypes = new HashSet<>(this.getRequiredQualifiers());
annotationTypes.addAll(this.getQualifiersMap().keySet());

return Objects.hash(this.type, annotationTypes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import st.networkers.rimor.qualify.DinamicallyQualified;
import st.networkers.rimor.context.ExecutionContext;
import st.networkers.rimor.context.Token;
import st.networkers.rimor.reflect.CachedClass;
import st.networkers.rimor.reflect.CachedMethod;
import st.networkers.rimor.qualify.reflect.QualifiedClass;
import st.networkers.rimor.qualify.reflect.QualifiedMethod;

import java.lang.reflect.Type;
import java.util.Arrays;
Expand Down Expand Up @@ -77,8 +77,8 @@ public Collection<Type> getProvidedTypes() {

private void addPresentAnnotations() {
try {
this.withAnnotationsOf(CachedMethod.build(this.getClass().getMethod("get", Token.class, ExecutionContext.class)))
.withAnnotationsOf(CachedClass.build(this.getClass()));
this.withAnnotationsOf(QualifiedMethod.build(this.getClass().getMethod("get", Token.class, ExecutionContext.class)))
.withAnnotationsOf(QualifiedClass.build(this.getClass()));
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
import java.util.Collection;

/**
* Provides objects at runtime.
* Provides context to executions.
*
* @see ProvidesContext
* @see AbstractExecutionContextProvider
*/
public interface ExecutionContextProvider<T> extends Qualified {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class ExecutionContextProviderRegistry implements Cloneable {
*/
public void register(ExecutionContextProvider<?> provider) {
for (Type type : provider.getProvidedTypes()) {
Token<?> token = Token.of(type, provider.getAnnotationsMap(), provider.getRequiredAnnotations());
Token<?> token = Token.of(type, provider.getQualifiersMap(), provider.getRequiredQualifiers());
this.providers.put(token, provider); // TODO throw if key already present?
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public Optional<?> get(Token<Optional<?>> token, ExecutionContext context) {
Type wrappedType = ReflectionUtils.unwrapOptional(token.getType());

// a token associated with the type and the same annotations of the optional
Token<?> wrappedToken = Token.of(wrappedType, token.getAnnotationsMap(), token.getRequiredAnnotations());
Token<?> wrappedToken = Token.of(wrappedType, token.getQualifiersMap(), token.getRequiredQualifiers());

return executionContextService.get(wrappedToken, context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import st.networkers.rimor.context.ExecutionContext;
import st.networkers.rimor.context.ExecutionContextService;
import st.networkers.rimor.reflect.CachedMethod;
import st.networkers.rimor.qualify.reflect.QualifiedMethod;

import java.lang.reflect.Method;
import java.util.Collection;
Expand All @@ -17,28 +17,28 @@ public static Builder builder() {
private final ExecutionContextService executionContextService;

private final Object bean;
private final CachedMethod method;
private final QualifiedMethod qualifiedMethod;
private final Collection<String> identifiers;

public HandlerMethodInstruction(ExecutionContextService executionContextService, Object bean, CachedMethod method,
Collection<String> identifiers) {
public HandlerMethodInstruction(ExecutionContextService executionContextService, Object bean,
QualifiedMethod qualifiedMethod, Collection<String> identifiers) {
this.executionContextService = executionContextService;
this.bean = bean;
this.method = method;
this.qualifiedMethod = qualifiedMethod;
this.identifiers = identifiers;
}

@Override
public Object run(ExecutionContext executionContext) {
return executionContextService.invokeMethod(method, bean, executionContext);
return executionContextService.invokeMethod(qualifiedMethod, bean, executionContext);
}

public Object getBean() {
return bean;
}

public CachedMethod getMethod() {
return method;
public QualifiedMethod getQualifiedMethod() {
return qualifiedMethod;
}

@Override
Expand All @@ -51,12 +51,12 @@ public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof HandlerMethodInstruction)) return false;
HandlerMethodInstruction that = (HandlerMethodInstruction) o;
return Objects.equals(bean, that.bean) && Objects.equals(method, that.method) && Objects.equals(identifiers, that.identifiers);
return Objects.equals(bean, that.bean) && Objects.equals(qualifiedMethod, that.qualifiedMethod) && Objects.equals(identifiers, that.identifiers);
}

@Override
public int hashCode() {
return Objects.hash(bean, method, identifiers);
return Objects.hash(bean, qualifiedMethod, identifiers);
}

public static class Builder {
Expand Down Expand Up @@ -86,7 +86,7 @@ public Builder identifiers(Collection<String> identifiers) {
}

public HandlerMethodInstruction create() {
return new HandlerMethodInstruction(executionContextService, bean, CachedMethod.build(method), identifiers);
return new HandlerMethodInstruction(executionContextService, bean, QualifiedMethod.build(method), identifiers);
}
}
}
Loading

0 comments on commit 1c91b10

Please sign in to comment.