增加MethodValidated注解的测试用例 及对MethodValidated使用场景和用法的说明注释#784
增加MethodValidated注解的测试用例 及对MethodValidated使用场景和用法的说明注释#784Authorlove merged 4 commits intoapache:masterfrom
Conversation
public void validate(String methodName, Class<?>[] parameterTypes, Object[] arguments) throws Exception {
String methodClassName = clazz.getName() + "$" + toUpperMethoName(methodName);
Class<?> methodClass = null;
try {
methodClass = Class.forName(methodClassName, false, Thread.currentThread().getContextClassLoader());
} catch (ClassNotFoundException e) {
}这里修改之后,下面执行save方法时,会默认校验Save.class分组 @interface Save {
} // 与方法同名接口,首字母大写,用于区分验证场景,如:@NotNull(groups = ValidationService.Save.class),可选参考 #504 |
|
|
||
| @interface Update { | ||
| } // 与方法同名接口,首字母大写,用于区分验证场景,如:@NotNull(groups = ValidationService.Update.class),可选 | ||
|
|
There was a problem hiding this comment.
这里可以添加
@interface RelatedQuery {}| private static final long serialVersionUID = 7158911668568000392L; | ||
|
|
||
| @NotNull(groups = ValidationService.Update.class) | ||
| private Integer id; |
There was a problem hiding this comment.
@NotNull(groups = {ValidationService.Update.class,ValidationService.RelatedQuery.class})
private Integer id;|
我觉得应该是这样的,你现在说的这个方案是读取以方法名为默认的分组来检查,如果我对relatedQuery方法,想用Save的分组来检查,现在的逻辑没法实现。现在每多一个方法,就要多一个分组。如果类的方法多了,那属性上注解的分组也就跟着多了,很不方便。 而且分组我可以提炼出一个公共的名字,多个方法共用。并不一定按我这个实现,但希望考虑的我建议。@Authorlove |
| } | ||
|
|
||
| private void validate(Set<ConstraintViolation<?>> violations, Object arg, Class<?> clazz, Class<?> methodClass) { | ||
| private void validate(Set<ConstraintViolation<?>> violations, Object arg, Class<?> clazz, Class<?> methodClass, Class<?>[] methodClasses ) { |
There was a problem hiding this comment.
这里可以改成
private void validate(Set<ConstraintViolation<?>> violations, Object arg, Class<?>... groups)
| } else if (methodClasses != null) { | ||
| for (int i = 0; i < methodClasses.length; i++) { | ||
| violations.addAll(validator.validate(parameterBean, Default.class, clazz, methodClasses[i])); | ||
| } |
There was a problem hiding this comment.
for循环去掉吧,维护个List<Class<?>> groups,最后groups.toArray(new Class[0])
| * @author: zhangyinyue | ||
| * @Createdate: 2017年10月10日 16:34 | ||
| */ | ||
| @Target({ElementType.METHOD}) |
There was a problem hiding this comment.
在方法层面上注解,本来就是作用在参数上的; 这个注解应该是要跟方法相关的吧,本来意义就是不同方法需要检查的参数分组不一样,应该没必要加上参数上面吧。
|
@zhangyinyue 这个注解灵活一些,可以加进来,你看看我comment的地方,完善下 |
| } | ||
|
|
||
| public void validate(String methodName, Class<?>[] parameterTypes, Object[] arguments) throws Exception { | ||
| List<Class<?>> groups = new ArrayList<>(); |
There was a problem hiding this comment.
Please set your api level to jdk1.6
| * 表示relatedQuery这个方法需要同时检查Save和Update这两个分组 | ||
| * @author: zhangyinyue | ||
| * @Createdate: 2017年10月10日 16:34 | ||
| */ |
There was a problem hiding this comment.
bad java doc, please follow java doc's specification
* 增加MethodValidated注解的测试用例 及对MethodValidated使用场景和用法的说明注释 * 将需要检查的分组维护到List<Class<?>> groups中,包括当前接口类及Default.class两个默认的分组 * 修改接口级别为jdk1.6; 按javadoc规范修改注释
#726 提交太多文件有点乱,关闭 #726,重新提交代码