Custom annotations to check data validity

Custom annotations to check data validity



package com.midea.common;

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


@Target(ElementType.FIELD)  
@Retention(RetentionPolicy.RUNTIME)  
@Inherited  
@Documented  
public @interface ModelFieldValidity {  
     long  max() default Long.MAX_VALUE;
     long  min() default Long.MIN_VALUE;   
     long  maxLen() default Long.MAX_VALUE;
     long  minLen() default Long.MIN_VALUE;    
     boolean enableNull() default false;
}  





package com.midea.common.Model;

import com.midea.common.ModelFieldValidity;

public class DescriptionModel {
	@ModelFieldValidity(max = 15, min = 0)
	private int age;

	@ModelFieldValidity(max = 15, min = 0, enableNull = true)
	private Integer high;

	@ModelFieldValidity(maxLen = 8, minLen = 1, enableNull = true)
	private String name;

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public int getHigh() {
		return high;
	}

	public void setHigh(int high) {
		this.high = high;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "DescriptionModel [age=" + age + ", high=" + high + ", name=" + name + "]";
	}

}



package com.midea.common;

import java.lang.reflect.Field;

public class ModelFieldValidityUtils {

	/**
	 * @param t
	 * @return Returns null if the verification passes, and returns the reason for the failure if the verification fails
	 */
	public static <T> String checkModelFields(T t) {
		String result = null;
		Field[] fields = t.getClass().getDeclaredFields();
		for (Field field : fields) {
			ModelFieldValidity modelFieldValidity = field.getAnnotation(ModelFieldValidity.class);
			if (null == modelFieldValidity) {
				continue;
			}
			try {
				field.setAccessible(true);
				Object object = field.get(t);

				// Determine if it can be empty
				if (null == object) {
					boolean enableNull = modelFieldValidity.enableNull();
					// System.out.println("enableNull == " + enableNull);
					if (!enableNull) {
						result = field.getName() + " is null";
						return result;
					} else {
						continue;
					}
				}

				if (object instanceof Number) {
					//Number comparison
					Long max = modelFieldValidity.max();
					Long min = modelFieldValidity.min();
					// System.out.println("max == " + max);
					// System.out.println("min == " + min);
					// System.out.println("Number == " + object);
					int check = ModelFieldValidityUtils.check(max, min, (Number) object);
					if (check == 1) {
						result = field.getName() + " = " + object + " > max(" + max + ")";
						return result;
					} else if (check == -1) {
						result = field.getName() + " = " + object + " < min(" + min + ")";
						return result;
					} else {
						continue;
					}
				} else if (object instanceof String) {
					// String comparison
					Long maxLen = modelFieldValidity.maxLen();
					Long minLen = modelFieldValidity.minLen();
					// System.out.println("maxLen == " + maxLen);
					// System.out.println("minLen == " + minLen);
					// System.out.println("String == " + object);
					int check = ModelFieldValidityUtils.check(maxLen, minLen, ((String) object).length());
					if (check == 1) {
						result = field.getName() + " Len = " + ((String) object).length() + " > maxLen(" + maxLen + ")";
						return result;
					} else if (check == -1) {
						result = field.getName() + " Len = " + ((String) object).length() + " < minLen(" + minLen + ")";
						return result;
					} else {
						continue;
					}
				}

			} catch (IllegalArgumentException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
				result = e1.getMessage();
				return result;
			} catch (IllegalAccessException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
				result = e1.getMessage();
				return result;
			}

		}
		return result;
	}

	private static <T extends Number> int check(T max, T min, T object) {
		if (object.doubleValue() > max.doubleValue()) {
			return 1;
		}

		if (object.doubleValue() < min.doubleValue()) {
			return -1;
		}

		return 0;
	}
}


package com.midea.common;

import java.lang.reflect.Field;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.midea.common.Model.DescriptionModel;

public class ModelFieldValidityUtilsTest {
	final static Logger logger = LogManager.getLogger (ModelFieldValidityUtilsTest.class);

	public static void main(String[] args) {
		DescriptionModel descriptionModel = new DescriptionModel();

		descriptionModel.setAge(1);
		descriptionModel.setHigh(1);
		descriptionModel.setName("asa");

		System.out.println("descriptionModel == " + descriptionModel.toString());

		// String result = null;

		String result = ModelFieldValidityUtils.checkModelFields(descriptionModel);

		System.out.println("result == " + result);

	}

}



Refer to the original text: http://blog.csdn.net/maguanghui_2012/article/details/69372209












Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326409620&siteId=291194637