Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 366
zhuguowei Avatar asked Jan 22 '15 15:01

zhuguowei


1 Answers

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

Signature signature =  proceedingJoinPoint.getSignature();
Class returnType = ((MethodSignature) signature).getReturnType();
like image 163
davioooh Avatar answered Oct 02 '22 04:10

davioooh