3

Im working with some AspectJ code and i want to catch all the executions for none private pointcuts.

@Pointcut("execution(public * *(..))")//Public
public void publicMethod(){};
@Pointcut("execution(protected * *(..))"//Protected
public void protectedMethod(){}

@Pointcut("@annotation(mypackage.name.annotationName")
public void annotationPointcut(){}

@Around("annotationPointcut() && (protectedMethod() || publicMethod())")
public Object test(){ System.out.println("Should not print private"); }

I read about using ! (not) but could not get it to work. Something like

@Pointcut("!execution(private * *(..))"

But without getting it to work.

I could not find a modifier name for default class modifier in the aspectJ, have I missed it or do i need to try and solve it by using ! not sign in some sort of way?

Regards a new dev that are learning aspectJ

Nosfert
  • 382
  • 1
  • 3
  • 11

1 Answers1

2

Try this to catch all non private methods.

@Pointcut("execution(!private * *(..))")
dogant
  • 1,376
  • 1
  • 10
  • 23
  • 1
    This is correct, but I want to add an explanation why `!execution(private * *(..))` is wrong: It means "intercept all joinpoints which are something else than private method executions". While this does intercept public, protected and package-local method executions, it also intercepts many more pointcuts, e.g. method calls (not the same as executions), constructor calls and executions, static and method constructor inintialisations, member get/set access and so forth. Thus, you get way too many hits. This is why *wheee* is right and *Nosfert* is not. – kriegaex Aug 04 '15 at 20:48
  • Sweet! It were all about where the ! sign should be! :) – Nosfert Aug 05 '15 at 16:34