Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a return value or exception from AspectJ?

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 done in some way using around and proceed.

like image 743
Dallas Avatar asked Apr 13 '11 22:04

Dallas


People also ask

Which type of advice allows to access the return value of JoinPoint?

After (finally) advice: Advice to be executed regardless of the means by which a join point exits (normal or exceptional return). Around advice: Advice that surrounds a join point such as a method invocation. This is the most powerful kind of advice.

Is AfterReturning advice executed after a JoinPoint completes normally?

AspectJ @AfterReturning advice is executed after a join point completes normally, for example, if the method returns without throwing an exception.

What is advice in AspectJ?

AspectJ supports three kinds of advice. The kind of advice determines how it interacts with the join points it is defined over. Thus AspectJ divides advice into that which runs before its join points, that which runs after its join points, and that which runs in place of (or "around") its join points.

What is JoinPoint AspectJ?

JoinPoint is an AspectJ interface that provides reflective access to the state available at a given join point, like method parameters, return value, or thrown exception. It also provides all static information about the method itself.


2 Answers

You can use after() returning and after() throwing advices as in beginning of the following document. If you're using @AspectJ syntax please refer to @AfterReturning and @AfterThrowing annotations (you can find samples here).

like image 148
Constantiner Avatar answered Sep 21 '22 07:09

Constantiner


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

    }
like image 41
MADHAIYAN M Avatar answered Sep 20 '22 07:09

MADHAIYAN M