1
1
package io .avaje .validation .generator ;
2
2
3
+ import static io .avaje .validation .generator .Util .trimAnnotations ;
3
4
import static java .util .function .Predicate .not ;
4
5
import static java .util .stream .Collectors .joining ;
5
6
import static java .util .stream .Collectors .toMap ;
19
20
import javax .lang .model .element .TypeElement ;
20
21
import javax .lang .model .element .VariableElement ;
21
22
import javax .lang .model .type .ArrayType ;
23
+ import javax .lang .model .type .TypeMirror ;
22
24
import javax .lang .model .util .ElementFilter ;
23
25
24
26
final class AnnotationUtil {
25
27
26
28
interface Handler {
27
- String attributes (AnnotationMirror annotationMirror , Element element );
29
+ String attributes (AnnotationMirror annotationMirror , Element element , Element target );
28
30
29
31
String attributes (Map <String , Object > attributeMap );
30
32
}
@@ -43,16 +45,12 @@ interface Handler {
43
45
handlers .put ("jakarta.validation.constraints.DecimalMax" , decimalHandler );
44
46
handlers .put ("jakarta.validation.constraints.DecimalMin" , decimalHandler );
45
47
46
- final var commonHandler = new CommonHandler ();
47
- final String [] keys = {
48
- "AssertFalse" ,
49
- "AssertTrue" ,
50
- "Null" ,
51
- "NotNull" ,
52
- "NotBlank" ,
53
- "NotEmpty" ,
48
+ final String [] withTypeKeys = {
49
+ "Length" ,
50
+ "Range" ,
51
+ "Max" ,
52
+ "Min" ,
54
53
"Size" ,
55
- "Email" ,
56
54
"Past" ,
57
55
"PastOrPresent" ,
58
56
"Future" ,
@@ -62,8 +60,21 @@ interface Handler {
62
60
"PositiveOrZero" ,
63
61
"Negative" ,
64
62
"NegativeOrZero" ,
65
- "Max" ,
66
- "Min"
63
+ };
64
+ var c = new CommonWithTypeHandler ();
65
+ for (final String key : withTypeKeys ) {
66
+ handlers .put ("io.avaje.validation.constraints." + key , c );
67
+ handlers .put ("jakarta.validation.constraints." + key , c );
68
+ }
69
+ final var commonHandler = new CommonHandler ();
70
+ final String [] keys = {
71
+ "AssertFalse" ,
72
+ "AssertTrue" ,
73
+ "Null" ,
74
+ "NotNull" ,
75
+ "NotBlank" ,
76
+ "NotEmpty" ,
77
+ "Email" ,
67
78
};
68
79
for (final String key : keys ) {
69
80
handlers .put ("io.avaje.validation.constraints." + key , commonHandler );
@@ -73,11 +84,11 @@ interface Handler {
73
84
74
85
private AnnotationUtil () {}
75
86
76
- static String annotationAttributeMap (AnnotationMirror annotationMirror ) {
87
+ static String annotationAttributeMap (AnnotationMirror annotationMirror , Element target ) {
77
88
final Element element = annotationMirror .getAnnotationType ().asElement ();
78
89
final Handler handler = handlers .get (element .toString ());
79
90
return Objects .requireNonNullElse (handler , defaultHandler )
80
- .attributes (annotationMirror , element );
91
+ .attributes (annotationMirror , element , target );
81
92
}
82
93
83
94
static String annotationAttributeMap (String annotationStr ) {
@@ -221,7 +232,7 @@ private static String avajeKey(String messageKey) {
221
232
static class PatternHandler extends BaseHandler {
222
233
223
234
@ Override
224
- public String attributes (AnnotationMirror annotationMirror , Element element ) {
235
+ public String attributes (AnnotationMirror annotationMirror , Element element , Element target ) {
225
236
return new PatternHandler ().writeAttributes (annotationMirror );
226
237
}
227
238
@@ -289,21 +300,24 @@ static class StandardHandler extends BaseHandler {
289
300
290
301
protected final AnnotationMirror annotationMirror ;
291
302
protected final Element element ;
303
+ protected final Element target ;
292
304
293
305
/** Prototype factory */
294
306
StandardHandler () {
295
307
this .annotationMirror = null ;
296
308
this .element = null ;
309
+ this .target = null ;
297
310
}
298
311
299
- StandardHandler (AnnotationMirror annotationMirror , Element element ) {
312
+ StandardHandler (AnnotationMirror annotationMirror , Element element , Element target ) {
300
313
this .annotationMirror = annotationMirror ;
301
314
this .element = element ;
315
+ this .target = target ;
302
316
}
303
317
304
318
@ Override
305
- public String attributes (AnnotationMirror annotationMirror , Element element ) {
306
- return new StandardHandler (annotationMirror , element ).writeAttributes ();
319
+ public String attributes (AnnotationMirror annotationMirror , Element element , Element target ) {
320
+ return new StandardHandler (annotationMirror , element , target ).writeAttributes ();
307
321
}
308
322
309
323
String writeAttributes () {
@@ -316,10 +330,15 @@ String writeAttributes() {
316
330
}
317
331
writeAttribute (member .getSimpleName (), value , defaultValue );
318
332
}
333
+ writeTypeAttribute (target .asType ());
319
334
sb .append (")" );
320
335
return sb .toString ();
321
336
}
322
337
338
+ protected void writeTypeAttribute (TypeMirror typeMirror ) {
339
+ // do nothing by default
340
+ }
341
+
323
342
void writeAttribute (Name simpleName , AnnotationValue value , AnnotationValue defaultValue ) {
324
343
writeAttributeKey (simpleName .toString ());
325
344
if (value != null ) {
@@ -365,18 +384,39 @@ public String attributes(Map<String, Object> attributeMap) {
365
384
}
366
385
}
367
386
387
+ static class CommonWithTypeHandler extends CommonHandler {
388
+
389
+ CommonWithTypeHandler (AnnotationMirror annotationMirror , Element element , Element target ) {
390
+ super (annotationMirror , element , target );
391
+ }
392
+
393
+ CommonWithTypeHandler () {
394
+ }
395
+
396
+ @ Override
397
+ public String attributes (AnnotationMirror annotationMirror , Element element , Element target ) {
398
+ return new CommonWithTypeHandler (annotationMirror , element , target ).writeAttributes ();
399
+ }
400
+
401
+ @ Override
402
+ protected void writeTypeAttribute (TypeMirror typeMirror ) {
403
+ writeAttributeKey ("_type" );
404
+ sb .append (trimAnnotations (typeMirror .toString ()) + ".class" );
405
+ }
406
+ }
407
+
368
408
static class CommonHandler extends StandardHandler {
369
409
370
410
/** Prototype factory only */
371
411
CommonHandler () {}
372
412
373
- CommonHandler (AnnotationMirror annotationMirror , Element element ) {
374
- super (annotationMirror , element );
413
+ CommonHandler (AnnotationMirror annotationMirror , Element element , Element target ) {
414
+ super (annotationMirror , element , target );
375
415
}
376
416
377
417
@ Override
378
- public String attributes (AnnotationMirror annotationMirror , Element element ) {
379
- return new CommonHandler (annotationMirror , element ).writeAttributes ();
418
+ public String attributes (AnnotationMirror annotationMirror , Element element , Element target ) {
419
+ return new CommonHandler (annotationMirror , element , target ).writeAttributes ();
380
420
}
381
421
382
422
@ Override
@@ -405,12 +445,12 @@ static class DecimalHandler extends CommonHandler {
405
445
DecimalHandler () {}
406
446
407
447
@ Override
408
- public String attributes (AnnotationMirror annotationMirror , Element element ) {
409
- return new DecimalHandler (annotationMirror , element ).writeAttributes ();
448
+ public String attributes (AnnotationMirror annotationMirror , Element element , Element target ) {
449
+ return new DecimalHandler (annotationMirror , element , target ).writeAttributes ();
410
450
}
411
451
412
- DecimalHandler (AnnotationMirror annotationMirror , Element element ) {
413
- super (annotationMirror , element );
452
+ DecimalHandler (AnnotationMirror annotationMirror , Element element , Element target ) {
453
+ super (annotationMirror , element , target );
414
454
}
415
455
416
456
@ Override
0 commit comments