2008-11-05 87 views
6

我使用AspectJ来建议所有公共方法,它们都有一个选定类的参数。我试过如下:AspectJ:切入点中的参数

这是工作奇妙的方法,至少有2个参数:

public void delete(Object item, Session currentSession); 

,但它不喜欢的方法工作:

public List listAll(Session currentSession); 

请问有什么可以改变我的切入点建议两种方法执行?换句话说:我期望“..”通配符表示“零个或多个参数”,但它看起来像是代表“一个或多个”...

回答

7

哦,好吧......我一直在这个讨厌的伎俩。仍在等待某个人出现“官方”切入点定义。

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); 
} 
4

如何:

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

我想,如果最后一个(或唯一的)参数的类型会议将匹配。通过交换参数的位置,您也可以匹配首选或唯一。但我不知道是否可以匹配任意位置。

+0

匹配任何任意位置是不可能的。 – 2017-10-03 20:47:46

3

我无法为您扩展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()); 
    } 
} 

PS:也许你可以通过within(SomeBaseClass+)within(*Postfix)within(com.company.package..*)范围缩小,以便不建议适用于整个宇宙。

2

你必须使用..末(双点),并开始如下:

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

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

+0

@Manrico Corazzi你应该把它标记为解决你的问题。所以其他人不会被其他工作转移 – Iomanip 2018-02-08 17:19:30

相关问题