我正在学习Java,不知怎的,我进入了AspectJ。我尝试从教程书中执行此代码:
pointcut adviceExecutionPointcut( ) : adviceexecution( );
// Advice declaration
before( ) : adviceExecutionPointcut( )
&& !within(AdviceExecutionRecipe +)
{
System.out.println(
"------------------- Aspect Advice Logic --------------------");
System.out.println("In the advice picked by ExecutionRecipe");
System.out.println(
"Signature: "
+ thisJoinPoint.getStaticPart( ).getSignature( ));
System.out.println(
"Source Line: "
+ thisJoinPoint.getStaticPart( ).getSourceLocation( ));
System.out.println(
"------------------------------------------------------------");
}
}
它以某种方式给了我这样的错误。
Exception in thread "main" java.lang.StackOverflowError
at helloWorld.ajc$before$helloWorld$2$5e65e204(helloWorld.aj)
at helloWorld.ajc$before$helloWorld$2$5e65e204(helloWorld.aj)
at helloWorld.ajc$before$helloWorld$2$5e65e204(helloWorld.aj)
at helloWorld.ajc$before$helloWorld$2$5e65e204(helloWorld.aj)
at helloWorld.ajc$before$helloWorld$2$5e65e204(helloWorld.aj)
作为参考,这是示例代码和建议代码。
public class myClass {
public void foo(int number, String name){
System.out.println("Inside foo(int, String)");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
myClass myObject = new myClass();
myObject.foo(1, "ican");
}
public aspect helloWorld {
public int a;
long startTime;
pointcut callPointcut(int value, String name) :
call(* myClass.foo(int, String)) && args(value,name);
before(int value, String name) : callPointcut(value,name)
{
startTime = System.nanoTime();
System.out.println("Start Time in ns :" + startTime);
}
任何帮助将不胜感激。感谢
答案 0 :(得分:2)
我没有看到包含adviceexecution切入点的方面的类型声明?我怀疑你把这个建议块放到你的helloWorld
方面。您获得递归stackoverflow的原因是因为建议适用于自身。警卫!within(AdviceExecutionRecipe +)
旨在阻止适用于自己的建议。但是,您无法将其从AdviceExecutionRecipe
更改为helloWorld
,因为它不会适用于您的任何建议。所以我会在另一个方面保留该建议块,并将其命名为AdviceExecutionRecipe
。有了这个,它对我有用:
$ ajc -1.8 *.java -showWeaveInfo
Join point 'adviceexecution(void helloWorld.ajc$before$helloWorld$1$68d3c671(int, java.lang.String))' in Type 'helloWorld' (helloWorld.java:9) advised by before advice from 'AdviceExecutionRecipe' (AdviceExecutionRecipe.java:5)
Join point 'method-call(void myClass.foo(int, java.lang.String))' in Type 'myClass' (myClass.java:10) advised by before advice from 'helloWorld' (helloWorld.java:9)
$ java myClass
------------------- Aspect Advice Logic --------------------
In the advice picked by ExecutionRecipe
Signature: void helloWorld.before(int, String)
Source Line: helloWorld.java:9
------------------------------------------------------------
Start Time in ns :216368803494701
Inside foo(int, String)