-
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,8 @@ | |
*/ | ||
package com.alibaba.dubbo.config.validation; | ||
|
||
import com.alibaba.dubbo.validation.MethodValidated; | ||
|
||
import javax.validation.constraints.Min; | ||
import javax.validation.constraints.NotNull; | ||
import javax.validation.constraints.Pattern; | ||
|
@@ -28,16 +30,31 @@ | |
*/ | ||
public interface ValidationService { // 缺省可按服务接口区分验证场景,如:@NotNull(groups = ValidationService.class) | ||
|
||
/** | ||
* 没有加上“@MethodValidated(ValidationService.Save.class)”这句代码时, | ||
* 现在的检查逻辑不会去检验groups = ValidationService.Save.class这个分组 | ||
* | ||
* @param parameter | ||
*/ | ||
@MethodValidated(Save.class) | ||
void save(ValidationParameter parameter); | ||
|
||
void update(ValidationParameter parameter); | ||
|
||
void delete(@Min(1) long id, @NotNull @Size(min = 2, max = 16) @Pattern(regexp = "^[a-zA-Z]+$") String operator); | ||
|
||
/** | ||
* 假设关联查询的时候需要同时传id和email的值。这时需要检查Sava分组和Update分组。 | ||
* @param parameter | ||
*/ | ||
@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 commentThe reason will be displayed to describe this comment to others. Learn more. 这里可以添加 @interface RelatedQuery {} |
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.alibaba.dubbo.validation; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* 方法分组验证注解 | ||
* 使用场景:当调用某个方法时,需要检查多个分组,可以在接口方法上加上该注解 | ||
* 用法 在接口方法上增加注解,如: | ||
* @MethodValidated({ValidationService.Save.class, ValidationService.Update.class}) | ||
* void relatedQuery(ValidationParameter parameter); | ||
* 表示relatedQuery这个方法需要同时检查Save和Update这两个分组 | ||
* @author: zhangyinyue | ||
* @Createdate: 2017年10月10日 16:34 | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. bad java doc, please follow java doc's specification |
||
@Target({ElementType.METHOD}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. 在方法层面上注解,本来就是作用在参数上的; 这个注解应该是要跟方法相关的吧,本来意义就是不同方法需要检查的参数分组不一样,应该没必要加上参数上面吧。 |
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
public @interface MethodValidated { | ||
Class<?>[] value() default {}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
import com.alibaba.dubbo.common.logger.Logger; | ||
import com.alibaba.dubbo.common.logger.LoggerFactory; | ||
import com.alibaba.dubbo.common.utils.ReflectUtils; | ||
import com.alibaba.dubbo.validation.MethodValidated; | ||
import com.alibaba.dubbo.validation.Validator; | ||
|
||
import javassist.ClassPool; | ||
|
@@ -55,9 +56,12 @@ | |
import java.lang.reflect.Array; | ||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Method; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Date; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. Please set your api level to jdk1.6 |
||
String methodClassName = clazz.getName() + "$" + toUpperMethoName(methodName); | ||
Class<?> methodClass = null; | ||
try { | ||
methodClass = Class.forName(methodClassName, false, Thread.currentThread().getContextClassLoader()); | ||
groups.add(methodClass); | ||
} catch (ClassNotFoundException e) { | ||
} | ||
Set<ConstraintViolation<?>> violations = new HashSet<ConstraintViolation<?>>(); | ||
Method method = clazz.getMethod(methodName, parameterTypes); | ||
Class<?>[] methodClasses = null; | ||
if (method.isAnnotationPresent(MethodValidated.class)){ | ||
methodClasses = method.getAnnotation(MethodValidated.class).value(); | ||
groups.addAll(Arrays.asList(methodClasses)); | ||
} | ||
//加入默认分组 | ||
groups.add(0, Default.class); | ||
groups.add(1, clazz); | ||
|
||
//将list转换为数组 | ||
Class<?>[] classgroups = groups.toArray(new Class[0]); | ||
|
||
Object parameterBean = getMethodParameterBean(clazz, method, arguments); | ||
if (parameterBean != null) { | ||
if (methodClass != null) { | ||
violations.addAll(validator.validate(parameterBean, Default.class, clazz, methodClass)); | ||
} else { | ||
violations.addAll(validator.validate(parameterBean, Default.class, clazz)); | ||
} | ||
violations.addAll(validator.validate(parameterBean, classgroups )); | ||
} | ||
|
||
for (Object arg : arguments) { | ||
validate(violations, arg, clazz, methodClass); | ||
validate(violations, arg, classgroups); | ||
} | ||
|
||
if (violations.size() > 0) { | ||
logger.error("Failed to validate service: " + clazz.getName() + ", method: " + methodName + ", cause: " + violations); | ||
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<?>... groups) { | ||
if (arg != null && !isPrimitives(arg.getClass())) { | ||
if (Object[].class.isInstance(arg)) { | ||
for (Object item : (Object[]) arg) { | ||
validate(violations, item, clazz, methodClass); | ||
validate(violations, item, groups); | ||
} | ||
} else if (Collection.class.isInstance(arg)) { | ||
for (Object item : (Collection<?>) arg) { | ||
validate(violations, item, clazz, methodClass); | ||
validate(violations, item, groups); | ||
} | ||
} else if (Map.class.isInstance(arg)) { | ||
for (Map.Entry<?, ?> entry : ((Map<?, ?>) arg).entrySet()) { | ||
validate(violations, entry.getKey(), clazz, methodClass); | ||
validate(violations, entry.getValue(), clazz, methodClass); | ||
validate(violations, entry.getKey(), groups); | ||
validate(violations, entry.getValue(), groups); | ||
} | ||
} else { | ||
if (methodClass != null) { | ||
violations.addAll(validator.validate(arg, Default.class, clazz, methodClass)); | ||
} else { | ||
violations.addAll(validator.validate(arg, Default.class, clazz)); | ||
} | ||
violations.addAll(validator.validate(arg, groups)); | ||
} | ||
} | ||
} | ||
|
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.