java 面向方面的编程 - 什么是“cflow”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5205916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Aspect oriented programming - what is 'cflow'?
提问by Joeblackdev
I have referred to the AspectJ reference hereit states that the 'cflow' is
我在这里提到了 AspectJ 参考,它指出“cflow”是
cflow(Pointcut)
- every join point in the control flow of each join point P picked out by Pointcut, including P itself
cflow(Pointcut)
- Pointcut 选取的每个连接点 P 的控制流中的每个连接点,包括 P 本身
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 的含义?为什么要使用它?
Thanks indeed.
确实谢谢。
回答by Maxym
cflow helps you to advice the whole control flow. Let's try an example, I have 4 small classes
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() {
}
}
my aspect:
我的方面:
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();
}
}
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:
在我的切入点中你可以看到cflow(call( * B.methodB() ) )
,所以我想从B.methodB
调用开始对控制流进行方面,当你运行测试类时,你会在控制台上看到:
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
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.
最后一个字符串不属于aspect,只是因为System.out.println
inside methodB
。所有打印的显示您控制流 - 方法链和“事件”(执行、调用、初始化...)。你看,我从Test
类开始,它调用了methodA
但它们不在“堆栈”中,因为我们对methodB
控制流感兴趣。
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);
cflowbelow is another pointcut, which means control flow excluding specified (in our case calling B.methodB
).
cflowbelow 是另一个切入点,这意味着不包括指定的控制流(在我们的例子中调用B.methodB
)。
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...)
小心添加!within(_aspect_)
切入点,否则除了StackOverflowError
. 发生这种情况是因为无法在编译时定义 cflow,并且在运行时方面也属于控制流(因此它导致了永恒的递归......)
well, think of control flow as similar to call stack, then you will get an idea of its usage ;)
好吧,把控制流想象成类似于调用堆栈,然后你就会了解它的用法;)