When using spring aop:around, how can I get return type of the pointcut method?

zhuguowei picture zhuguowei · Jan 22, 2015 · Viewed 8.3k times · Source

I have a requirement now, that is when using mybatis(especially those batch execute sql), check parameter first , if the parameter is null or empty , then just return, don't proceed and if the return type is List,eg.

List<User> getByIds(List<Long> idList)

return empty ArrayList, if the return type is void:

 void batchInsert(List<User>)

return null. The purpose is to avoid this situation, eg.

select * from user where id in ()
insert into user(name,email) values ()

but from joinPoint I can't get return type,only can get args.

Object[] args = joinPoint.getArgs();
if(args!=null&&args.length=1){
    if(args[0] instanceof List){
        if(((List) obj).isEmpty()){
                if(returnType.equals("java.util.List"))
                    return new ArrayList();
                else if(returnType.equals("void"))
                    return null;    
    }
}
return joinPoint.proceed();

So how can I get return type in aop:around?

Answer

davioooh picture davioooh · Jan 22, 2015

To get method return type/class from a ProceedingJoinPoint you can do this:

Signature signature =  proceedingJoinPoint.getSignature();
Class returnType = ((MethodSignature) signature).getReturnType();