aspectj支持對連接點(diǎn)類型的判斷,包括this(當(dāng)前對象),可選目標(biāo)引用target(被調(diào)用對象),以及指定參數(shù)類型
捕獲何時(shí)this引用是一個(gè)特定的類型
即連接點(diǎn)處的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.
后面例子針對this(Type)不包括this(Id)
demo在下面
捕獲何時(shí)連接點(diǎn)的目標(biāo)對象是特定的類型
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.
this 和 target 區(qū)別(個(gè)人理解)
this是連接點(diǎn)處的當(dāng)前對象(即調(diào)用的對象)
target是連接點(diǎn)處的目標(biāo)對象(即被調(diào)用的對象)
比如連接點(diǎn)是A.func()中,里面調(diào)用B.func();
此時(shí)this就是A當(dāng)前對象a扣唱,
target就是B當(dāng)前對象b.
上面this和target的demo
package aspectj;
public class MyClass {
public void doSth(){
}
public static void main(String[] args) {
MyClass myClass = new MyClass();
myClass.doSth();
}
}
package aspectj;
public class Main {
public static void main(String[] args) {
MyClass myClass = new MyClass();
myClass.doSth();
}
}
package aspectj;
public aspect HelloWorld {
pointcut thisPoint(MyClass myClass): this(myClass);
before(MyClass myClass) : thisPoint(myClass) {
System.out.println(thisJoinPoint.getSignature());
System.out.println(thisJoinPoint.getSourceLocation());
System.out.println("thisPoint " + thisJoinPoint.getKind());
System.out.println();
}
pointcut targetPoint(MyClass myClass): target(myClass);
before(MyClass myClass) : targetPoint(myClass) {
System.out.println(thisJoinPoint.getSignature());
System.out.println(thisJoinPoint.getSourceLocation());
System.out.println("targetPoint " + thisJoinPoint.getKind());
System.out.println();
}
}
輸出
aspectj.MyClass()
MyClass.java:3
thisPoint initialization
aspectj.MyClass()
MyClass.java:3
targetPoint initialization
aspectj.MyClass()
MyClass.java:3
thisPoint constructor-execution
aspectj.MyClass()
MyClass.java:3
targetPoint constructor-execution
void aspectj.MyClass.doSth()
Main.java:6
targetPoint method-call
void aspectj.MyClass.doSth()
MyClass.java:4
thisPoint method-execution
void aspectj.MyClass.doSth()
MyClass.java:4
targetPoint method-execution
還是比較好理解的
捕獲何時(shí)連接點(diǎn)的參數(shù)是某個(gè)數(shù)字黍氮,類型和次序
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.
demo
package aspectj;
public class Main {
public static void main(String[] args) {
MyClass myClass = new MyClass();
test(myClass);
}
public static void test(MyClass myClass) {
myClass.doSth();
}
}
package aspectj;
public class MyClass {
public void doSth(){
}
}
package aspectj;
public aspect HelloWorld {
pointcut argPoint(MyClass myClass): args(myClass);
before(MyClass myClass) : argPoint(myClass) && !within(HelloWorld){
System.out.println(thisJoinPoint.getSignature());
System.out.println(thisJoinPoint.getSourceLocation());
System.out.println("argPoint " + thisJoinPoint.getKind());
System.out.println();
}
}
輸出
void aspectj.Main.test(MyClass)
Main.java:6
argPoint method-call
void aspectj.Main.test(MyClass)
Main.java:9
argPoint method-execution
思考
區(qū)分this和target區(qū)別
參數(shù)為基本類型在前面講過如args(int)還能獲取傳int參數(shù)值
refer
https://eclipse.org/aspectj/doc/released/progguide/semantics-pointcuts.html
《aspectj cookbook》