Getting a return value or exception from AspectJ?

后端 未结 3 1746
眼角桃花
眼角桃花 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:21

    You can also get return value using after returing advice.

    package com.eos.poc.test;   
    
    public class AOPDemo {
                public static void main(String[] args) {
                    AOPDemo demo = new AOPDemo();
                    String result= demo.append("Eclipse", " aspectJ");
               }
                public String append(String s1, String s2) {
                    System.out.println("Executing append method..");
                    return s1 + s2;
              }
    
    }
    

    The defined aspect for getting return value:

    public aspect DemoAspect {
        pointcut callDemoAspectPointCut():
            call(* com.eos.poc.test.AOPDemo.append(*,*));
    
        after() returning(Object r) :callDemoAspectPointCut(){
            System.out.println("Return value: "+r.toString()); // getting return value
    
        }
    

提交回复
热议问题