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

增加MethodValidated注解的测试用例 及对MethodValidated使用场景和用法的说明注释 #784

Merged
merged 4 commits into from
Oct 30, 2017

Conversation

zhangyinyue
Copy link
Contributor

#726 提交太多文件有点乱,关闭 #726,重新提交代码

@Authorlove
Copy link
Contributor

Authorlove commented Oct 26, 2017

 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
方法上多个group上的校验,可以在parameter上配置该group实现,我觉得新增一个注解的意义不大。
@zhangyinyue

*/
@MethodValidated({Save.class, Update.class})
void relatedQuery(ValidationParameter parameter);

@interface Save {
} // 与方法同名接口,首字母大写,用于区分验证场景,如:@NotNull(groups = ValidationService.Save.class),可选

@interface Update {
} // 与方法同名接口,首字母大写,用于区分验证场景,如:@NotNull(groups = ValidationService.Update.class),可选

Copy link
Contributor

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;
Copy link
Contributor

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;

@zhangyinyue
Copy link
Contributor Author

zhangyinyue commented Oct 27, 2017

我觉得应该是这样的,你现在说的这个方案是读取以方法名为默认的分组来检查,如果我对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 ) {
Copy link
Contributor

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]));
}
Copy link
Contributor

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})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以考虑支持参数注解

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在方法层面上注解,本来就是作用在参数上的; 这个注解应该是要跟方法相关的吧,本来意义就是不同方法需要检查的参数分组不一样,应该没必要加上参数上面吧。

@Authorlove
Copy link
Contributor

@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<>();
Copy link
Contributor

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
*/
Copy link
Contributor

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

@Authorlove Authorlove merged commit 1420cb4 into apache:master Oct 30, 2017
rolandhe pushed a commit to rolandhe/dubbo that referenced this pull request Sep 9, 2019
* 增加MethodValidated注解的测试用例  及对MethodValidated使用场景和用法的说明注释

* 将需要检查的分组维护到List<Class<?>> groups中,包括当前接口类及Default.class两个默认的分组

* 修改接口级别为jdk1.6; 按javadoc规范修改注释
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

Successfully merging this pull request may close these issues.

2 participants