面向方面的编程-什么是"cflow"? [英] Aspect oriented programming - what is 'cflow'?

查看:164
本文介绍了面向方面的编程-什么是"cflow"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在此处中提到了AspectJ参考. "cflow"是

I have referred to the AspectJ reference here it states that the 'cflow' is

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

cflow(Pointcut) - every join point in the control flow of each join point P picked out by Pointcut, including P itself

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

This isn't entirely lucid for me and I was wondering if someone could elaborate a little more on the meaning of cflow please? Why use it?

的确如此.

推荐答案

cflow可帮助您建议整个控制流程.让我们尝试一个例子,我有4个小类

cflow helps you to advice the whole control flow. Let's try an example, I have 4 small classes

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 );
    }

}

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

and my runner (just to see what happens):

public class Test {

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

}

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

in my pointcut you could see cflow(call( * B.methodB() ) ), so I want to aspect control flow starting from B.methodB calling, and when you run Test class you see on console:

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

最后一个字符串不属于aspect,仅是因为methodB中的System.out.println.所有显示的内容都显示了控制流程的方法链-方法和事件"链(执行,调用,初始化...).您看,我从Test类开始,该类称为methodA,但是它们不在'stack'中,因为我们对methodB控制流感兴趣.

last string does not belong to aspect, it is just because of System.out.println inside methodB. All printed shows you control flow - chains of methods and 'events' (execution, calling, initializations...). You see, I started from Test class, which called methodA but they are not in 'stack', because we were interested in methodB control flow.

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

If you want to get that stack, but without first line (calling itself), you could try to define

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

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

cflowbelow is another pointcut, which means control flow excluding specified (in our case calling B.methodB).

请注意在切入点中添加!within(_aspect_),否则,除了StackOverflowError之外,您将一无所获.发生这种情况是因为无法在编译时定义cflow,并且在运行时方面也属于控制流(因此导致永恒的递归...)

Be careful to add !within(_aspect_) in pointcut, otherwise you will get nothing good but StackOverflowError. It happens because cflow can't be defined at compile time, and at runtime aspect belongs to control flow too (so it leads to eternal recursion...)

好吧,认为控制流类似于调用堆栈,那么您将了解其用法;)

well, think of control flow as similar to call stack, then you will get an idea of its usage ;)

这篇关于面向方面的编程-什么是"cflow"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆