2016-03-10 3 views
0

Я изучаю Java и как-то попадал в AspectJ. Я попытался выполнить этот код из учебника:advexecution() не работает в 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

Какая ошибка? он даже компилируется? вставьте в свой стек трассировку, возможно, – r4ccoon

+0

Не компилируется. Он помещен в сообщение –

ответ

2

Я не вижу объявления типа для аспекта, содержащего pointcutcingcutcutcutcut? Я подозреваю, что вы отбросили этот совет в свой аспект 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) 
Смежные вопросы