aspectj支持对连接点类型的判断,包括this(当前对象),可选目标引用target(被调用对象),以及指定参数类型
捕获何时this引用是一个特定的类型
即连接点处的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在下面
捕获何时连接点的目标对象是特定的类型
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 区别(个人理解)
this是连接点处的当前对象(即调用的对象)
target是连接点处的目标对象(即被调用的对象)
比如连接点是A.func()中,里面调用B.func();
此时this就是A当前对象a,
target就是B当前对象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
还是比较好理解的
捕获何时连接点的参数是某个数字,类型和次序
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
思考
区分this和target区别
参数为基本类型在前面讲过如args(int)还能获取传int参数值
refer
https://eclipse.org/aspectj/doc/released/progguide/semantics-pointcuts.html
《aspectj cookbook》