Custom annotation is not working when applied on invoked method

Viewed 27

I have created a custom annotation to log the time taken by a method to execute.

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface InterceptExecutionTime {
    String action() default "";
}

@Around("@annotation(InterceptExecutionTime) && execution(* *(..))")
    public void logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
        String action = null;
        try {
            final MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            final InterceptExecutionTime annotation = signature.getMethod().getAnnotation(InterceptExecutionTime.class);
            action = annotation.action();

            long startTimeInMills = System.currentTimeMillis();
            joinPoint.proceed();

            long endTimeInMills = System.currentTimeMillis();
            long executionTime = endTimeInMills - startTimeInMills;
            log.info(String.format("Time taken to execute actions %s is %s (in milliseconds)", action, executionTime));

        }
        catch (Exception exception) {
            log.error(String.format("unable to log execution time for actions %s", action));
        }
    }

Now when I am using this annotation on a method like this:-

@Component
public class CustomerQueryHandler {

    public CustomerDocument getCustomerDocument(int userId, String documentType) throws JsonProcessingException {
        CustomerDocument.DocumentType customerDocumentType = CustomerDocument.DocumentType.fromString(documentType);

        CustomerDocument customerDocument = customerDocumentRepository.findByUserIdAndDocumentTypeAndIsDeletedFalse(userId, customerDocumentType);

        if (customerDocument == null) {
            throw new NoSuchCustomerDocumentException(String.format("No %s document found for userId %s", documentType, userId));
        }

        return mapToCustomerDocument(customerDocument);
    }

    @InterceptExecutionTime(action = "fetch customer document")
    public CustomerDocument mapToCustomerDocument(CustomerDocument customerDocument) throws JsonProcessingException {
        return new CustomerDocument(customerDocument.getVerificationType() != null ?
                customerDocument.getVerificationType().name() : null,
                null);
    }
}

This is not working. But when i use the annotation on the method - getCustomerDocument(). It is working perfectly fine.

@Component
public class CustomerQueryHandler {

    @InterceptExecutionTime(action = "Decrypt customer document")
    public CustomerDocument getCustomerDocument(int userId, String documentType) throws JsonProcessingException {
        CustomerDocument.DocumentType customerDocumentType = CustomerDocument.DocumentType.fromString(documentType);

        CustomerDocument customerDocument = customerDocumentRepository.findByUserIdAndDocumentTypeAndIsDeletedFalse(userId, customerDocumentType);

        if (customerDocument == null) {
            throw new NoSuchCustomerDocumentException(String.format("No %s document found for userId %s", documentType, userId));
        }

        return mapToCustomerDocument(customerDocument);
    }

   
    public CustomerDocument mapToCustomerDocument(CustomerDocument customerDocument) throws JsonProcessingException {
        return new CustomerDocument(customerDocument.getVerificationType() != null ?
                customerDocument.getVerificationType().name() : null,
                null);
    }
}

Can anyone please help what am i doing wrong here ?

0 Answers
Related