rayman
rayman

Reputation: 21596

Creating aspect the retrieves object from method and return another object

I am trying to write an aspect (in Spring) which taking the input params from a method in my package do some manipulation and return to that method a result.

Is that possible?

For example:

public MyClass {

 Public void execute (Object object)
  {
     //doSomeLogic with the returned object from the aspect
  }
}

@Aspect
public class ExecutionAspect {




@Before(// any idea?)
        public void getArgument(JoinPoint joinPoint) {


         Object[] signatureArgs = joinPoint.getArgs();
         for (Object signatureArg: signatureArgs) {
             MyObject myObject=(MyObject)signatureArg;
             //do some manipulation on myObject
}
                  //Now how do I return the object to the intercepted method?


    }

Thanks, ray.

Upvotes: 2

Views: 7917

Answers (1)

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 298838

If you want to change the return value, you will have to use an @Around advice.

@Aspect
public class AroundExample {

  @Around("some.pointcut()")
  public Object doSomeStuff(ProceedingJoinPoint pjp) throws Throwable {

    Object[] args = joinPoint.getArgs(); // change the args if you want to
    Object retVal = pjp.proceed(args); // run the actual method (or don't)
    return retVal; // return the return value (or something else)
  }

}

The mechanism is described here: Spring Reference > AOP > Around Advice

Upvotes: 10

Related Questions