Skip to content

Commit 23d95a2

Browse files
committed
✨ feat: add annotation @ExecutorSince #4
1 parent e8d8b73 commit 23d95a2

File tree

5 files changed

+86
-6
lines changed

5 files changed

+86
-6
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.clarify4j.config.annotation;
2+
3+
import java.lang.annotation.*;
4+
5+
@Documented
6+
@Retention(RetentionPolicy.RUNTIME)
7+
@Target({ElementType.METHOD, ElementType.TYPE})
8+
public @interface ExecutorSince {
9+
}

plugin/src/main/groovy/org/clarify4j/config/annotation/Saga.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,4 @@
99
String expression();
1010

1111
boolean disable() default false;
12-
13-
Class<?> clazz() default Saga.class;
1412
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.clarify4j.config.handler;
2+
3+
import org.aspectj.lang.ProceedingJoinPoint;
4+
import org.aspectj.lang.Signature;
5+
import org.aspectj.lang.annotation.Around;
6+
import org.aspectj.lang.annotation.Aspect;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
import org.springframework.stereotype.Component;
10+
import org.unify4j.common.Time4j;
11+
12+
import java.util.Date;
13+
14+
@Aspect
15+
@Component
16+
public class ExecutorSinceHandler {
17+
protected static final Logger logger = LoggerFactory.getLogger(ExecutorSinceHandler.class);
18+
19+
@Around(value = "@annotation(org.clarify4j.config.annotation.ExecutorSince)")
20+
public Object execute(ProceedingJoinPoint joinPoint) throws Throwable {
21+
Date start = new Date();
22+
Object proceed = joinPoint.proceed();
23+
String since = Time4j.sinceSmallRecently(start, new Date());
24+
Signature signature = joinPoint.getSignature();
25+
String clazz = joinPoint.getTarget().getClass().getSimpleName();
26+
String method = signature.getName();
27+
logger.info("Execution of method '{}' in class '{}' completed in {}", method, clazz, since);
28+
return proceed;
29+
}
30+
}

plugin/src/main/groovy/org/clarify4j/config/handler/SagaHandler.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import org.springframework.expression.spel.standard.SpelExpressionParser;
1515
import org.springframework.expression.spel.support.StandardEvaluationContext;
1616
import org.springframework.stereotype.Component;
17-
import org.unify4j.common.Object4j;
1817

1918
import java.lang.reflect.Method;
2019

@@ -45,9 +44,6 @@ public void handle(JoinPoint joinPoint) throws Throwable {
4544
context.setVariable(parameters[i], args[i]);
4645
}
4746
String value = EXPRESSION_PARSER.parseExpression(saga.expression(), TEMPLATE_PARSER_CONTEXT).getValue(context, String.class);
48-
if (Object4j.allNotNull(saga.clazz())) {
49-
logger = LoggerFactory.getLogger(saga.clazz());
50-
}
5147
logger.info(value);
5248
}
5349
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.clarify4j;
2+
3+
import org.clarify4j.config.annotation.ExecutorSince;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.lang.annotation.Annotation;
7+
import java.lang.annotation.Documented;
8+
import java.lang.annotation.Retention;
9+
import java.lang.annotation.RetentionPolicy;
10+
import java.lang.reflect.Method;
11+
12+
import static org.junit.jupiter.api.Assertions.assertEquals;
13+
import static org.junit.jupiter.api.Assertions.assertNotNull;
14+
15+
public class ExecutorSinceTest {
16+
@ExecutorSince
17+
private static class TestClass {
18+
@ExecutorSince
19+
public void testMethod() {
20+
}
21+
}
22+
23+
@Test
24+
void annotationTypeShouldBeAvailableOnClass() {
25+
ExecutorSince annotation = TestClass.class.getAnnotation(ExecutorSince.class);
26+
assertNotNull(annotation, "Annotation should be present on the class.");
27+
}
28+
29+
@Test
30+
void annotationTypeShouldBeAvailableOnMethod() throws NoSuchMethodException {
31+
Method method = TestClass.class.getMethod("testMethod");
32+
ExecutorSince annotation = method.getAnnotation(ExecutorSince.class);
33+
assertNotNull(annotation, "Annotation should be present on the method.");
34+
}
35+
36+
@Test
37+
void shouldHaveRuntimeRetention() {
38+
RetentionPolicy retentionPolicy = ExecutorSince.class.getAnnotation(Retention.class).value();
39+
assertEquals(RetentionPolicy.RUNTIME, retentionPolicy, "Retention policy should be RUNTIME.");
40+
}
41+
42+
@Test
43+
void shouldBeDocumented() {
44+
Annotation documented = ExecutorSince.class.getAnnotation(Documented.class);
45+
assertNotNull(documented, "Annotation should be documented.");
46+
}
47+
}

0 commit comments

Comments
 (0)