12

I'm developing an aspect that checks arguments of setter methods and overwrites empty strings with null value. This is my state so far:

@Before("execution(* de.foo.entity.*.set*(..)) && args(java.lang.String)")
public void check(final JoinPoint jp) {
    LOGGER.debug(jp.getSignature().toLongString());
    Object[] args = jp.getArgs();
    for (int i = 0; i < args.length; i++) {
        if (args[i] instanceof String && ((String) args[i]).isEmpty()) {
            args[i] = null;
        }
    }
}

Unfortunately the overwrite statement args[i] = null; does now work! Do anyone know how should I overwrite it?

Cheers,

Kevin

eglobetrotter
  • 718
  • 4
  • 10
  • 25

1 Answers1

32

I believe you have to implement an around advice, instead of a before advice.

Because you can use proceed with your new arguments:

proceed(newArgs);
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
Ralph
  • 118,862
  • 56
  • 287
  • 383
  • Thanks, why do I have to impelement an around advice instead of before? Where is the difference excepts that with an around advice I'm able to control execution of a method, catch Exceptions and check return value? – eglobetrotter Nov 30 '10 at 09:42
  • 7
    A before advice gets a copy of the argument array, but it can't modify the original arguments. That's what an around advice is for. Ralph: +1 – Sean Patrick Floyd Nov 30 '10 at 10:01