Skip to content

Commit

Permalink
✨ feat: add annotation @ExecutorSince #4
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Sep 1, 2024
1 parent e8d8b73 commit 23d95a2
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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 {
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,4 @@
String expression();

boolean disable() default false;

Class<?> clazz() default Saga.class;
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}
47 changes: 47 additions & 0 deletions plugin/src/test/groovy/org/clarify4j/ExecutorSinceTest.java
Original file line number Diff line number Diff line change
@@ -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.");
}
}

0 comments on commit 23d95a2

Please sign in to comment.