Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aspectj overwrite an argument of a method

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

like image 772
eglobetrotter Avatar asked Nov 30 '10 09:11

eglobetrotter


People also ask

What is Pointcut in AspectJ?

AspectJ provides primitive pointcuts that capture join points at these times. These pointcuts use the dynamic types of their objects to pick out join points. They may also be used to expose the objects used for discrimination. this(Type or Id) target(Type or Id)

Is AspectJ slow?

No doubt about it: AspectJ won't make your programs faster. It adds an additional amount of overhead to your program. Every aspect is a piece of code that has to be executed, consuming time along the way.

What is the use of AspectJ in spring?

Aspect: An aspect is a class that implements enterprise application concerns that cut across multiple classes, such as transaction management. Aspects can be a normal class configured through Spring XML configuration or we can use Spring AspectJ integration to define a class as Aspect using @Aspect annotation.


1 Answers

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);
like image 77
Ralph Avatar answered Oct 06 '22 00:10

Ralph