Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

volidator不同的类不能复用吗 #22

Closed
12266601032 opened this issue Aug 22, 2017 · 9 comments
Closed

volidator不同的类不能复用吗 #22

12266601032 opened this issue Aug 22, 2017 · 9 comments

Comments

@12266601032
Copy link
Contributor

image

@neoremind
Copy link
Owner

是可以复用的,这里仅仅是为了做一个单例的Validator,保证全局有且仅有一个该类型的Validator。这需要开发人员保证Validator是thread-safe的。

@12266601032
Copy link
Contributor Author

CLASS_2_ANNOTATION_VALIDATOR_MAP是根据待验证类的class缓存的该类里的所有validator 因为上面那个问题导致 CLASS_2_ANNOTATION_VALIDATOR_MAP 最后存的validators是空的,然后在执行doValidate时 导致执行不到@FluentValidate指定的验证类

@neoremind
Copy link
Owner

可以贴出来复现问题的代码吗?

@12266601032
Copy link
Contributor Author

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) {
		
	}
	
}

}

@12266601032
Copy link
Contributor Author

在验证bcar时 就不执行NameValidator了

@12266601032
Copy link
Contributor Author

image

@neoremind
Copy link
Owner

I will look into the issue later.

@neoremind
Copy link
Owner

neoremind commented Aug 22, 2017

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!

@neoremind
Copy link
Owner

Fixed. So close the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants