Search code examples
javaaopaspectj

How to configure aspectj ignore getters and setters


I have an aspect that currently works to capture all public method executions within my package.

I would like to modify that to exclude both setters and getters, so I tried that and these are the variants I tried:

This one works, but does obviously does not do anything for setters or getters.

@Around("execution(public * *(..)) && !within(com.walterjwhite.logging..*)")

This does not compile:

@Around("execution(public * *(..)) && !within(* set*(..))")

This compiles, but doesn't prevent capturing setters/getters:

@Around("execution(public * *(..)) && !execution(* set*(..))")

I also came across this post as a reference, but that didn't work. I get compilation errors when trying to compile the aspect.

How can I exclude getters and setters in aspectJ?


Solution

  • The second one does not compile because within() needs a type signature, not a method signature. The answer you are referring to is just plain wrong, I have no idea why it was accepted. I just wrote a new one in order to correct that false information.

    Your last try is basically right, but only ignores setters, not getters. Try this:

    Driver application:

    package de.scrum_master.app;
    
    public class Application {
      private int id;
      private String name;
    
      public int getId() {
        return id;
      }
      public void setId(int id) {
        this.id = id;
      }
      public String getName() {
        return name;
      }
    
      public void setName(String name) {
        this.name = name;
      }
    
      public void doSomething() {
        System.out.println("Doing something");
      }
    
      public static void main(String[] args) {
        Application application = new Application();
        application.setId(11);
        application.setName("John Doe");
        application.doSomething();
        System.out.println(application.getId() + " - " + application.getName());
      }
    }
    

    Aspect:

    package de.scrum_master.aspect;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    
    @Aspect
    public class MyAspect {
      @Around("execution(public * *(..)) && !execution(void set*(*)) && !execution(!void get*())")
      public Object myAdvice(ProceedingJoinPoint thisJoinPoint) throws Throwable {
        System.out.println(thisJoinPoint);
        return thisJoinPoint.proceed();
      }
    }
    

    Console log:

    execution(void de.scrum_master.app.Application.main(String[]))
    execution(void de.scrum_master.app.Application.doSomething())
    Doing something
    11 - John Doe
    

    Please note that

    • if you have getters for boolean values like isActive() and also want to ignore them, you have to extend the pointcut by && !execution(boolean is*()).
    • those kinds of pointcuts with name patterns are always just heuristics, so beware of method names such as getaway, settleConflict, isolate. They would also be ignored.