Search in sources :

Example 1 with Validator

use of com.dfire.validator.annotation.Validator in project Hotchpotch by carryxyh.

the class Check method validator.

/**
 * 拦截所有使用Validator注解的接口,并进行参数合法有效性验证
 * @param joinPoint
 * @return
 * @throws Throwable
 */
@Around("@annotation(com.dfire.validator.annotation.Validator)")
public Object validator(ProceedingJoinPoint joinPoint) throws Throwable {
    Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
    Object[] objects = joinPoint.getArgs();
    LocalVariableTableParameterNameDiscoverer u = new LocalVariableTableParameterNameDiscoverer();
    // 获得接口实现类的所有方法
    Method[] methods = joinPoint.getTarget().getClass().getDeclaredMethods();
    Map<String, Integer> paramMap = new HashMap<>();
    for (Method method1 : methods) {
        // 判断接口名与参数是否一致
        if (StringUtils.equals(method.getName(), method1.getName()) && method.getParameterTypes().length == method1.getParameterTypes().length && checkMethod(method, method1)) {
            String[] params = u.getParameterNames(method1);
            for (int i = 0; i < params.length; i++) {
                paramMap.put(params[i], i);
            }
            break;
        }
    }
    // 取得需要验证的注解对象
    Validator interfaceValidator = method.getAnnotation(Validator.class);
    // 取得明细验证规则
    Check[] params = interfaceValidator.value();
    int index = 0;
    for (int i = 0; i < params.length; i++) {
        Check p = params[i];
        Integer idx = paramMap.get(p.name());
        index = idx != null ? idx : i;
        // 如未指定验证参数名字,且当前规则所在索引大于参数个数时,报出异常
        if (index >= objects.length) {
            throw new Exception("未指定name或name错误 的注解索引已 大于 参数个数,请检查!注:name需与实现参数名一致!");
        }
        // 验证参数对象
        Object o = objects[index];
        Class<? extends ValidatorAdapter> vda = p.adapter();
        // 如未指定适配器,则默认使用oval验证对象
        if (vda.getName().equals(ValidatorAdapter.class.getName())) {
            if (o != null) {
                // 当验证对象不为null,使用oval验证框架验证
                net.sf.oval.Validator validator = new net.sf.oval.Validator();
                List<ConstraintViolation> ret = validator.validate(o);
                if (ret != null && ret.size() > 0) {
                    return processResult(p.name(), ret.get(0).getErrorCode(), ret.get(0).getMessage(), method.getReturnType());
                }
            }
        } else {
            ValidatorAdapter adapter = (ValidatorAdapter) Class.forName(vda.getName()).newInstance();
            // 根据注解相应设置的条件调用相应的验证方法
            if ((p.v().length == 0 && !adapter.validate(o)) || (p.v().length == 1 && !adapter.validate(o, p.v()[0])) || (p.v().length == 2 && !adapter.validate(o, p.v()[0], p.v()[1]))) {
                String message = StringUtils.isBlank(p.message()) ? adapter.getMessage() : p.message();
                return processResult(p.name(), p.errorCode(), message, method.getReturnType());
            }
        }
    }
    // 调用目标方法
    return joinPoint.proceed();
}
Also used : LocalVariableTableParameterNameDiscoverer(org.springframework.core.LocalVariableTableParameterNameDiscoverer) ValidatorAdapter(com.dfire.validator.validator.abstracts.ValidatorAdapter) MethodSignature(org.aspectj.lang.reflect.MethodSignature) HashMap(java.util.HashMap) Check(com.dfire.validator.annotation.Check) Method(java.lang.reflect.Method) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) ValidatorException(com.dfire.validator.exception.ValidatorException) ConstraintViolation(net.sf.oval.ConstraintViolation) Validator(com.dfire.validator.annotation.Validator) Around(org.aspectj.lang.annotation.Around)

Aggregations

Check (com.dfire.validator.annotation.Check)1 Validator (com.dfire.validator.annotation.Validator)1 ValidatorException (com.dfire.validator.exception.ValidatorException)1 ValidatorAdapter (com.dfire.validator.validator.abstracts.ValidatorAdapter)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 ConstraintViolation (net.sf.oval.ConstraintViolation)1 ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)1 Around (org.aspectj.lang.annotation.Around)1 MethodSignature (org.aspectj.lang.reflect.MethodSignature)1 LocalVariableTableParameterNameDiscoverer (org.springframework.core.LocalVariableTableParameterNameDiscoverer)1