11package restx .annotations .processor ;
22
33import com .google .common .base .CaseFormat ;
4+ import com .google .common .base .Function ;
45import com .google .common .base .Joiner ;
56import com .google .common .base .Optional ;
67import com .google .common .collect .*;
1415import restx .http .HttpStatus ;
1516import restx .security .PermitAll ;
1617import restx .security .RolesAllowed ;
18+ import restx .validation .ValidatedFor ;
1719
1820import javax .annotation .processing .RoundEnvironment ;
1921import javax .annotation .processing .SupportedAnnotationTypes ;
3840public class RestxAnnotationProcessor extends RestxAbstractProcessor {
3941 final Template routerTpl ;
4042
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+
4150 public RestxAnnotationProcessor () {
4251 routerTpl = Mustaches .compile (RestxAnnotationProcessor .class , "RestxRouter.mustache" );
4352 }
@@ -163,11 +172,19 @@ private void buildResourceMethodParams(ResourceMethodAnnotation annotation, Reso
163172 }
164173 }
165174
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+
166182 resourceMethod .parameters .add (new ResourceMethodParameter (
167183 p .asType ().toString (),
168184 paramName ,
169185 reqParamName ,
170- parameterKind ));
186+ parameterKind ,
187+ validationGroups ));
171188 }
172189 if (!pathParamNamesToMatch .isEmpty ()) {
173190 error (
@@ -492,8 +509,16 @@ private static class ResourceMethodParameter {
492509 final String name ;
493510 final String reqParamName ;
494511 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+ };
495520
496- private ResourceMethodParameter (String type , String name , String reqParamName , ResourceMethodParameterKind kind ) {
521+ private ResourceMethodParameter (String type , String name , String reqParamName , ResourceMethodParameterKind kind , String [] validationGroupsFQNs ) {
497522 Matcher guavaOptionalMatcher = guavaOptionalPattern .matcher (type );
498523 Matcher java8OptionalMatcher = java8OptionalPattern .matcher (type );
499524 this .realType = type ;
@@ -513,6 +538,15 @@ private ResourceMethodParameter(String type, String name, String reqParamName, R
513538 this .name = name ;
514539 this .reqParamName = reqParamName ;
515540 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+ }
516550 }
517551 }
518552
@@ -539,7 +573,8 @@ public String fetchFromReqCode(ResourceMethodParameter parameter) {
539573 },
540574 BODY {
541575 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 ():"" );
543578 }
544579 },
545580 CONTEXT {
0 commit comments