我有一个豆类
BeanClass {
<prop1>;
<prop2>;
<prop3>;
//getters and setters
...
}
和验证实用程序:
Validations {
static void notNull(Object value, ValidationResult result) {
if (...) {
...
}
}
static void doesNotExceedMaxLength(Object value, int max, ValidationResult result) {
if (...) {
...
}
}
我想生成一个如下所示的验证器 -
BeanValidator {
ValidationResult validate(BeanClass bean) {
ValidationResult validationResult = new ValidationResult();
//prop1, prop3 are mandatory...
notNull(bean.getProp1(), validationResult);
notNull(bean.getProp3(), validationResult);
//prop2 must not be more than 255 characters in length
doesNotExceedMaxLength(bean.getProp2(), 255, validationResult)
return validationResult;
}
}
哪些字段是强制性的,哪些值不能超过特定数量,将在运行时查看 bean 类来决定。
我如何使用 Byte Buddy 做到这一点?