AspectJ: parameter in a pointcut

后端 未结 5 1228
说谎
说谎 2021-01-01 01:54

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(Sessi         


        
5条回答
  •  天涯浪人
    2021-01-01 02:59

    I cannot extend AspectJ syntax for you, but I can offer a workaround. But first let me explain why it is not possible to do what you want with an args definition in a pointcut: because if you would match your EhealthSession parameter anyplace within the method signature, how should AspectJ handle the case that the signature contains multiple parameters of that class? The meaning of eheSess would be ambiguous.

    Now the workaround: It might be slower - how much depends on your environment, just test it - but you could just have the pointcut match all potential methods regardless of their parameter list and then let the advice find the parameter you need by inspecting the parameter list:

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

    P.S.: Maybe you can narrow the focus via within(SomeBaseClass+) or within(*Postfix) or within(com.company.package..*) so as not to apply the advice to the whole universe.

提交回复
热议问题