Thursday, March 3, 2011

AspectJ: parameter in a pointcut

I'm using AspectJ to advice all the public methods which do have an argument of a chosen class. I tried the following:

pointcut permissionCheckMethods(Session sess) : 
    (execution(public * *(.., Session)) && args(*, sess));

This is working wonderfully for methods with at least 2 arguments:

public void delete(Object item, Session currentSession);

but it does not work with methods like:

public List listAll(Session currentSession);

How may I change my pointcut to advice both methods executions? In other words: I expected the ".." wildcard to represent "zero or more arguments", but it looks like it means instead "one or more"...

From stackoverflow
  • Oh well... I worked that around with this nasty trick. Still waiting for someone to show up with an "official" pointcut definition.

    pointcut permissionCheckMethods(EhealthSession eheSess) : 
        (execution(public * *(.., EhealthSession)) && args(*, eheSess))
        && !within(it.___.security.PermissionsCheck);
    
    pointcut permissionCheckMethods2(EhealthSession eheSess) : 
        (execution(public * *(EhealthSession)) && args(eheSess))
        && !within(it.___.security.PermissionsCheck)
        && !within(it.___.app.impl.EhealthApplicationImpl);
    
    before(EhealthSession eheSess) throws AuthorizationException : permissionCheckMethods(eheSess)
    {
        Signature sig = thisJoinPointStaticPart.getSignature(); 
        check(eheSess, sig);
    }
    
    before(EhealthSession eheSess) throws AuthorizationException : permissionCheckMethods2(eheSess)
    {
        Signature sig = thisJoinPointStaticPart.getSignature(); 
        check(eheSess, sig);
    }
    
  • How about:

    pointcut permissionCheckMethods(Session sess) : 
    (execution(public * *(..)) && args(.., sess));
    

    I guess this will match if last (or only) argument is of type Session. By swapping the positions of args you can also match first-or-only. But i don't know if matching any arbitrary position is possible.

0 comments:

Post a Comment