AspectJ MethodSignature为getParameterNames()返回null

时间:2012-01-14 03:47:12

标签: java reflection aop aspectj

我有一个方面可以根据目标方法的详细信息进行各种计算,因此会提前提取这些内容,如下所示:

    @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);

1 个答案:

答案 0 :(得分:1)

适合我:

@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) throws Throwable因为createAccount()返回Long。否则我收到错误:applying to join point that doesnt return void: {0}