1
1
package restx .annotations .processor ;
2
2
3
3
import com .google .common .base .CaseFormat ;
4
+ import com .google .common .base .Function ;
4
5
import com .google .common .base .Joiner ;
5
6
import com .google .common .base .Optional ;
6
7
import com .google .common .collect .*;
14
15
import restx .http .HttpStatus ;
15
16
import restx .security .PermitAll ;
16
17
import restx .security .RolesAllowed ;
18
+ import restx .validation .ValidatedFor ;
17
19
18
20
import javax .annotation .processing .RoundEnvironment ;
19
21
import javax .annotation .processing .SupportedAnnotationTypes ;
38
40
public class RestxAnnotationProcessor extends RestxAbstractProcessor {
39
41
final Template routerTpl ;
40
42
43
+ private static final Function <Class ,String > FQN_EXTRACTOR = new Function <Class ,String >(){
44
+ @ Override
45
+ public String apply (Class clazz ) {
46
+ return clazz .getCanonicalName ();
47
+ }
48
+ };
49
+
41
50
public RestxAnnotationProcessor () {
42
51
routerTpl = Mustaches .compile (RestxAnnotationProcessor .class , "RestxRouter.mustache" );
43
52
}
@@ -163,11 +172,19 @@ private void buildResourceMethodParams(ResourceMethodAnnotation annotation, Reso
163
172
}
164
173
}
165
174
175
+ ValidatedFor validatedFor = p .getAnnotation (ValidatedFor .class );
176
+
177
+ String [] validationGroups = new String [0 ];
178
+ if (validatedFor != null ) {
179
+ validationGroups = Annotations .getAnnotationClassValuesAsFQCN (p , ValidatedFor .class , "value" );
180
+ }
181
+
166
182
resourceMethod .parameters .add (new ResourceMethodParameter (
167
183
p .asType ().toString (),
168
184
paramName ,
169
185
reqParamName ,
170
- parameterKind ));
186
+ parameterKind ,
187
+ validationGroups ));
171
188
}
172
189
if (!pathParamNamesToMatch .isEmpty ()) {
173
190
error (
@@ -492,8 +509,16 @@ private static class ResourceMethodParameter {
492
509
final String name ;
493
510
final String reqParamName ;
494
511
final ResourceMethodParameterKind kind ;
512
+ final List <String > validationGroupsFQNs ;
513
+
514
+ private static final Function <String , String > CLASS_APPENDER_FCT = new Function <String , String >() {
515
+ @ Override
516
+ public String apply (String fqn ) {
517
+ return fqn +".class" ;
518
+ }
519
+ };
495
520
496
- private ResourceMethodParameter (String type , String name , String reqParamName , ResourceMethodParameterKind kind ) {
521
+ private ResourceMethodParameter (String type , String name , String reqParamName , ResourceMethodParameterKind kind , String [] validationGroupsFQNs ) {
497
522
Matcher guavaOptionalMatcher = guavaOptionalPattern .matcher (type );
498
523
Matcher java8OptionalMatcher = java8OptionalPattern .matcher (type );
499
524
this .realType = type ;
@@ -513,6 +538,15 @@ private ResourceMethodParameter(String type, String name, String reqParamName, R
513
538
this .name = name ;
514
539
this .reqParamName = reqParamName ;
515
540
this .kind = kind ;
541
+ this .validationGroupsFQNs = Arrays .asList (validationGroupsFQNs );
542
+ }
543
+
544
+ public Optional <String > joinedValidationGroupFQNExpression (){
545
+ if (this .validationGroupsFQNs == null || this .validationGroupsFQNs .isEmpty ()) {
546
+ return Optional .absent ();
547
+ } else {
548
+ return Optional .of (Joiner .on (", " ).join (Iterables .transform (this .validationGroupsFQNs , CLASS_APPENDER_FCT )));
549
+ }
516
550
}
517
551
}
518
552
@@ -539,7 +573,8 @@ public String fetchFromReqCode(ResourceMethodParameter parameter) {
539
573
},
540
574
BODY {
541
575
public String fetchFromReqCode (ResourceMethodParameter parameter ) {
542
- return String .format ("checkValid(validator, body)" , parameter .type );
576
+ Optional <String > validationGroupsExpr = parameter .joinedValidationGroupFQNExpression ();
577
+ return String .format ("checkValid(validator, body%s)" , validationGroupsExpr .isPresent ()?"," +validationGroupsExpr .get ():"" );
543
578
}
544
579
},
545
580
CONTEXT {
0 commit comments