代码之家  ›  专栏  ›  技术社区  ›  Manrico Corazzi

AspectJ:切入点中的参数

  •  6
  • Manrico Corazzi  · 技术社区  · 17 年前

    我使用AspectJ来建议所有具有所选类参数的公共方法。我尝试了以下方法:

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

    这对于至少有2个参数的方法非常有效:

    public void delete(Object item, Session currentSession);
    

    但它不适用于以下方法:

    public List listAll(Session currentSession);
    

    我如何更改切入点以建议两种方法的执行?换句话说:我原本希望“..”通配符表示“零个或多个参数”,但看起来它的意思是“一个或多个人”。..

    5 回复  |  直到 5 年前
        1
  •  8
  •   Manrico Corazzi    17 年前

    好吧。..我用这个卑鄙的伎俩解决了这个问题。仍在等待有人给出“官方”切入点定义。

    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);
    }
    
        2
  •  4
  •   Tahir Akhtar    17 年前

    怎么样:

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

    如果最后一个(或唯一一个)参数的类型为Session,我想这将匹配。通过交换args的位置,您还可以匹配first或only。但我不知道是否可以匹配任意位置。

        3
  •  3
  •   kriegaex    13 年前

    我无法为您扩展AspectJ语法,但我可以提供一种解决方法。但首先让我解释一下为什么不可能用一个 args 切入点中的定义:因为如果你想匹配你的 EhealthSession 参数在方法签名中的任何位置,AspectJ应该如何处理签名包含该类的多个参数的情况?……的含义 eheSess 这将是模棱两可的。

    现在的解决方法是:它可能会更慢——多少取决于你的环境,只需测试一下——但你可以让切入点匹配所有潜在的方法,而不管它们的参数列表如何,然后让建议通过检查参数列表来找到你需要的参数:

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

    附言:也许你可以通过以下方式缩小焦点 within(SomeBaseClass+) within(*Postfix) within(com.company.package..*) 以免将建议应用于整个宇宙。

        4
  •  3
  •   Iomanip    9 年前

    你必须使用。.(双点)在结尾和开头如下:

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

    也摆脱 && args(*, sess) 因为这意味着您希望只捕获第一个参数具有任何类型的方法,但 sess 作为第二个参数,也不超过2个参数。。

        5
  •  0
  •   Chirag Thakkar    6 年前
    @Before(value = "execution(public * *(.., org.springframework.data.domain.Pageable , ..))")
    private void isMethodPageable () {
        log.info("in a Aspect point cut isPageableParameterAvailable()");
    }