/**
 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.jboot.components.valid.interceptor;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.kit.Ret;
import io.jboot.components.valid.ValidUtil;
import io.jboot.utils.ClassUtil;

import javax.validation.constraints.Size;
import java.lang.reflect.Parameter;
import java.util.Collection;
import java.util.Map;


public class SizeInterceptor implements Interceptor {

    @Override
    public void intercept(Invocation inv) {
        Parameter[] parameters = inv.getMethod().getParameters();

        for (int index = 0; index < parameters.length; index++) {
            Size size = parameters[index].getAnnotation(Size.class);
            if (size != null) {
                Object validObject = inv.getArg(index);
                if (validObject == null) {
                    String reason = parameters[index].getName() + " need size is " + size.min() + " ~ " + size.max()
                            + ", but current value is null at method: " + ClassUtil.buildMethodString(inv.getMethod());
                    Ret paras = Ret.by("max", size.max()).set("min", size.min());
                    ValidUtil.throwValidException(parameters[index].getName(), size.message(), paras, reason);
                    return;
                }

                int len = getObjectLen(validObject);
                if (len < size.min() || len > size.max()) {
                    String reason = parameters[index].getName() + " need size is " + size.min() + " ~ " + size.max()
                            + ", but current value size (or length) is " + len + " at method: " + ClassUtil.buildMethodString(inv.getMethod());
                    Ret paras = Ret.by("max", size.max()).set("min", size.min());
                    ValidUtil.throwValidException(parameters[index].getName(), size.message(), paras, reason);
                }
            }
        }

        inv.invoke();
    }

    private int getObjectLen(Object validObject) {
        if (validObject instanceof Number) {
            return ((Number) validObject).intValue();
        }
        if (validObject instanceof CharSequence) {
            return ((CharSequence) validObject).length();
        }
        if (validObject instanceof Map) {
            return ((Map<?, ?>) validObject).size();
        }
        if (validObject instanceof Collection) {
            return ((Collection) validObject).size();
        }
        if (validObject.getClass().isArray()) {
            return ((Object[]) validObject).length;
        }

        return -1;
    }


}
@@ -854,16 +854,47 @@
 assUtil;
+%0Aimport io.jboot.utils.StrUtil;
 %0A%0Aimport
@@ -1358,9 +1358,9 @@
 ize 
-!
+=
 = nu
@@ -1361,32 +1361,69 @@
  == null) %7B%0A    
+            continue;%0A            %7D%0A%0A
             Obje
@@ -1458,16 +1458,18 @@
 index);%0A
+%0A 
         
@@ -1463,32 +1463,256 @@
 );%0A%0A            
+//%E4%B8%8D%E6%8C%87%E5%AE%9A @Size(min=xxx)%EF%BC%8C%E5%80%BC%E9%85%8D%E7%BD%AE%E4%BA%86 max%EF%BC%8C%E5%88%99%E8%B7%B3%E8%BF%87%E7%A9%BA%E6%95%B0%E6%8D%AE%E7%9A%84%E5%86%85%E5%AE%B9%0A            if (size.min() == 0 && (validObject == null %7C%7C (validObject instanceof String && StrUtil.isBlank((String) validObject)))) %7B%0A                continue;%0A            %7D%0A%0A       
      if (validOb
@@ -1719,36 +1719,32 @@
 ject == null) %7B%0A
-    
                 
@@ -1856,36 +1856,32 @@
                 
-  
-  
 + %22, but current
@@ -1964,36 +1964,32 @@
                 
-    
 Ret paras = Ret.
@@ -2026,36 +2026,32 @@
 %22, size.min());%0A
-    
                 
@@ -2153,28 +2153,24 @@
             
-    
 return;%0A    
@@ -2169,36 +2169,32 @@
 rn;%0A            
-    
 %7D%0A%0A             
@@ -2192,20 +2192,16 @@
         
-  
-  
 int len 
@@ -2229,20 +2229,16 @@
 bject);%0A
-    
         
@@ -2281,28 +2281,24 @@
 ze.max()) %7B%0A
-    
             
@@ -2414,36 +2414,32 @@
                 
-    
 + %22, but current
@@ -2546,36 +2546,32 @@
                 
-    
 Ret paras = Ret.
@@ -2624,36 +2624,32 @@
                 
-  
-  
 ValidUtil.throwV
@@ -2727,26 +2727,8 @@
 n);%0A
-                %7D%0A
     
@@ -2773,24 +2773,25 @@
 e();%0A    %7D%0A%0A
+%0A
     private 

修复 注解 @Size(max=xxx) 拦截错误的问题