加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

字段校验 冬蒽

发布时间:2020-12-15 02:47:12 所属栏目:Java 来源:网络整理
导读:今天研究一下本公司系统中字段验证的子系统,本博文方便日后回看。 主要的技术,包括自定义注解、反射 自定义注解,如下:最大长度注解 @Target({ ElementType.FIELD })@Retention(RetentionPolicy.RUNTIME)public @interface MaxLength {/** * b方法说明:/b
今天研究一下本公司系统中字段验证的子系统,本博文方便日后回看。

主要的技术,包括自定义注解、反射


自定义注解,如下:最大长度注解
@Target({ ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
public @interface MaxLength {
	/**
	 * <b>方法说明:</b>
	 * <ul>
	 * 最大长度
	 * </ul>
	 * @return 
	 */
	int value();
}


编写实体类,在字段上填加自定义注解
	@MaxLength(value = 12)
	private String id;


创建n个对象,封装到list中,进行遍历,通过单例模式,获取数据验证引擎,执行校验方法。

校验方法:
	/** 数据验证实现类集合 */
	private List<IDataValidate> dataValidateList;

	 /**
	 * <b>方法说明:</b>
	 * <ul>
	 * 执行数据验证
	 * </ul>
	 * @param validateObject 需要验证的数据类对象
	 * @param fieldStartNum 字段起始位
	 * @return 验证失败信息
	 */
	public String validate(Object validateObject,int fieldStartNum) {
		StringBuilder result = new StringBuilder();

		DataValidationMemory memory = DataValidationMemory.getInstance();

		//类名
		Class<?> className = validateObject.getClass();
		//类的注解字段信息
		Map<Class<? extends Annotation>,Map<Field,Annotation>> objectAnnotationFieldMap = memory.getObjectAnnotationFieldMap(className);

		//如果内存中不存在此类的注解字段信息,则初始化
		if (objectAnnotationFieldMap == null) {
			memory.initObjectField(validateObject.getClass(),fieldStartNum);
		}

		if (dataValidateList == null || dataValidateList.size() <= 0) {
			throw new GnntException("注解验证实现类集合为空");
		}

		//循环此类的所有数据验证实现类,执行其注解验证方法
		for (IDataValidate dataValidate : dataValidateList) {
			result.append(dataValidate.validate(validateObject));
		}

		return result.toString();
	}

dataValidateList来源:通过spring.xml注入,通过遍历接口,实现循环注解的校验。
DataValidationMemory:数据验证内存类.

public class DataValidationMemory {
	/** 数据验证内存类 */
	private static DataValidationMemory memory;

	/** 
	 * 数据验证类对象的注解字段集合
	 * 数据类,注解类,字段注解对象
	 */
	private Map<Class<?>,Map<Class<? extends Annotation>,Annotation>>> objectAnnotationFieldMap = new ConcurrentHashMap<Class<?>,Annotation>>>();
	/** 数据验证类对象的字段集合 */
	private Map<Class<?>,List<Field>> objectFieldMap = new ConcurrentHashMap<Class<?>,List<Field>>();


	/**
	 * <b>方法说明:</b>
	 * <ul>
	 * 初始化数据验证类字段
	 * </ul>
	 * @param objectClass 数据验证类
	 * @param fieldStartNum 字段起始位
	 */
	public synchronized void initObjectField(Class<?> objectClass,int fieldStartNum) {
		if (objectClass == null) {
			throw new GnntException("需要数据验证的类为空");
		}
		if (fieldStartNum <= 0) {
			throw new GnntException("字段起始位不能为负值");
		}

		//注解字段信息集合
		Map<Class<? extends Annotation>,Annotation>> fieldAnnotationMap = new HashMap<Class<? extends Annotation>,Annotation>>();
		//字段集合
		List<Field> fieldList = new ArrayList<Field>();

		//类中所有字段信息
		Field[] objectClassFields = objectClass.getDeclaredFields();
		//循环此类所有的字段信息,初始化其注解配置
		for (int i = (fieldStartNum - 1); i < objectClassFields.length; i++) {
			Field field = objectClassFields[i];
			//解决字段是private修饰符无法获取字段信息问题
			if (!field.isAccessible()) {
				field.setAccessible(true);
			}

			//循环所有的注解
			Annotation[] fieldAnnotations = field.getAnnotations();
			if (fieldAnnotations != null) {
				for (Annotation annotation : fieldAnnotations) {
					//字段对应的注解信息集合
					Class<? extends Annotation> annotationType = annotation.annotationType();
					Map<Field,Annotation> annotationMap = fieldAnnotationMap.get(annotationType);
					//如果该注解类不存在,则创建
					if (annotationMap == null) {
						annotationMap = new HashMap<Field,Annotation>();
					}

					if (annotationMap.containsKey(field)) {
						throw new GnntException("同一字段不允许出现重复注解");
					}

					annotationMap.put(field,annotation);
					fieldAnnotationMap.put(annotationType,annotationMap);
				}
				fieldList.add(field);
			}
		}
		objectAnnotationFieldMap.put(objectClass,fieldAnnotationMap);
		objectFieldMap.put(objectClass,fieldList);
	}
}

附件中为objectAnnotationFieldMap的debug结果,数据类,注解类,字段注解对象
objectFieldMap:数据类,字段list

	public StringBuilder validate(Object validateObject) {
		StringBuilder result = new StringBuilder();

		try {
			//获取数据验证类对象的字段集合
			Map<Class<? extends Annotation>,Annotation>> objectAnnotationFieldMap = DataValidationMemory.getInstance().getObjectAnnotationFieldMap(validateObject.getClass());
			Map<Field,Annotation> fieldAnnotationMap = objectAnnotationFieldMap.get(MaxLength.class);
			if (fieldAnnotationMap == null) {
				return result;
			}

			Set<Field> fieldSet = fieldAnnotationMap.keySet();
			//循环该注解的所有字段验证
			for (Field field : fieldSet) {
				try {
					Annotation annotation = fieldAnnotationMap.get(field);
					Object fieldValue = field.get(validateObject);
					if (fieldValue != null) {
						result.append(this.dealValidate(field,annotation,fieldValue.toString()));
					}
				} catch (Exception e) {
					GnntLogUtil.operationLogger.error("选项验证,系统异常",e);
					result.append("字段[").append(field.getName()).append("]验证异常");
				}
			}
		} catch (Exception e) {
			GnntLogUtil.operationLogger.error("选项验证,系统异常",e);
			result.append("注解[").append(MaxLength.class).append("]验证异常");
		}

		return result;
	}





	/**
	 * <b>方法说明:</b>
	 * <ul>
	 * 执行注解验证
	 * </ul>
	 * @param field 字段对象
	 * @param annotation 注解
	 * @param fieldValue 字段值
	 * @return 验证结果
	 */
	private StringBuilder dealValidate(Field field,Annotation annotation,String fieldValue) {
		StringBuilder result = new StringBuilder();

		if (annotation == null) {
			return result;
		}

		int value = ((MaxLength) annotation).value();
		if (value < 0) {
			throw new GnntException("注解值不能为负数");
		}

		if (fieldValue != null && fieldValue.length() > 0) {
			if (fieldValue.length() > value) {
				result.append("字段[").append(field.getName()).append("]值[").append(fieldValue).append("]值超长;");
			}
		}

		return result;
	}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读