java Aspectj 覆盖方法的参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4312224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:48:21  来源:igfitidea点击:

Aspectj overwrite an argument of a method

javaaopaspectjspring-aop

提问by eglobetrotter

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

我正在开发一个方面,它检查 setter 方法的参数并用空值覆盖空字符串。到目前为止,这是我的状态:

@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?

不幸的是,overwrite 语句args[i] = null;现在可以工作了!有谁知道我应该如何覆盖它?

Cheers,

干杯,

Kevin

凯文

回答by Ralph

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

我相信你必须实现一个 around 建议,而不是一个 before 建议。

Because you can use proceed with your new arguments:

因为您可以使用继续您的新参数:

proceed(newArgs);