Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] Problem with @this(..)

Hi,

i got a class where some methods need authentication and different access 
levels.

The access level required is described by an annotation:

class Sample {
  @AuthRequired(AuthLevel.ADMIN)
  public int doSomethingSerious() { ... }

  @AuthRequired(AuthLevel.USER)
  public void doSomethingElse() { ... }
}

I tried to write a pointcut and an advise like described in AspectJ 5 
Developer's Notebook, chapter 2, Signature Patterns:

public aspect AuthenticationAspect {
  pointcut authRequiredMethodExecution(Sample s, AuthRequired ar)
    :execution(@AuthRequired * Sample+.*(..)) && this(s) && @this(ar);

  before(Sample s, AuthRequired ar) : authRequiredMethodExecution(s,ar) {
    doSomethingUsefull();
  }
}

The problem is, that my advise never gets executed. I read on within the 
documentation and found out that @this(..) matches objects which are 
annotated with the specified annotation, that means my advise would only 
get executed if the class Sample has the annotation @AuthRequired...
Somehow that is a contradiction to one of the examples mentioned in the 
documentation:

pointcut txRequiredMethod(Tx transactionAnnotation) :
  	    execution(* *(..)) && @this(transactionAnnotation) 
  	    && if(transactionAnnotation.policy() == TxPolicy.REQUIRED);

This sample looks like @this could be used to catch method annotations...

My question is now, how can i write a pointcut which is applied to methods 
with @AuthRequired and an advise with access to this annotation?

Thanks

Best regards
T. Jungblut


Back to the top