AspectJ MethodSignature 为 getParameterNames() 返回 null

发布于 2024-12-27 03:07:47 字数 748 浏览 2 评论 0原文

我有一个方面,它根据目标方法的详细信息进行各种计算,因此预先提取这些信息,如下所示:

    @Around("execution(* com.xyz.service.AccountService.*(..))")
public void validateParams(ProceedingJoinPoint joinPoint) throws Throwable {
    final MethodSignature signature = (MethodSignature) joinPoint.getSignature();

    final String methodName = signature.getName();
    final String[] parameterNames = signature.getParameterNames();
    final Object[] arguments = joinPoint.getArgs();
    ...
    ...
    ...
    joinPoint.proceed();
}

在提取的详细信息中,除了始终返回 null 的parameterNames 之外,所有详细信息都反映了预期信息。我希望它按照下面的签名返回 {accountDetails} 。有人知道我可能缺少什么,还是这是一个错误?

这是我正在处理的目标方法的签名:

Long createAccount(RequestAccountDetails accountDetails);

I have an aspect that does various computations based on the target method's details, and therefore extracts these upfront as follows:

    @Around("execution(* com.xyz.service.AccountService.*(..))")
public void validateParams(ProceedingJoinPoint joinPoint) throws Throwable {
    final MethodSignature signature = (MethodSignature) joinPoint.getSignature();

    final String methodName = signature.getName();
    final String[] parameterNames = signature.getParameterNames();
    final Object[] arguments = joinPoint.getArgs();
    ...
    ...
    ...
    joinPoint.proceed();
}

Of the extracted details, all reflect the expected info except parameterNames which always returns null. I would expect it to return {accountDetails} as per the signature below. Would anyone know what I may be missing, or is this a bug?

Here's the signature of the target method I am working against:

Long createAccount(RequestAccountDetails accountDetails);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

开始看清了 2025-01-03 03:07:47

对我有用:

@Aspect
public class MyAspect {

    @Around("execution(* *(..)) && !within(MyAspect)")
    public Object validateParams(ProceedingJoinPoint joinPoint) throws Throwable {
        final MethodSignature signature = (MethodSignature) joinPoint.getSignature();

        final String[] parameterNames = signature.getParameterNames();
        for (String string : parameterNames) {
            System.out.println("paramName: " + string);
        }

        return joinPoint.proceed();

    }
}

输出: paramName: accountDetails

我已将 validateParams 的签名更改为:
public Object validateParams(ProceedingJoinPoint joinPoint) 会抛出 Throwable,因为 createAccount() 返回一个 Long。否则,我会收到错误:申请不返回 void 的连接点:{0}

works for me:

@Aspect
public class MyAspect {

    @Around("execution(* *(..)) && !within(MyAspect)")
    public Object validateParams(ProceedingJoinPoint joinPoint) throws Throwable {
        final MethodSignature signature = (MethodSignature) joinPoint.getSignature();

        final String[] parameterNames = signature.getParameterNames();
        for (String string : parameterNames) {
            System.out.println("paramName: " + string);
        }

        return joinPoint.proceed();

    }
}

output: paramName: accountDetails

I have changed the signature of validateParams to:
public Object validateParams(ProceedingJoinPoint joinPoint) throws Throwable because createAccount() returns a Long. Otherwise I get the error: applying to join point that doesnt return void: {0}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文