Aspectjが引数を使用してメソッド引数をバインドするようにしたいと思います。
このようなもの:
@Before("@annotation(authorized) && args(java.util.String)")
public void authorize(JoinPoint joinPoint, Authorized authorized, String str)
ただし、String引数が存在することは期待できません。String引数を持つメソッドだけでなく、そのアノテーションを使用するすべてのメソッドにアドバイスを適用したいと思います。
アドバイスされたメソッドにString引数がない場合はstr
、null値を入力したいと思います。これは可能ですか?または、使用する唯一のオプションjoinPoint.getArgs()
ですか?
アンディの答えへのコメントであなたが尋ねた質問に対する答えがあります:
引数の量が不明であるが、特定のタイプの引数で終わらないメソッドにアドバイスすることは可能でしょうか?
package de.scrum_master.app;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface Authorized {}
package de.scrum_master.app;
public class Application {
@Authorized static void bla(String string, int i, int j) {}
@Authorized static void baz(String string, int i, Integer integer) {}
@Authorized static void zot(String string) {}
@Authorized static void bar(Integer integer) {}
@Authorized static void foo() {}
public static void main(String[] args) {
foo();
bar(new Integer(11));
zot("xxx");
baz("yyy", 123, new Integer(22));
bla("zzz", 123, 456);
}
}
package de.scrum_master.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import de.scrum_master.app.Authorized;
@Aspect
public class MyAspect {
@Before("@annotation(authorized) && execution(* *(..)) && !execution(* *(.., Integer))")
public void authorize(JoinPoint joinPoint, Authorized authorized) {
System.out.println(joinPoint);
}
}
コンソール出力:
execution(void de.scrum_master.app.Application.foo())
execution(void de.scrum_master.app.Application.zot(String))
execution(void de.scrum_master.app.Application.bla(String, int, int))
あなたが見ることができるように、二つの方法baz
およびbar
ない特定のタイプで終わる-Integer
この例では、 -マッチング対象から除外されています。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加