2008-11-05 24 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