0

I am using AspectJ annotations and for some reason it seems that the resolution scope of pointcuts differs for a named pointcut versus an anonymous pointcut.

For example in the code below an identical pointcut is resolved if anonymous but not when it is named. The named pointcut will however match if I use a wildcard instead of a specific type.

Any thoughts?

import some_other_package.not_the_one_where_this_aspect_is.Account;

@Aspect
public class MyClass {


//this does not match... but matches if Account is replaced by *
@Pointcut("execution(* Account.withdraw(..)) && args(amount)")
public void withdr(double amount){}

@Before("withdr(amount)")
public void dosomething1(double amount){}


//this matches
@Before("execution(* Account.withdraw(..)) && args(amount)")
public void dosomthing2(double amount){}

}
ccol002
  • 25
  • 6

1 Answers1

0

In @AspectJ syntax imports are not useful according to the documentation. You need to either fully qualify class names or use jokers. That the import works in your second case, is rather an aberration from the expected behaviour than to be expected. You cannot rely on it.

If you do it like this, both variants will work:

@Aspect
public class AccountInterceptor {
    @Pointcut("execution(* *..Account.withdraw(..)) && args(amount)")
    public void withdraw(double amount) {}

    @Before("withdraw(amount)")
    public void doSomething1(JoinPoint joinPoint, double amount) {
        System.out.println(joinPoint + " -> " + amount);
    }

    @Before("execution(* *..Account.withdraw(..)) && args(amount)")
    public void doSomething2(JoinPoint joinPoint, double amount) {
        System.out.println(joinPoint + " -> " + amount);
    }
}
kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • Maybe the "..." was misleading (I edited it now). Account is a fully qualified class name. I should think that this is actually a bug. – ccol002 Apr 04 '13 at 15:19
  • I did not refer to the import, I knew what you meant. You need the fully qualified class name in your pointcut! I guess Andy Clement also told you so on the AspectJ mailing list, I just realised. – kriegaex Apr 04 '13 at 18:37
  • OK now I got it. Sorry I missed Andy's answer. – ccol002 Apr 05 '13 at 06:22