AspectJ: parameter in pointcut

I use AspectJ to advise all public methods that have an argument of the selected class. I tried the following:

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

This works great for methods with at least two arguments:

 public void delete(Object item, Session currentSession); 

but it does not work with methods such as:

 public List listAll(Session currentSession); 

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

+6
aspectj
source share
4 answers

Well, well ... I worked with this nasty trick. Still waiting for someone to appear 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); } 
+8
source share

What about:

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

I think this will match if the last (or only) argument is of type Session. By rearranging args, you can also match the first or only. But I do not know if a coincidence of an arbitrary position is possible.

+4
source share

I cannot extend the AspectJ syntax for you, but I can offer a workaround. But first let me explain why it is impossible to do what you want with the args definition in pointcut: because if you map your EhealthSession parameter EhealthSession in the method signature, how does AspectJ handle the case where the signature contains several parameters of this class? The value of eheSess would be mixed.

Now there’s a workaround: it can be slower - how much it depends on your environment, just check it out - but you can just bind a pointcut to all potential methods regardless of their parameter list, and then give advice to find the parameter you need to check the parameter list:

 pointcut permissionCheckMethods() : execution(public * *(..)); before() throws AuthorizationException : permissionCheckMethods() { for (Object arg : thisJoinPoint.getArgs()) { if (arg instanceof EhealthSession) check(arg, thisJoinPointStaticPart.getSignature()); } } 

PS: Perhaps you can narrow the focus through within(SomeBaseClass+) or within(*Postfix) or within(com.company.package..*) , so as not to apply recommendations for the entire universe.

+3
source share

You should use .. (double dots) at the end and beginning as follows:

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

Also get rid of && args(*, sess) because this means that you expect to catch only those methods of any type for the first parameter, but sess as the second parameter and no more than 2 parameters.

+2
source share

All Articles