0

I have this aspect that is called on every method annotated with @LogActivity annotation.

@Aspect
@Component
public class EndpointAspect {

    @Around("@annotation(LogActivity)")
    public Object logActivity(ProceedingJoinPoint joinPoint) throws Throwable {
        Object result = joinPoint.proceed();
        // I want to access message here. see below
        return result;
    }

}

And this is one annotated method:

public class contrlr {

    @LogActivity
    @GetRequest("/home")
    public String aMethod() {
        String message = "some RUNTIME-GENERATED message"; // I want to send it to aspect
        return "home.html"
    }

}

I want to pass the message to aspect. note that message may not be hard-coded constant. How to do that?

Ali Tavakol
  • 405
  • 3
  • 11
  • Does this answer your question? [How to get a method's annotation value from a ProceedingJoinPoint?](https://stackoverflow.com/questions/21275819/how-to-get-a-methods-annotation-value-from-a-proceedingjoinpoint) – tsamridh86 Jan 26 '22 at 14:10
  • @tsamridh86 value here is not constant. edited. – Ali Tavakol Jan 26 '22 at 14:12
  • You will have to `return` that message, is it acceptable? I don't think any kind of reflection will allow you to go inside a method and pick out a specific `String` – tsamridh86 Jan 26 '22 at 14:14
  • but it is a controller method that handles a @GetRequest and returns a string to a specific thymeleaf html page template name. the real thing I want is how to log user activity on certain actions in a decoupled way. – Ali Tavakol Jan 26 '22 at 14:17
  • 1
    you might have to move those actions into a separate method, and then apply annotations on it, if you cannot directly modify it. Otherwise, we can wait for other answers too, i've bookmarked the question for my own future reading. – tsamridh86 Jan 26 '22 at 14:19
  • but wait; what if we remove the aspect or if no one handles the annotation? – Ali Tavakol Jan 26 '22 at 14:20
  • if no one handles the annotations, then nothing is going to happen, it just has an annotation. – tsamridh86 Jan 26 '22 at 14:21
  • 2
    I agree with @tsamridh86, you either need to return the desired message so that in the aspect you will have access, or if that is not possible, then extracting the "massaging" logic in subsequent methods, which are themselves annotated with your LogActivity annotation. – David Laci Jan 26 '22 at 16:21

0 Answers0