5、cflow、cflowbelow

控制流
先写个简单的demo
HelloWorld.java

package aspectj;

public class HelloWorld {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("main");
        Teacher teacher=new Teacher();
        teacher.first();
        teacher.second();
    }

}

Teacher.java

package aspectj;

public class Teacher {

    public void first()
    {
        second();
        System.out.println("first");
    }
    public void  second()
    {
        System.out.println("second");
    }

}

你觉得main方法的执行流程是什么样的呢?


image.png

下面用代码演示下

修改HelloAspect.aj

package aspectj;

public aspect HelloAspect {

    pointcut PrintPointCut() : cflow(execution(* aspectj.HelloWorld.main(..)))&&!within(HelloAspect);
    
    before() : PrintPointCut() {
        System.out.println("拦截位置"+thisJoinPoint.getSourceLocation());
    }
}

输出:


image.png

我们把HelloWorld类和Teacher类中的代码行数截出来


image.png

image.png

继续修改下HelloAspect.aj打印出具体的jointpoint
package aspectj;

public aspect HelloAspect {

    pointcut PrintPointCut() : cflow(execution(* aspectj.HelloWorld.main(..)))&&!within(HelloAspect);
    
    before() : PrintPointCut() {
        System.out.println("拦截位置"+thisJoinPoint.getSourceLocation()+"代码"+thisJoinPoint);
    }
}



image.png
cflowbelow

修改下HelloAspect.aj
修改cflow为cflowbelow

package aspectj;

public aspect HelloAspect {

    pointcut PrintPointCut() : cflowbelow(execution(* aspectj.HelloWorld.main(..)))&&!within(HelloAspect);
    
    before() : PrintPointCut() {
        System.out.println("拦截位置"+thisJoinPoint.getSourceLocation()+"代码"+thisJoinPoint);
    }
}






输出:


image.png

cflowbelow捕获程序控制流程内的所有连接点,不包括初始连接点

你可能感兴趣的:(5、cflow、cflowbelow)