java注解长度最⼤_java⾃定义注解验证类的属性必填,最⼩、
最⼤值、最⼩最⼤长度...
编写不易,给个赞
⾃定义注解,设置范围和注解⽅法
import java.lang.annotation.*;
@Target({ ElementType.FIELD, ElementType.TYPE })
@Inherited
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface DataLengh
{
int min() default 0;
int max() default 2147483647;
boolean isRequired() default false;
String message() default "字段长度不符合";
}
import java.math.BigDecimal;
import java.util.Date;
public class TestObj
{
@DataLengh(min = 2,max = 5,message = "名称",isRequired = true)
public String name;
@DataLengh(min = 1,max = 120,message = "年龄")
bigdecimal转换为integerpublic Integer age;
@DataLengh(min = 1,max = 120,message = "⾦额",isRequired = true)
public Double money;
@DataLengh(min = 1,max = 120,message = "双精度",isRequired = true)
public BigDecimal decimal;
@DataLengh(min = 1,max = 120,message = "float",isRequired = true)
public float aFloat;
@DataLengh(message = "⽇期",isRequired = true)
public Date date;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public TestObj(String name, Integer age) {
this.name = name;
this.age = age;
}
public TestObj(String name, Integer age, double money, BigDecimal decimal, float aFloat, Date date) { this.name = name;
this.age = age;
< = money;
this.decimal = decimal;
this.aFloat = aFloat;
this.date = date;
}
public TestObj() {
}
}
import del.DataLengh;
import del.TestObj;
import java.lang.annotation.Annotation;
import flect.Field;
import java.math.BigDecimal;
import java.util.Date;
/**
* 校验数据长度的帮助类
*
* @author LGH
*/
public class DataLenCheckHelper {
/**
* 校验数据属性⾄
*
* @param obj
* @throws Exception
*/
@SuppressWarnings("rawtypes")
public static void checkAttributeValueLen(Object obj) throws Exception {
if (null != obj) {
// 得到class
Class cls = Class();
System.out.println("校验对象中参数的数据长度是否符合要求,校验对象:" + Name()); // 得到所有属性
Field[] fields = DeclaredFields();
for (int i = 0; i < fields.length; i++) {// 遍历
try {
// 得到属性
Field field = fields[i];
Annotation[] anns = Annotations();
DataLengh dataLen = null;
for (Annotation ann : anns) {
if (ann instanceof DataLengh)
dataLen = (DataLengh) ann;
}
// 判断该属性是否有校验数据长度的注解
if (null != dataLen) {
// 打开私有访问
field.setAccessible(true);
// 获取属性
String name = Name();
/
/ 获取属性值
Object value = (obj);
// 获取最⼩长度
int minLen = dataLen.min();
// 获取最⼤长度
int maxLen = dataLen.max();
// 获取字段必填
boolean isRequired = dataLen.isRequired();
// 获取字段备注
String message = ssage();
// 数据的长度
String data = null;
// ⼀个个赋值
/**
* 验证必填
*/
if (isRequired) {
if (value == null) {
System.out.print("对象:" + Name() + "中存在不符合条件的参数,参数名:" + name + "(" + message + "),要求必填"); }
if (value == null || value instanceof String) {
data = (String) value;
if (data.length() == 0) {
System.out.print("对象:" + Name() + "中存在不符合条件的参数,参数名:" + name + "(" + message + "), 要求必填"); }
}
}
if (null != value && value instanceof String) {
checkStringLegth(name, value, data, cls, minLen, maxLen, message);
} else if (null != value && value instanceof Integer) {
checkInter(name, value, data, cls, minLen, maxLen, message);
} else if (null != value && value instanceof BigDecimal) {
checkBigDecimal(name, value, data, cls, minLen, maxLen, message);
} else if (null != value && value instanceof Float) {
checkFloat(name, value, data, cls, minLen, maxLen, message);
} else if (null != value && value instanceof Float) {
checkDouble(name, value, data, cls, minLen, maxLen, message);
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
public static void checkStringLegth(String name, Object value, String data, Class cls, int minLen, int maxLen, String message) throws Exception {
int vaLen = 0;
data = (String) value;
vaLen = data.length();
if (minLen != 0) {
if (minLen > vaLen || vaLen > maxLen) {
System.out.print("对象:" + Name() + "中存在不符合条件的参数,参数名:" + name + "(" + message + "),参数值:" + data + ",指定的数据长度范围:(" + minLen + "," + maxLen + ")实际长度:" + vaLen
+ ",不符合条件");
throw new Exception();
}
} else {
if (vaLen > maxLen) {
System.out.print("对象:" + Name() + "中存在不符合条件的参数,参数名:" + name + "(" + message + "),参数值:" + data + ",指定的数据最⼤长度:" + maxLen + ",实际长度:" + vaLen
+ ",不符合条件");
throw new Exception();
}
}
}
public static void checkInter(String name, Object value, String data, Class cls, int minLen, int maxLen, String message) throws Exception {
int vaLen = 0;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。