面向方面的编程 - 什么是“cflow”?

2023-12-27

我参考了AspectJ参考here http://www.eclipse.org/aspectj/doc/released/progguide/quick.html它指出“cflow”是

cflow(Pointcut)- 每个连接点 每个连接点P的控制流程 通过Pointcut挑选出来,包括P 本身

这对我来说并不完全清楚,我想知道是否有人可以详细说明 cflow 的含义?为什么要使用它?

确实谢谢。


cflow 可帮助您为整个控制流程提供建议。让我们尝试一个例子,我有 4 个小班

public class A {

    public static void methodA() {
        B.methodB();
    }

}

public class B {

    public static void methodB() {
        C.methodC();
        int a = 1;
        int b = 2;
        System.out.println( a + b );
    }

}

public class C {

    public static void methodC() {
        D.methodD();
    }

}

public class D {

    public static void methodD() {

    }

}

我的方面:

public aspect CFlow {

    public pointcut flow() : cflow(call( * B.methodB() ) ) && !within(CFlow);

    before() : flow() {
        System.out.println( thisJoinPoint );
    }

}

和我的跑步者(只是为了看看会发生什么):

public class Test {

    public static void main(String[] args) {
        A.methodA();
    }

}

在我的切入点中你可以看到cflow(call( * B.methodB() ) ),所以我想从以下方面开始控制流程B.methodB调用,当您运行 Test 类时,您会在控制台上看到:

call(void test.B.methodB())
staticinitialization(test.B.<clinit>)
execution(void test.B.methodB())
call(void test.C.methodC())
staticinitialization(test.C.<clinit>)
execution(void test.C.methodC())
call(void test.D.methodD())
staticinitialization(test.D.<clinit>)
execution(void test.D.methodD())
get(PrintStream java.lang.System.out)
call(void java.io.PrintStream.println(int))
3

最后一个字符串不属于方面,只是因为System.out.println inside methodB。所有打印的内容都显示您控制流程 - 方法链和“事件”(执行、调用、初始化...)。你看,我从Test类,称为methodA但它们不在“堆栈”中,因为我们感兴趣的是methodB控制流。

如果您想获取该堆栈,但没有第一行(调用自身),您可以尝试定义

public pointcut flow() :  cflowbelow(call( * B.methodB() ) ) && !within(CFlow);

cflowbelow 是另一个切入点,这意味着控制流不包括指定的(在我们的例子中调用B.methodB).

小心添加!within(_aspect_)在切入点中,否则你不会得到任何好处,但是StackOverflowError。发生这种情况是因为 cflow 无法在编译时定义,并且在运行时方面也属于控制流(因此它导致永恒递归......)

好吧,将控制流视为类似于调用堆栈,然后您就会了解其用法;)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

面向方面的编程 - 什么是“cflow”? 的相关文章

随机推荐