File tree Expand file tree Collapse file tree 3 files changed +24
-6
lines changed
main/java/org/springframework/validation
test/java/org/springframework/validation/beanvalidation Expand file tree Collapse file tree 3 files changed +24
-6
lines changed Original file line number Diff line number Diff line change 1
1
/*
2
- * Copyright 2002-2011 the original author or authors.
2
+ * Copyright 2002-2014 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
35
35
* class is supposed to be validated at the method level (acting as a pointcut
36
36
* for the corresponding validation interceptor), but also optionally specifying
37
37
* the validation groups for method-level validation in the annotated class.
38
- * Can also be used as a meta-annotation on a custom stereotype annotation.
38
+ * Applying this annotation at the method level allows for overriding the
39
+ * validation groups for a specific method but does not serve as a pointcut;
40
+ * a class-level annotation is nevertheless necessary to trigger method validation
41
+ * for a specific bean to begin with. Can also be used as a meta-annotation on a
42
+ * custom stereotype annotation or a custom group-specific validated annotation.
39
43
*
40
44
* @author Juergen Hoeller
41
45
* @since 3.1
44
48
* @see org.springframework.validation.beanvalidation.SpringValidatorAdapter
45
49
* @see org.springframework.validation.beanvalidation.MethodValidationPostProcessor
46
50
*/
47
- @ Target ({ElementType .TYPE , ElementType .PARAMETER })
51
+ @ Target ({ElementType .TYPE , ElementType .METHOD , ElementType . PARAMETER })
48
52
@ Retention (RetentionPolicy .RUNTIME )
49
53
@ Documented
50
54
public @interface Validated {
Original file line number Diff line number Diff line change @@ -141,8 +141,11 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
141
141
* @return the applicable validation groups as a Class array
142
142
*/
143
143
protected Class <?>[] determineValidationGroups (MethodInvocation invocation ) {
144
- Validated valid = AnnotationUtils .findAnnotation (invocation .getThis ().getClass (), Validated .class );
145
- return (valid != null ? valid .value () : new Class <?>[0 ]);
144
+ Validated validatedAnn = AnnotationUtils .findAnnotation (invocation .getMethod (), Validated .class );
145
+ if (validatedAnn == null ) {
146
+ validatedAnn = AnnotationUtils .findAnnotation (invocation .getThis ().getClass (), Validated .class );
147
+ }
148
+ return (validatedAnn != null ? validatedAnn .value () : new Class <?>[0 ]);
146
149
}
147
150
148
151
Original file line number Diff line number Diff line change @@ -124,17 +124,28 @@ public interface MyValidInterface {
124
124
125
125
@ NotNull Object myValidMethod (@ NotNull (groups = MyGroup .class ) String arg1 , @ Max (10 ) int arg2 );
126
126
127
- @ Async void myValidAsyncMethod (@ NotNull (groups = MyGroup .class ) String arg1 , @ Max (10 ) int arg2 );
127
+ @ MyValid
128
+ @ Async void myValidAsyncMethod (@ NotNull (groups = OtherGroup .class ) String arg1 , @ Max (10 ) int arg2 );
128
129
}
129
130
130
131
131
132
public interface MyGroup {
132
133
}
133
134
134
135
136
+ public interface OtherGroup {
137
+ }
138
+
139
+
135
140
@ Validated ({MyGroup .class , Default .class })
136
141
@ Retention (RetentionPolicy .RUNTIME )
137
142
public @interface MyStereotype {
138
143
}
139
144
145
+
146
+ @ Validated ({OtherGroup .class , Default .class })
147
+ @ Retention (RetentionPolicy .RUNTIME )
148
+ public @interface MyValid {
149
+ }
150
+
140
151
}
You can’t perform that action at this time.
0 commit comments