Skip to content

Commit 0a754b4

Browse files
committed
refactoring to simplify 'around' method
1 parent e1da3f8 commit 0a754b4

File tree

1 file changed

+18
-12
lines changed
  • powertools-idempotency/src/main/java/software/amazon/lambda/powertools/idempotency/internal

1 file changed

+18
-12
lines changed

powertools-idempotency/src/main/java/software/amazon/lambda/powertools/idempotency/internal/IdempotentAspect.java

+18-12
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
import org.aspectj.lang.annotation.Aspect;
2020
import org.aspectj.lang.annotation.Pointcut;
2121
import org.aspectj.lang.reflect.MethodSignature;
22-
import org.slf4j.Logger;
23-
import org.slf4j.LoggerFactory;
2422
import software.amazon.lambda.powertools.idempotency.Constants;
2523
import software.amazon.lambda.powertools.idempotency.IdempotencyKey;
2624
import software.amazon.lambda.powertools.idempotency.Idempotent;
@@ -39,8 +37,6 @@
3937
*/
4038
@Aspect
4139
public class IdempotentAspect {
42-
private static final Logger LOG = LoggerFactory.getLogger(IdempotentAspect.class);
43-
4440
@SuppressWarnings({"EmptyMethod"})
4541
@Pointcut("@annotation(idempotent)")
4642
public void callAt(Idempotent idempotent) {
@@ -59,8 +55,24 @@ public Object around(ProceedingJoinPoint pjp,
5955
if (method.getReturnType().equals(void.class)) {
6056
throw new IdempotencyConfigurationException("The annotated method doesn't return anything. Unable to perform idempotency on void return type");
6157
}
62-
JsonNode payload = null;
6358

59+
JsonNode payload = getPayload(pjp, method);
60+
if (payload == null) {
61+
throw new IdempotencyConfigurationException("Unable to get payload from the method. Ensure there is at least one parameter or that you use @IdempotencyKey");
62+
}
63+
64+
IdempotencyHandler idempotencyHandler = new IdempotencyHandler(pjp, method.getName(), payload);
65+
return idempotencyHandler.handle();
66+
}
67+
68+
/**
69+
* Retrieve the payload from the annotated method parameters
70+
* @param pjp joinPoint
71+
* @param method the annotated method
72+
* @return the payload used for idempotency
73+
*/
74+
private JsonNode getPayload(ProceedingJoinPoint pjp, Method method) {
75+
JsonNode payload = null;
6476
// handleRequest or method with one parameter: get the first one
6577
if ((isHandlerMethod(pjp) && placedOnRequestHandler(pjp))
6678
|| pjp.getArgs().length == 1) {
@@ -77,12 +89,6 @@ public Object around(ProceedingJoinPoint pjp,
7789
}
7890
}
7991
}
80-
81-
if (payload == null) {
82-
throw new IdempotencyConfigurationException("Unable to get payload from the method. Ensure there is at least one parameter or that you use @IdempotencyKey");
83-
}
84-
85-
IdempotencyHandler idempotencyHandler = new IdempotencyHandler(pjp, method.getName(), payload);
86-
return idempotencyHandler.handle();
92+
return payload;
8793
}
8894
}

0 commit comments

Comments
 (0)