15

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?

1 Answer 1

41

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

Signature signature =  proceedingJoinPoint.getSignature();
Class returnType = ((MethodSignature) signature).getReturnType();
2
  • 2
    Thanks! It works! But I decided to apply this by mybatis interceptor.
    – zhuguowei
    Commented Jan 23, 2015 at 13:27
  • Thanx a million Commented Dec 12, 2019 at 11:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.