Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointcut expression for argument in any position

Tags:

java

aspectj

@Before(value = "@annotation(OwnershipCheck) && args(enquiry)")
public void checkOwnership(Enquiry enquiry) throws Throwable
{
}

The above expression will match for any method with the OwnershipCheck annotation and takes an enquiry as a parameter.

How can I extend this expression for any method with the OwnershipCheck annotation and takes an enquiry in any position with or without another parameters.

That is, needs to match

@OwnershipCheck    
public void one(Enquiry enquiry)

@OwnershipCheck
public void two(Object obj, Enquiry enquiry)

@OwnershipCheck
public void three(Enquiry enquiry, Object ob)

@OwnershipCheck
public void four(Object obj, Enquiry enquiry, Object other)
like image 988
dom farr Avatar asked Nov 05 '22 00:11

dom farr


1 Answers

Here is how I did this:

@Pointcut("@annotation(protections) && args(request,..)")
private void httpRequestAsFirstParam(Protections protections, HttpServletRequest request){}

@Pointcut("@annotation(protections) && args(..,request)")
private void httpRequestAsLastParam(Protections protections, HttpServletRequest request){}

@Pointcut("@annotation(protections) && args(*,request,..)")
private void httpRequestAsSecondParam(Protections protections, HttpServletRequest request){}

@Around("httpRequestAsFirstParam(protections, request) " +
        "|| httpRequestAsLastParam(protections, request) " +
        "|| httpRequestAsSecondParam(protections, request)")
public Object protect(ProceedingJoinPoint joinPoint, Protections protections, HttpServletRequest request) throws Throwable {
    //...
}

You can't do args(.., enquiry, ..), but it is possible with args(*, enquiry, ..) and combine with first and last argument matchers.

like image 119
Bahadır Yağan Avatar answered Nov 15 '22 01:11

Bahadır Yağan