1

With Spring AOP, I am writing an Aspect with an around advice that intercepts any method annotated with @MyAnnotation. Suppose the intercepted method was declared as follows,

@MyAnnotation
public String someMethod(ArrayList<Integer> arrList) {...}

In the advice method, I want to get infomation that the first parameter, i.e. arrList, has the type ArrayList with the type parameter Integer.

I have tried following this question by doing this

@Around("@annotation(MyAnnotation)")
public Object advice(ProceedingJoinPoint pjp) {
    Method method = ((MethodSignature) pjp.getSignature()).getMethod();
    Class<?>[] paramsClass = method.getParameterTypes();
    logger.info(((ParameterizedType) paramsClass[0].getGenericSuperclass()).getActualTypeArguments()[0])
    ...
}

But what was logged out is just an E. How shoud I do to so that it prints java.lang.Integer?

Community
  • 1
  • 1
asinkxcoswt
  • 2,252
  • 5
  • 29
  • 57
  • 1
    See this comment: http://stackoverflow.com/questions/19860393/java-generics-obtaining-actual-type-of-generic-parameter#19860597 – Andrei Stefan Apr 22 '14 at 07:22

1 Answers1

1

Try this:

@Around("@annotation(MyAnnotation)")
public Object advice(ProceedingJoinPoint pjp) throws Throwable {
    Method method = ((MethodSignature) pjp.getSignature()).getMethod();
    Type[] genericParamsClass = method.getGenericParameterTypes();

    logger.info(((ParameterizedType) genericParamsClass[0]).getActualTypeArguments()[0]);
    ...
}
Rahul garg
  • 9,202
  • 5
  • 34
  • 67
Andrei Stefan
  • 51,654
  • 6
  • 98
  • 89
  • If you have time, please explain what exactly the `method.getGenericParameterTypes()` returns. I have tried to find out what is the different between `class` and `type` and as far as I understand, `type` is the generalization of `class`, and other `type` are `interface`, `array` and `primitive`. But I can't figure out what kind of `type` returned by `method.getGenericParameterTypes()` that can have the information that the generic parameter is `java.lang.Integer`. Thank you. – asinkxcoswt Apr 22 '14 at 08:21
  • 1
    http://stackoverflow.com/questions/6747383/difference-between-getgenericparametertypes-and-getparametertypes – Andrei Stefan Apr 22 '14 at 08:39