Getting a return value or exception from AspectJ?

后端 未结 3 1588
眼角桃花
眼角桃花 2021-02-19 04:10

I am able to get the signature and arguments from advised method calls, but I cannot figure out how to get the return values or exceptions. I\'m kind of assuming that it can be

3条回答
  •  遥遥无期
    2021-02-19 04:14

    Using an around() advice, you can get the return value of the intercepted method call by using proceed(). You can even change the value returned by the method if you want to.

    For instance, suppose you have a method m() inside class MyClass:

    public class MyClass {
      int m() {
        return 2;
      }
    }
    

    Suppose you have the following aspect in its own .aj file:

    public aspect mAspect {
       pointcut mexec() : execution(* m(..));
    
       int around() : mexec() {    
         // use proceed() to do the computation of the original method
         int original_return_value = proceed();
    
         // change the return value of m()
         return original_return_value * 100;
       }
    }
    

提交回复
热议问题