-
Notifications
You must be signed in to change notification settings - Fork 26.4k
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
增加MethodValidated注解的测试用例 及对MethodValidated使用场景和用法的说明注释 #784
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 |
*/ | ||
@MethodValidated({Save.class, Update.class}) | ||
void relatedQuery(ValidationParameter parameter); | ||
|
||
@interface Save { | ||
} // 与方法同名接口,首字母大写,用于区分验证场景,如:@NotNull(groups = ValidationService.Save.class),可选 | ||
|
||
@interface Update { | ||
} // 与方法同名接口,首字母大写,用于区分验证场景,如:@NotNull(groups = ValidationService.Update.class),可选 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里可以添加
@interface RelatedQuery {}
@@ -34,6 +34,9 @@ | |||
|
|||
private static final long serialVersionUID = 7158911668568000392L; | |||
|
|||
@NotNull(groups = ValidationService.Update.class) | |||
private Integer id; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@NotNull(groups = {ValidationService.Update.class,ValidationService.RelatedQuery.class})
private Integer id;
我觉得应该是这样的,你现在说的这个方案是读取以方法名为默认的分组来检查,如果我对relatedQuery方法,想用Save的分组来检查,现在的逻辑没法实现。现在每多一个方法,就要多一个分组。如果类的方法多了,那属性上注解的分组也就跟着多了,很不方便。 而且分组我可以提炼出一个公共的名字,多个方法共用。并不一定按我这个实现,但希望考虑的我建议。@Authorlove |
throw new ConstraintViolationException("Failed to validate service: " + clazz.getName() + ", method: " + methodName + ", cause: " + violations, violations); | ||
} | ||
} | ||
|
||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里可以改成
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
可以考虑支持参数注解
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
在方法层面上注解,本来就是作用在参数上的; 这个注解应该是要跟方法相关的吧,本来意义就是不同方法需要检查的参数分组不一样,应该没必要加上参数上面吧。
@zhangyinyue 这个注解灵活一些,可以加进来,你看看我comment的地方,完善下 |
@@ -231,51 +235,60 @@ else if (memberValue instanceof ArrayMemberValue) { | |||
} | |||
|
|||
public void validate(String methodName, Class<?>[] parameterTypes, Object[] arguments) throws Exception { | |||
List<Class<?>> groups = new ArrayList<>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bad java doc, please follow java doc's specification
* 增加MethodValidated注解的测试用例 及对MethodValidated使用场景和用法的说明注释 * 将需要检查的分组维护到List<Class<?>> groups中,包括当前接口类及Default.class两个默认的分组 * 修改接口级别为jdk1.6; 按javadoc规范修改注释
#726 提交太多文件有点乱,关闭 #726,重新提交代码