package com;
import java.awt.Checkbox;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
import java.lang.reflect.Field;
import java.security.KeyStore.PrivateKeyEntry;
//基于注解形式的校验规则
public class Damo7 {
public static void main(String[] args) {
Xman x=new Xman(100,"小红","三天三夜");
x.setContent(null);
x.setGender(1);
BeanVaildator.check(x);
}
}
//注解做代码层面的简单配置
@Target(ElementType.FIELD)//作用于成员变量
@interface Rule{
int max() default 0;
int min() default 0;
//信息描述
String value() default "";
//能否为空
boolean isNull() default true;
}
//bean的校验规则
class BeanVaildator{
public static <T> void check(T t) {
//拿到t中的每一个属性
Class clazz=t.getClass();
Field[] fs=clazz.getDeclaredFields();
for(Field field:fs) {
//强制访问私有属性
field.setAccessible(true);
try {
Object obj=field.get(t);
//如果满足rule可以为空,就可以为空,否则不能为空
Rule rule=field.getAnnotation(Rule.class);
if (rule!=null) {
//校验规则的规范
int max=rule.max();
int min=rule.min();
String valueAnn=rule.value();
boolean isNull=rule.isNull();
//取出实际值
Object value=field.get(t);
if (isNull) {
if (value==null) {
continue;
}
}else {
//如果不能为空的情况下为空l
if (value==null) throw new MyParamException(valueAnn+"该变量不能为空");
}
//是Integer 还是String
if (value instanceof String) {
int strLength=((String)value).length();
if(strLength<min || strLength>max) {
throw new MyParamException(valueAnn+"字符串长度限制出问题");
}
}else if(value instanceof Integer){
if((Integer)value<min||(Integer)value>max){
throw new MyParamException(valueAnn+"值大小越界");
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//设置一个自定义异常
class MyParamException extends RuntimeException{
private String message;
private Exception e;
public MyParamException(String message) {
super(message);
this.message=message;
}
public MyParamException(Exception e) {
super(e);
this.e=e;
}
public MyParamException() {
this.message="参数异常";
}
}
class Xman{
//年龄不超过200;
@Rule(min=1,max=200,value="年龄")
private Integer age;
//名字不超五个字
@Rule(min=0,max=5,value="名字")
private String name;
//不能为空
@Rule(isNull=true,value="能力描述",min=0,max=2000)
private String content;//能力描述
@Rule(isNull=false,value="性别",min=1,max=2)
private Integer gender=0;
public Xman() {
super();
}
public Xman(Integer age, String name, String content) {
super();
this.age = age;
this.name = name;
this.content = content;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return "Xman [age=" + age + ", name=" + name + ", content=" + content + "]";
}
}
class ClassRoom{
private String name;
private Integer id;
private Integer size;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
@Override
public String toString() {
return "ClassRoom [name=" + name + ", id=" + id + ", size=" + size + "]";
}
class School {
private Integer id;
private String name;
private Integer age;
@Override
public String toString() {
return "School [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
java 基于注解形式的校验规则
最新推荐文章于 2024-07-12 18:15:20 发布