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 无法在编译时定义,并且在运行时方面也属于控制流(因此它导致永恒递归......)
好吧,将控制流视为类似于调用堆栈,然后您就会了解其用法;)