6

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);
Michael-7
  • 1,739
  • 14
  • 17
  • 1
    From the looks of it, there's no dependable way (independent of the options set at compile time) of getting parameter names. See [Can I obtain method parameter name using Java reflection?](http://stackoverflow.com/questions/2237803/can-i-obtain-method-parameter-name-using-java-reflection) and [Is there a way to obtain names of method parameters in Java?](http://stackoverflow.com/questions/381502/is-there-a-way-to-obtain-names-of-method-parameters-in-java). So I suspect this may be what's at play here. – Michael-7 Jan 15 '12 at 01:00

1 Answers1

1

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}

Fred
  • 446
  • 3
  • 5
  • 1
    Thanks Fred! From my analyses, it appears there are certain environments / JVM configs where paramNames are available, but it's been a while back now and can't recall the specific details. Thanks for pointing out on the return value, I'd adapted the question from my code and might have left out some bits. – Michael-7 Mar 13 '12 at 16:52