From 23d95a298b72dc3a1e416231b0354d8101096b82 Mon Sep 17 00:00:00 2001 From: arisnguyenit97 Date: Sun, 1 Sep 2024 21:34:02 +0700 Subject: [PATCH] :sparkles: feat: add annotation @ExecutorSince #4 --- .../config/annotation/ExecutorSince.java | 9 ++++ .../org/clarify4j/config/annotation/Saga.java | 2 - .../config/handler/ExecutorSinceHandler.java | 30 ++++++++++++ .../clarify4j/config/handler/SagaHandler.java | 4 -- .../org/clarify4j/ExecutorSinceTest.java | 47 +++++++++++++++++++ 5 files changed, 86 insertions(+), 6 deletions(-) create mode 100644 plugin/src/main/groovy/org/clarify4j/config/annotation/ExecutorSince.java create mode 100644 plugin/src/main/groovy/org/clarify4j/config/handler/ExecutorSinceHandler.java create mode 100644 plugin/src/test/groovy/org/clarify4j/ExecutorSinceTest.java diff --git a/plugin/src/main/groovy/org/clarify4j/config/annotation/ExecutorSince.java b/plugin/src/main/groovy/org/clarify4j/config/annotation/ExecutorSince.java new file mode 100644 index 0000000..f0684f8 --- /dev/null +++ b/plugin/src/main/groovy/org/clarify4j/config/annotation/ExecutorSince.java @@ -0,0 +1,9 @@ +package org.clarify4j.config.annotation; + +import java.lang.annotation.*; + +@Documented +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.METHOD, ElementType.TYPE}) +public @interface ExecutorSince { +} diff --git a/plugin/src/main/groovy/org/clarify4j/config/annotation/Saga.java b/plugin/src/main/groovy/org/clarify4j/config/annotation/Saga.java index 1f8f6f9..72be58f 100644 --- a/plugin/src/main/groovy/org/clarify4j/config/annotation/Saga.java +++ b/plugin/src/main/groovy/org/clarify4j/config/annotation/Saga.java @@ -9,6 +9,4 @@ String expression(); boolean disable() default false; - - Class clazz() default Saga.class; } diff --git a/plugin/src/main/groovy/org/clarify4j/config/handler/ExecutorSinceHandler.java b/plugin/src/main/groovy/org/clarify4j/config/handler/ExecutorSinceHandler.java new file mode 100644 index 0000000..b06407f --- /dev/null +++ b/plugin/src/main/groovy/org/clarify4j/config/handler/ExecutorSinceHandler.java @@ -0,0 +1,30 @@ +package org.clarify4j.config.handler; + +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.Signature; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; +import org.unify4j.common.Time4j; + +import java.util.Date; + +@Aspect +@Component +public class ExecutorSinceHandler { + protected static final Logger logger = LoggerFactory.getLogger(ExecutorSinceHandler.class); + + @Around(value = "@annotation(org.clarify4j.config.annotation.ExecutorSince)") + public Object execute(ProceedingJoinPoint joinPoint) throws Throwable { + Date start = new Date(); + Object proceed = joinPoint.proceed(); + String since = Time4j.sinceSmallRecently(start, new Date()); + Signature signature = joinPoint.getSignature(); + String clazz = joinPoint.getTarget().getClass().getSimpleName(); + String method = signature.getName(); + logger.info("Execution of method '{}' in class '{}' completed in {}", method, clazz, since); + return proceed; + } +} diff --git a/plugin/src/main/groovy/org/clarify4j/config/handler/SagaHandler.java b/plugin/src/main/groovy/org/clarify4j/config/handler/SagaHandler.java index bfe0b92..a634a0a 100644 --- a/plugin/src/main/groovy/org/clarify4j/config/handler/SagaHandler.java +++ b/plugin/src/main/groovy/org/clarify4j/config/handler/SagaHandler.java @@ -14,7 +14,6 @@ import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.expression.spel.support.StandardEvaluationContext; import org.springframework.stereotype.Component; -import org.unify4j.common.Object4j; import java.lang.reflect.Method; @@ -45,9 +44,6 @@ public void handle(JoinPoint joinPoint) throws Throwable { context.setVariable(parameters[i], args[i]); } String value = EXPRESSION_PARSER.parseExpression(saga.expression(), TEMPLATE_PARSER_CONTEXT).getValue(context, String.class); - if (Object4j.allNotNull(saga.clazz())) { - logger = LoggerFactory.getLogger(saga.clazz()); - } logger.info(value); } } diff --git a/plugin/src/test/groovy/org/clarify4j/ExecutorSinceTest.java b/plugin/src/test/groovy/org/clarify4j/ExecutorSinceTest.java new file mode 100644 index 0000000..57e89fc --- /dev/null +++ b/plugin/src/test/groovy/org/clarify4j/ExecutorSinceTest.java @@ -0,0 +1,47 @@ +package org.clarify4j; + +import org.clarify4j.config.annotation.ExecutorSince; +import org.junit.jupiter.api.Test; + +import java.lang.annotation.Annotation; +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.reflect.Method; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class ExecutorSinceTest { + @ExecutorSince + private static class TestClass { + @ExecutorSince + public void testMethod() { + } + } + + @Test + void annotationTypeShouldBeAvailableOnClass() { + ExecutorSince annotation = TestClass.class.getAnnotation(ExecutorSince.class); + assertNotNull(annotation, "Annotation should be present on the class."); + } + + @Test + void annotationTypeShouldBeAvailableOnMethod() throws NoSuchMethodException { + Method method = TestClass.class.getMethod("testMethod"); + ExecutorSince annotation = method.getAnnotation(ExecutorSince.class); + assertNotNull(annotation, "Annotation should be present on the method."); + } + + @Test + void shouldHaveRuntimeRetention() { + RetentionPolicy retentionPolicy = ExecutorSince.class.getAnnotation(Retention.class).value(); + assertEquals(RetentionPolicy.RUNTIME, retentionPolicy, "Retention policy should be RUNTIME."); + } + + @Test + void shouldBeDocumented() { + Annotation documented = ExecutorSince.class.getAnnotation(Documented.class); + assertNotNull(documented, "Annotation should be documented."); + } +}