简体   繁体   中英

How can I exclude getters and setters in aspectJ?

I have a class aspectJ in my maven project whitch hepls me to show the Begin and the End of any called method in my project. I try now to exclude all getters and setters. I try modify this annotation: @Around("execution(public * *(..)) by @Around("execution(public * *(..) && !within(* set*(..))")

But it doesn't whork and it gives me that in the consol:

 [ERROR] Failed to execute goal org.codehaus.mojo:aspectj-maven-plugin:1.7:compile (default) on project spb-lceb: AJC compiler errors:
 [ERROR] error at @Around("execution(public * *(..) && !within(* set*(..))")
 Syntax error on token "execution(public * *(..) && !within(* set*(..))", ")" expected

Any idea

The accepted solution is clearly wrong because within(* set*(..)) will not even compile. This pointcut type needs a type signature, not a method signature. Furthermore, it only tries to take care of setters, not getters as asked by the OP.

The correct solution is:

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

By accepting the wrong solution the OP even irritated someone else trying the same here . This is why after such a long time I am writing this answer.

I think it's only a typo because you have a mising ) at the end of the execution call before the && operator:

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

Should be:

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

Try it, that should do the trick.

And for the methods that begins with Get the best solution is to rename them to get rid of this conflict.

//package name to exclude
@Pointcut("execution(* com.aasif.dao.*.*(..))")
private void forDaoPackage() {}

//getter
@Pointcut("execution(* com.aasif.dao.*.get*(..))")
private void getter() {}

//setter
@Pointcut("execution(* com.aasif.dao.*.set*(..))")
private void setter() {}

//excluding getter and setter  method 
@Pointcut("forDaoPackage() && !(getter() || setter())")
private void forNoGetterSetter() {}


//applying to advice
@Before("forNoGetterSetter()")
public void excludingGetter() {
    System.out.println("performCloud()");
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM