我引用了 AspectJ 引用 here它指出“cflow”是
cflow(Pointcut)
- every join point in the control flow of each join point P picked out by Pointcut, including P itself
这对我来说并不完全清楚,我想知道是否有人可以详细说明 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
调用开始控制流,当您运行测试类时,您会在控制台上看到:
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
最后一个字符串不属于方面,只是因为methodB
里面的System.out.println
。所有打印的都显示您控制流程 - 方法链和“事件”(执行、调用、初始化......)。你看,我从 Test
类开始,它调用 methodA
但它们不在“堆栈”中,因为我们对 methodB
控制流感兴趣.
如果您想获得该堆栈,但没有第一行(调用自身),您可以尝试定义
public pointcut flow() : cflowbelow(call( * B.methodB() ) ) && !within(CFlow);
cflowbelow 是另一个切入点,表示不包括指定的控制流(在我们的例子中调用B.methodB
)。
注意在切入点中添加!within(_aspect_)
,否则除了StackOverflowError
,你什么也得不到。发生这种情况是因为 cflow 不能在编译时定义,并且在运行时方面也属于控制流(所以它会导致永远递归......)
好吧,把控制流想象成调用栈,然后你就会明白它的用法;)
关于java - 面向方面的编程 - 什么是 'cflow' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5205916/