說明
本節(jié)結(jié)合《aspectj cookbook》第4章進(jìn)行學(xué)習(xí),利用官網(wǎng)以及demo學(xué)習(xí)
連接點(diǎn)有多種凌外,列舉如下
http://www.eclipse.org/aspectj/doc/released/progguide/quick.html#quick-pointcuts
這里以方法為切點(diǎn)
捕獲方法的調(diào)用
形式:call(MethodPattern)
定義:When a method is called, not including super calls of non-static methods.
作用:在方法調(diào)用時觸發(fā)涕侈,環(huán)境是調(diào)用類
MethodPattern定義見后面?zhèn)渥⑿菜铮纠缦?/p>
demo
MyClass.java
package aspectj;
public class MyClass {
public void foo() {
System.out.println("foo");
}
public static void main(String[] args) {
MyClass myClass = new MyClass();
myClass.foo();
}
}
HelloWorld.aj
package aspectj;
public aspect HelloWorld {
pointcut callPointcut():call(void aspectj.MyClass.foo());
before(): callPointcut()
{
// System.out.println("Entering: " + thisJoinPoint.getSourceLocation());
System.out.println("hello");
}
}
結(jié)果
hello
foo
說明:call調(diào)用依賴于方法目標(biāo)的動靜態(tài)類型陪拘,這里不展開
捕獲方法調(diào)用上傳遞的參數(shù)值
結(jié)合args完成綁定
形式:args(Type or Id, ...)
定義: Picks out each join point where the arguments are instances of the appropriate type (or type of the identifier if using that form). A null argument is matched iff the static type of the argument (declared parameter type or field type) is the same as, or a subtype of, the specified args type.
作用: 從join point獲取參數(shù)渴庆,提供給pointcut
demo
package aspectj;
public aspect HelloWorld {
//pointcut定義參數(shù)類型以及名字,args定義參數(shù)名,和pointcut參數(shù)名匹配
pointcut callPointcut(int x):call(void aspectj.MyClass.foo(int)) && args(x);
//advice需要定義參數(shù)類型以及參數(shù)名,和后面的pointcut的參數(shù)名匹配
before(int y): callPointcut(y)
{
System.out.println("hello " + y);
}
}
package aspectj;
public class MyClass {
public void foo(int x) {
System.out.println("foo " + x);
}
public static void main(String[] args) {
MyClass myClass = new MyClass();
myClass.foo(3);
}
}
結(jié)果
hello 3
foo 3
捕獲方法調(diào)用的目標(biāo)
也就是說捕獲是哪個對象(實(shí)例雅镊,instance)調(diào)用了作為join point的方法
結(jié)合tartge完成綁定
形式:target(Type or Id, ...)
定義: Picks out each join point where the target object (the object on which a call or field operation is applied to) is an instance of Type, or of the type of the identifier Id (which must be bound in the enclosing advice or pointcut definition). Will not match any calls, gets, or sets of static members.
作用: 從join point獲取調(diào)用的目標(biāo)對象襟雷,提供給pointcut進(jìn)行綁定
demo
package aspectj;
public class MyClass {
public void foo(int x) {
System.out.println("foo " + x);
}
public static void main(String[] args) {
MyClass myClass = new MyClass();
System.out.println(myClass);
myClass.foo(3);
}
}
package aspectj;
public aspect HelloWorld {
//pointcut定義參數(shù)類型以及名字,args定義參數(shù)名,和pointcut參數(shù)名匹配
pointcut callPointcut(int x, MyClass y):call(void aspectj.MyClass.foo(int)) && args(x) && target(y);
//advice需要定義參數(shù)類型以及參數(shù)名,和后面的pointcut的參數(shù)名匹配
before(int x, MyClass y): callPointcut(x,y)
{
System.out.println("hello " + x + " " + y);
}
}
結(jié)果
aspectj.MyClass@85ede7b
hello 3 aspectj.MyClass@85ede7b
foo 3
執(zhí)行一個方法時捕獲它
形式 execution(MethodPattern)
定義: Picks out each method execution join point whose signature matches MethodPattern.
作用:在方法執(zhí)行時觸發(fā)
備注:觸發(fā)的環(huán)境是在目標(biāo)類方法中(call的環(huán)境是調(diào)用類)
和call的區(qū)別就是仁烹,call是調(diào)用耸弄,是在方法外面(調(diào)用前后),execution是執(zhí)行,在方法里面(執(zhí)行前后)
demo
package aspectj;
public aspect HelloWorld {
//pointcut定義參數(shù)類型以及名字,args定義參數(shù)名卓缰,和pointcut參數(shù)名匹配
pointcut callPointcut(int x, MyClass y):call(void aspectj.MyClass.foo(int)) && args(x) && target(y);
//advice需要定義參數(shù)類型以及參數(shù)名,和后面的pointcut的參數(shù)名匹配
before(int x, MyClass y): callPointcut(x,y)
{
System.out.println("before call ");
System.out.println(thisJoinPoint.getSourceLocation());
}
pointcut exePointcut(int x, MyClass y):execution(void aspectj.MyClass.foo(int)) && args(x) && target(y);
before(int x, MyClass y): exePointcut(x,y)
{
System.out.println("before execution ");
System.out.println(thisJoinPoint.getSourceLocation());
}
}
輸出
aspectj.MyClass@723279cf
before call
MyClass.java:10
before execution
MyClass.java:4
foo 3
在執(zhí)行方法時捕獲this引用的值
形式:this(Type or Id)
定義:Picks out each join point where the currently executing object (the object bound to this) is an instance of Type, or of the type of the identifier Id (which must be bound in the enclosing advice or pointcut definition). Will not match any join points from static contexts.
作用:執(zhí)行期間(僅限execution),獲取this引用指向的對象
demo
package aspectj;
public aspect HelloWorld {
pointcut exePointcut(int x, MyClass y):execution(void aspectj.MyClass.foo(int)) && args(x) && this(y);
before(int x, MyClass y): exePointcut(x,y)
{
System.out.println("before execution " + y);
System.out.println(thisJoinPoint.getSourceLocation());
}
}
輸出
aspectj.MyClass@723279cf
before execution aspectj.MyClass@723279cf
MyClass.java:4
foo 3
備注
Pattern形式的定義
https://eclipse.org/aspectj/doc/released/progguide/semantics-pointcuts.html
MethodPattern =
[ModifiersPattern] TypePattern
[TypePattern . ] IdPattern (TypePattern | ".." , ... )
[ throws ThrowsPattern ]
ConstructorPattern =
[ModifiersPattern ]
[TypePattern . ] new (TypePattern | ".." , ...)
[ throws ThrowsPattern ]
FieldPattern =
[ModifiersPattern] TypePattern [TypePattern . ] IdPattern
ThrowsPattern =
[ ! ] TypePattern , ...
TypePattern =
IdPattern [ + ] [ [] ... ]
| ! TypePattern
| TypePattern && TypePattern
| TypePattern || TypePattern
| ( TypePattern )
IdPattern =
Sequence of characters, possibly with special * and .. wildcards
ModifiersPattern =
[ ! ] JavaModifier ...
不同signature需要的參數(shù)
https://eclipse.org/aspectj/doc/released/adk15notebook/join-point-signatures.html
環(huán)境說明
https://eclipse.org/aspectj/doc/released/progguide/semantics-joinPoints.html
問題
this和target環(huán)境的區(qū)分的意義
refer
語法說明 https://eclipse.org/aspectj/doc/released/progguide/semantics.html
《aspectj cookbook》