自定义注解校验


FieldValid

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ ElementType.METHOD, ElementType.TYPE ,ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface FieldValid {
    String value() default "";
    String name() default "";
    boolean required() default true;
    String msg() default "";
    String[] condition() default {};
}

ReflectUtil


import com.sinosoft.application.claim.dto.custom.FieldValid;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.List;

public class ReflectUtil {

    public static  <T> Object getFieldVal(String fieldName, T t) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Class<?> clazz = t.getClass();
        String firstLetter = fieldName.substring(0, 1).toUpperCase();
        String getter = "get" + firstLetter + fieldName.substring(1);
        Method method = clazz.getMethod(getter);
        return method.invoke(t);
    }

    public static  <T,V> Object setFieldVal(String fieldName, T t, Class<V> clazzType, V val) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Class<?> clazz = t.getClass();
        String firstLetter = fieldName.substring(0, 1).toUpperCase();
        String setter = "set" + firstLetter + fieldName.substring(1);
        Method method = clazz.getMethod(setter, clazzType);
        return method.invoke(t, val);
    }

    //private static Character defalutCharacter = new Character('0');
    public static  <T> String  valid( T t, String... conditionStr ) throws IllegalAccessException {
        Class<?> clazz = t.getClass();
        Field[] declaredFields = clazz.getDeclaredFields();
        if(declaredFields == null || declaredFields.length <= 0){
            return null;
        }
        for (Field field : declaredFields) {
            FieldValid fieldValid = field.getAnnotation(FieldValid.class);
            if(fieldValid == null){
                continue;
            }
            boolean required = fieldValid.required();
            String[] condition = fieldValid.condition();
            // can not access a member of class xxx with modifiers "private"
            // Object objVal = field.get(t);

            Object objVal = null;
            try {
                objVal = getFieldVal(field.getName(), t);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            if(required){
                if(objVal == null){
                    return fieldValid.msg();
                }
            }
            if(conditionStr != null && condition.length > 0){
                List<String> condList = Arrays.asList(condition);
                for (String cond : conditionStr) {
                    boolean contains = condList.contains(cond);
                    if(contains && objVal == null) {
                        return fieldValid.msg();
                    }
                }
            }
        }
        return null;
    }


}

使用

 @FieldValid(name = "操作类型",msg = "操作类型[actionType]必传")
    private String actionType;
@FieldValid(name = "姓名",msg = "姓名[clientName]必传" , condition = ENUM_1.RECOGNIZE, required = false)
    private String clientName;    
----
String valid = ReflectUtil.valid(eleDocRequestDto);
 if(valid != null){
     return valid ;
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值