Search code examples
getsetaspectjpointcut

how to write Get() pointcut to retrive the attributes that have been used in the program in AspectJ


I have written the following pointcut but it is giving the run time error (Exception in thread "main" java.lang.StackOverflowError)

pointcut traceAttribs():(get(* *));


Solution

  • What I understand from your question is that you need to create an aspect that knows your methods argument in real time.

    There is a useful simple examples in Mkyong's page and probably your need is on point 8.

    If you want to get a method name and arguments in your aspect you could write something like this:

    @Aspect
    public class LoggingAspect {
    
       @Around("execution(* com.your.package.YourClass.yourMethodAround(..))")
       public void logAround(ProceedingJoinPoint joinPoint) throws Throwable {
    
        System.out.println("Your method: " + joinPoint.getSignature().getName());
        System.out.println("and method arguments: " + Arrays.toString(joinPoint.getArgs()));
    
        System.out.println("Your method will be executed. ");
        joinPoint.proceed(); //continue on the intercepted method
        System.out.println("Ended execution");
    
       }
    }
    

    Hope to help