6

私はAspectJを使用して、選択したクラスの引数を持つすべてのパブリックメソッドにアドバイスしています。私は次のことを試しました:

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

これは、少なくとも 2 つの引数を持つメソッドに対して素晴らしく機能します。

public void delete(Object item, Session currentSession);

ただし、次のような方法では機能しません。

public List listAll(Session currentSession);

ポイントカットを変更して、両方のメソッドの実行をアドバイスするにはどうすればよいですか? 言い換えれば、「..」ワイルドカードは「0 個以上の引数」を表すと思っていましたが、代わりに「1 個以上」を意味するようです...

4

5 に答える 5

8

まあ...私はこの厄介なトリックでそれを回避しました。誰かが「公式の」ポイントカット定義を提示するのをまだ待っています。

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);
}
于 2008-11-26T11:37:26.930 に答える
4

どうですか:

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

最後の(または唯一の)引数がSession型の場合、これは一致すると思います。argsの位置を入れ替えることで、first-or-onlyに一致させることもできます。しかし、任意の位置に一致させることが可能かどうかはわかりません。

于 2009-06-10T09:06:42.637 に答える
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..*)

于 2012-08-09T11:43:42.120 に答える