Closed 12266601032 closed 7 years ago
是可以复用的,这里仅仅是为了做一个单例的Validator,保证全局有且仅有一个该类型的Validator。这需要开发人员保证Validator是thread-safe的。
CLASS_2_ANNOTATION_VALIDATOR_MAP是根据待验证类的class缓存的该类里的所有validator 因为上面那个问题导致 CLASS_2_ANNOTATION_VALIDATOR_MAP 最后存的validators是空的,然后在执行doValidate时 导致执行不到@FluentValidate指定的验证类
可以贴出来复现问题的代码吗?
package com.baidu.unbiz.fluentvalidator;
import org.junit.Test;
import com.baidu.unbiz.fluentvalidator.annotation.FluentValidate;
public class ValidatorTest {
@Test
public void testReuseValidator(){
CarA acar = new CarA();
CarB bcar = new CarB();
FluentValidator.checkAll().on(acar).doValidate().result(ResultCollectors.toSimple());
FluentValidator.checkAll().on(bcar).doValidate().result(ResultCollectors.toSimple());
}
public class CarA{
@FluentValidate({NameValidator.class})
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class CarB{
@FluentValidate({NameValidator.class})
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public static class NameValidator implements Validator<String>{
@Override
public boolean accept(ValidatorContext context, String t) {
return true;
}
@Override
public boolean validate(ValidatorContext context, String t) {
System.out.println("..........");
return true;
}
@Override
public void onException(Exception e, ValidatorContext context, String t) {
}
}
}
在验证bcar时 就不执行NameValidator了
I will look into the issue later.
Great catch! I am enable to reproduce the problem, your solution works, would you mind to create a PR? Or I can update the issue by myself. Thanks for you finding!
Fixed. So close the issue.