pointcut支持布尔表达式 &&,||,! 以及 匿名,重用
捕获何时连接点上的运行时条件为true
if(BooleanExpression)
Picks out each join point where the boolean expression evaluates to true. The boolean expression used can only access static members, parameters exposed by the enclosing pointcut or advice, and thisJoinPoint forms. In particular, it cannot call non-static methods on the aspect or use return values or exceptions exposed by after advice.
切入点结合复合表达式
即&& || !
结合上面的if demo如下
package aspectj;
public aspect HelloWorld {
//可以定义变量
private static int value = 0;
//!withincode 用来去除MyClass.get()里面的所有joinPoint
//!execution 用来去除MyClass.get() 执行时的这个joinPoint
pointcut ifPoint(): if((thisJoinPoint.getThis() instanceof MyClass) &&
((MyClass)thisJoinPoint.getThis()).get() > value) &&
!withincode(* MyClass.get()) &&
!execution(* MyClass.get());
after() : ifPoint() && !within(HelloWorld){
System.out.println(thisJoinPoint.getSignature());
System.out.println(thisJoinPoint.getSourceLocation());
System.out.println(thisJoinPoint.getKind());
System.out.println();
}
}
package aspectj;
public class MyClass {
public void doSth(int x){
}
public int get() {
return 3;
}
public static void main(String[] args) {
MyClass myClass = new MyClass();
myClass.doSth(5);
}
}
输出
aspectj.MyClass()
MyClass.java:3
constructor-execution
aspectj.MyClass()
MyClass.java:3
initialization
void aspectj.MyClass.doSth(int)
MyClass.java:4
method-execution
声明匿名切入点
每一个切入点的声明都用到了匿名切入点,也可以直接用在通知声明上
比如上例中
if((thisJoinPoint.getThis() instanceof MyClass) &&((MyClass)thisJoinPoint.getThis()).get() > value)
!withincode(* MyClass.get())
!execution(* MyClass.get());
!within(HelloWorld)
这里面每一个都可叫匿名切入点
重用切入点
就是声明了切入点之后,在其他地方引用该pointcut
比如上例中
pointcut ifPoint():xxxx(省略) 这里是声明切入点
after() : ifPoint() && !within(HelloWorld) 这里重用了切入点 ifPoint()
思考
demo中withincode和execution的区别
execution只是一个连接点,即函数执行的那一个
withincode则是函数执行里面所有连接点,比如调用其他方法,获取field
refer
https://eclipse.org/aspectj/doc/released/progguide/semantics-pointcuts.html
《aspectj cookbook》