Skip to content

Commit

Permalink
refactor: optimize code and remove deprecated apis (GitHub #272)
Browse files Browse the repository at this point in the history
  • Loading branch information
Createsequence committed Apr 27, 2024
1 parent eefe369 commit 1f035ac
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,6 @@
*/
public enum MappingType {

/**
* <p>Instead of mapping by key,
* combine key and value directly into a Map collection in sequence,
* such as {@code Collections#zip}.
*
* <p>When using this type,
* the return {@link ContainerMethod#resultKey()} and {@link ContainerMethod#resultType()} are <strong>ignored</strong>.
*
* @see 2.4.0
* @deprecated Use {@link #ORDER_OF_KEYS} instead.
*/
@Deprecated
NONE,

/**
* <p>Instead of mapping by key,
* combine key and value directly into a Map collection in sequence,
Expand All @@ -37,18 +23,6 @@ public enum MappingType {
*/
ORDER_OF_KEYS,

/**
* <p>The return value of the method is already a {@link Map} set grouped by the key value.
* No further conversion is required according to the key value.
*
* <p>When using this type,
* the return {@link ContainerMethod#resultKey()} and {@link ContainerMethod#resultType()} are <strong>ignored</strong>.
*
* @deprecated Use {@link #NO_MAPPING} instead.
*/
@Deprecated
MAPPED,

/**
* <p>The return value of the method is already a {@link Map} set grouped by the key value.
* No further conversion is required according to the key value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import cn.crane4j.core.util.Asserts;
import cn.crane4j.core.util.ClassUtils;
import cn.crane4j.core.util.StringUtils;
import lombok.Builder;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.nullness.qual.NonNull;
Expand Down Expand Up @@ -53,10 +55,17 @@ public MethodInvokerContainer createContainer(
@Nullable Object target, Method method, MappingType mappingType,
@Nullable String namespace, Class<?> resultType, String resultKey, DuplicateStrategy duplicateStrategy) {
log.debug("create method container from [{}]", method);
return doCreateContainer(
target, getMethodInvoker(target, method), method,
mappingType, namespace, resultType, resultKey, duplicateStrategy
);
MethodInvokerContainerCreation containerCreation = MethodInvokerContainerCreation.builder()
.target(target)
.method(method)
.methodInvoker(getMethodInvoker(target, method))
.mappingType(mappingType)
.namespace(namespace)
.resultType(resultType)
.resultKey(resultKey)
.duplicateStrategy(duplicateStrategy)
.build();
return doCreateContainer(containerCreation);
}

/**
Expand All @@ -74,32 +83,46 @@ target, getMethodInvoker(target, method), method,
public MethodInvokerContainer createContainer(
@Nullable Object target, MethodInvoker methodInvoker, MappingType mappingType,
@Nullable String namespace, Class<?> resultType, String resultKey, DuplicateStrategy duplicateStrategy) {
return doCreateContainer(
target, methodInvoker, null,
mappingType, namespace, resultType, resultKey, duplicateStrategy
);
MethodInvokerContainerCreation containerCreation = MethodInvokerContainerCreation.builder()
.target(target)
.methodInvoker(methodInvoker)
.mappingType(mappingType)
.namespace(namespace)
.resultType(resultType)
.resultKey(resultKey)
.duplicateStrategy(duplicateStrategy)
.build();
return doCreateContainer(containerCreation);
}

private MethodInvokerContainer doCreateContainer(
@Nullable Object target, MethodInvoker methodInvoker, @Nullable Method method, MappingType mappingType,
@Nullable String namespace, Class<?> resultType, String resultKey, DuplicateStrategy duplicateStrategy) {
private MethodInvokerContainer doCreateContainer(MethodInvokerContainerCreation containerCreation) {
Method method = containerCreation.getMethod();
String namespace = getNamespace(method, containerCreation.getNamespace());
MappingType mappingType = containerCreation.getMappingType();
MethodInvoker methodInvoker = containerCreation.getMethodInvoker();
Object target = containerCreation.getTarget();

namespace = getNamespace(method, namespace);
MethodInvokerContainer container;
if (mappingType == MappingType.NO_MAPPING || mappingType == MappingType.MAPPED) {
if (mappingType == MappingType.NO_MAPPING) {
container = doCreateNoMappingContainer(target, methodInvoker, method, namespace);
} else if (mappingType == MappingType.ORDER_OF_KEYS || mappingType == MappingType.NONE) {
} else if (mappingType == MappingType.ORDER_OF_KEYS) {
Asserts.isNotNull(method, "method must not be null when mapping type is [{}]", mappingType);
// fix https://gitee.com/opengoofy/crane4j/issues/I97R7E
container = isSingleParameterMethod(method) ?
doCreateSingleKeyContainer(target, methodInvoker, namespace) :
doCreateOrderOfKeysContainer(target, methodInvoker, method, namespace);
} else if (mappingType == MappingType.ONE_TO_ONE) {
container = doCreateOneToOneContainer(
target, methodInvoker, method, namespace, resultType, resultKey, duplicateStrategy);
target, methodInvoker, method, namespace,
containerCreation.getResultType(), containerCreation.getResultKey(),
containerCreation.getDuplicateStrategy()
);
} else if (mappingType == MappingType.ONE_TO_MANY) {
container = doCreateOneToManyContainer(
target, methodInvoker, method, namespace, resultType, resultKey, duplicateStrategy);
target, methodInvoker, method, namespace,
containerCreation.getResultType(), containerCreation.getResultKey(),
containerCreation.getDuplicateStrategy()
);
} else {
throw new Crane4jException("unsupported mapping type: " + mappingType);
}
Expand Down Expand Up @@ -229,4 +252,20 @@ protected MethodInvoker findKeyGetter(Class<?> resultType, String resultKey) {
Asserts.isNotNull(keyGetter, "cannot find getter method [{}] on [{}]", resultKey, resultType);
return keyGetter;
}

@Getter
@Builder
private static class MethodInvokerContainerCreation {
@Nullable
private final Object target;
@Nullable
private final Method method;
private final MethodInvoker methodInvoker;
private final MappingType mappingType;
@Nullable
private final String namespace;
private final Class<?> resultType;
private final String resultKey;
private final DuplicateStrategy duplicateStrategy;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void findSetter() {
}

@AllArgsConstructor
private static class Foo {
protected static class Foo {

@Getter
private Integer id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public Object read(Object node, String propertyName) {
JsonNode jsonNode = jsonNodeAssistant.convertTargetToJsonNode(node);
String actualPropertyName = jsonNodeAssistant.determinePropertyName(propertyName);
JsonNode result = jsonNode.get(actualPropertyName);
// TODO how to handle the value if accept type of container is not string?
// if the result is a value node, return the text value of it
return Objects.nonNull(result) && result.isValueNode() ? result.asText() : result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public class MethodResultAutoOperateAdvisor
&& AnnotatedElementUtils.isAnnotated(m, AutoOperate.class)
);


@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
Method method = methodInvocation.getMethod();
Expand Down

0 comments on commit 1f035ac

Please sign in to comment.