[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] "About execution flow of advices when occur an exception"

Dear All,

 First of all, I would like to thank both Echlin Harmer and Eric Bodden  by the help in last topic.

 Second of all, I have another question for the list and for AspectJ experts.

 In order to illustrate consider the example below:

//The class:

public class C {     

      public void m(int a, int b){

            System. out.println("Method m()");

      }

}


 // The aspect:

public aspect AspectoC {

      before(C current) :

            execution(!static * C.*(..)) &&

            within(C) &&

            this(current){

            boolean x = false ;

            System.out.println( "Code to execute before all non-static method of Class C");

            if (x){

                  throw new Error("error<before>!" );

            }

      }

     

      before(C current) :

            execution(void C.m(int ,int)) &&

            within(C) &&

            this(current){

            System.out.println( "Code to execute before execution of method m(int, int)");

      }

     

      void around (C current) :

            execution(void C.m(int ,int)) &&

            within(C) &&

            this(current){

            proceed(current); // executed after proceed() call

            System.out.println( "Code to execute after execution of method m(int, int)");

      }

     

      after(C current) :

            execution(!static * C.*(..)) &&

            within(C) &&

            this(current){

            boolean x = false ;

            System.out.println( "Code to execute after all non-static method of Class C");

            if (x)

                  throw new Error("error<after>!" );

      }

}


 If we execute this code above, we will have the result: 

Code to execute before all non-static method of Class C

Code to execute before execution of method m(int, int)

Method m()

Code to execute after execution of method m(int, int)

Code to execute after all non-static method of Class C


About this result it is ok!!!

-However, consider in the first advice if we change the boolean value of x to True;



If we execute the code above with this modification, we will have the result:

Code to execute before all non-static method of Class C

Code to execute after all non-static method of Class C

Exception in thread "main" java.lang.Error: error<before>!

      at teste.AspectoC.ajc$before$teste_AspectoC$1$397accb1(AspectoC.aj:13 )

      at teste.C.m(C.java:6)

      at teste.CodigoCliente.main(CodigoCliente.java:14 )



The question is: Why after the first advice threw an error (java.lang.error) and the last advice executed???

-It was supposed to threw an error and stops..



SECOND THING:

  Consider now in the first and last advice if we change the boolean value of x to True in both;


 
If we execute the code above with this another modification, we will have the result:

Code to execute before all non-static method of Class C

Code to execute after all non-static method of Class C

Exception in thread "main" java.lang.Error: error<after>!

      at teste.AspectoC.ajc$after$teste_AspectoC$4$397accb1(AspectoC.aj:42 )

      at teste.C.m(C.java:6)

      at teste.CodigoCliente.main(CodigoCliente.java:14 )


Now we see that the first advice caused the error, and besides executed the first, it executed the last... the wird thing that we can notice is that the error message printed has the sentence of the last advice insted the first (error<before>). So, I dont undestand this execution flow...


Questions:


Thanks, any help!!!







--
--------------------------------------------
Henrique Mostaert
EIG 2005 - Eclipse Innovation Grant - Awards Program
Sun Certified Programmer for Java2 platform 1.4