1
1
package io .avaje .validation .generator ;
2
2
3
+ import static io .avaje .validation .generator .ProcessingContext .isAssignable2Interface ;
4
+ import static io .avaje .validation .generator .Util .trimAnnotations ;
3
5
import static java .util .function .Predicate .not ;
4
6
import static java .util .stream .Collectors .joining ;
5
7
import static java .util .stream .Collectors .toMap ;
6
8
7
- import java .util .Arrays ;
8
- import java .util .HashMap ;
9
- import java .util .List ;
10
- import java .util .Map ;
11
- import java .util .Objects ;
9
+ import java .util .*;
12
10
import java .util .regex .Pattern ;
13
11
14
12
import javax .lang .model .element .AnnotationMirror ;
19
17
import javax .lang .model .element .TypeElement ;
20
18
import javax .lang .model .element .VariableElement ;
21
19
import javax .lang .model .type .ArrayType ;
20
+ import javax .lang .model .type .TypeMirror ;
22
21
import javax .lang .model .util .ElementFilter ;
23
22
24
23
final class AnnotationUtil {
25
24
26
25
interface Handler {
27
- String attributes (AnnotationMirror annotationMirror , Element element );
26
+ String attributes (AnnotationMirror annotationMirror , Element element , Element target );
28
27
29
28
String attributes (Map <String , Object > attributeMap );
30
29
}
@@ -51,8 +50,12 @@ interface Handler {
51
50
"NotNull" ,
52
51
"NotBlank" ,
53
52
"NotEmpty" ,
54
- "Size" ,
55
53
"Email" ,
54
+ "Length" ,
55
+ "Range" ,
56
+ "Max" ,
57
+ "Min" ,
58
+ "Size" ,
56
59
"Past" ,
57
60
"PastOrPresent" ,
58
61
"Future" ,
@@ -62,22 +65,64 @@ interface Handler {
62
65
"PositiveOrZero" ,
63
66
"Negative" ,
64
67
"NegativeOrZero" ,
65
- "Max" ,
66
- "Min"
67
68
};
68
69
for (final String key : keys ) {
69
70
handlers .put ("io.avaje.validation.constraints." + key , commonHandler );
70
71
handlers .put ("jakarta.validation.constraints." + key , commonHandler );
71
72
}
72
73
}
73
74
75
+ static Map <String ,String > KNOWN_TYPES = new HashMap <>();
76
+ static {
77
+ KNOWN_TYPES .put ("byte" , "Byte" );
78
+ KNOWN_TYPES .put ("java.lang.Byte" , "Byte" );
79
+ KNOWN_TYPES .put ("short" , "Short" );
80
+ KNOWN_TYPES .put ("java.lang.Short" , "Short" );
81
+ KNOWN_TYPES .put ("int" , "Integer" );
82
+ KNOWN_TYPES .put ("java.lang.Integer" , "Integer" );
83
+ KNOWN_TYPES .put ("java.util.OptionalInt" , "Integer" );
84
+ KNOWN_TYPES .put ("long" , "Long" );
85
+ KNOWN_TYPES .put ("java.lang.Long" , "Long" );
86
+ KNOWN_TYPES .put ("java.util.OptionalLong" , "Long" );
87
+ KNOWN_TYPES .put ("float" , "Float" );
88
+ KNOWN_TYPES .put ("java.lang.Float" , "Float" );
89
+ KNOWN_TYPES .put ("double" , "Double" );
90
+ KNOWN_TYPES .put ("java.lang.Double" , "Double" );
91
+ KNOWN_TYPES .put ("java.util.OptionalDouble" , "Double" );
92
+ KNOWN_TYPES .put ("java.math.BigDecimal" , "BigDecimal" );
93
+ KNOWN_TYPES .put ("java.math.BigInteger" , "BigInteger" );
94
+ KNOWN_TYPES .put ("java.lang.String" , "String" );
95
+ //TODO; Consider java.time types
96
+ }
97
+
98
+ static String lookupType (TypeMirror typeMirror ) {
99
+ String rawType = trimAnnotations (typeMirror .toString ());
100
+ final String val = KNOWN_TYPES .get (rawType );
101
+ if (val != null ) {
102
+ return val ;
103
+ }
104
+ if (isAssignable2Interface (rawType , "java.math.BigDecimal" )) {
105
+ return "BigDecimal" ;
106
+ }
107
+ if (isAssignable2Interface (rawType , "java.math.BigInteger" )) {
108
+ return "BigInteger" ;
109
+ }
110
+ if (isAssignable2Interface (rawType , "java.lang.Number" )) {
111
+ return "Number" ;
112
+ }
113
+ if (isAssignable2Interface (rawType , "java.lang.CharSequence" )) {
114
+ return "CharSequence" ;
115
+ }
116
+ return null ;
117
+ }
118
+
74
119
private AnnotationUtil () {}
75
120
76
- static String annotationAttributeMap (AnnotationMirror annotationMirror ) {
121
+ static String annotationAttributeMap (AnnotationMirror annotationMirror , Element target ) {
77
122
final Element element = annotationMirror .getAnnotationType ().asElement ();
78
123
final Handler handler = handlers .get (element .toString ());
79
124
return Objects .requireNonNullElse (handler , defaultHandler )
80
- .attributes (annotationMirror , element );
125
+ .attributes (annotationMirror , element , target );
81
126
}
82
127
83
128
static String annotationAttributeMap (String annotationStr ) {
@@ -100,7 +145,6 @@ static String annotationAttributeMap(String annotationStr) {
100
145
convertTypeUse (element , attributeMap );
101
146
102
147
final Handler handler = handlers .get (result );
103
-
104
148
return Objects .requireNonNullElse (handler , defaultHandler ).attributes (attributeMap );
105
149
}
106
150
@@ -221,7 +265,7 @@ private static String avajeKey(String messageKey) {
221
265
static class PatternHandler extends BaseHandler {
222
266
223
267
@ Override
224
- public String attributes (AnnotationMirror annotationMirror , Element element ) {
268
+ public String attributes (AnnotationMirror annotationMirror , Element element , Element target ) {
225
269
return new PatternHandler ().writeAttributes (annotationMirror );
226
270
}
227
271
@@ -289,21 +333,24 @@ static class StandardHandler extends BaseHandler {
289
333
290
334
protected final AnnotationMirror annotationMirror ;
291
335
protected final Element element ;
336
+ protected final Element target ;
292
337
293
338
/** Prototype factory */
294
339
StandardHandler () {
295
340
this .annotationMirror = null ;
296
341
this .element = null ;
342
+ this .target = null ;
297
343
}
298
344
299
- StandardHandler (AnnotationMirror annotationMirror , Element element ) {
345
+ StandardHandler (AnnotationMirror annotationMirror , Element element , Element target ) {
300
346
this .annotationMirror = annotationMirror ;
301
347
this .element = element ;
348
+ this .target = target ;
302
349
}
303
350
304
351
@ Override
305
- public String attributes (AnnotationMirror annotationMirror , Element element ) {
306
- return new StandardHandler (annotationMirror , element ).writeAttributes ();
352
+ public String attributes (AnnotationMirror annotationMirror , Element element , Element target ) {
353
+ return new StandardHandler (annotationMirror , element , target ).writeAttributes ();
307
354
}
308
355
309
356
String writeAttributes () {
@@ -316,10 +363,19 @@ String writeAttributes() {
316
363
}
317
364
writeAttribute (member .getSimpleName (), value , defaultValue );
318
365
}
366
+ writeTypeAttribute (target .asType ());
319
367
sb .append (")" );
320
368
return sb .toString ();
321
369
}
322
370
371
+ protected void writeTypeAttribute (TypeMirror typeMirror ) {
372
+ String _type = lookupType (typeMirror );
373
+ if (_type != null ) {
374
+ writeAttributeKey ("_type" );
375
+ sb .append ('"' ).append (_type ).append ('"' );
376
+ }
377
+ }
378
+
323
379
void writeAttribute (Name simpleName , AnnotationValue value , AnnotationValue defaultValue ) {
324
380
writeAttributeKey (simpleName .toString ());
325
381
if (value != null ) {
@@ -370,13 +426,13 @@ static class CommonHandler extends StandardHandler {
370
426
/** Prototype factory only */
371
427
CommonHandler () {}
372
428
373
- CommonHandler (AnnotationMirror annotationMirror , Element element ) {
374
- super (annotationMirror , element );
429
+ CommonHandler (AnnotationMirror annotationMirror , Element element , Element target ) {
430
+ super (annotationMirror , element , target );
375
431
}
376
432
377
433
@ Override
378
- public String attributes (AnnotationMirror annotationMirror , Element element ) {
379
- return new CommonHandler (annotationMirror , element ).writeAttributes ();
434
+ public String attributes (AnnotationMirror annotationMirror , Element element , Element target ) {
435
+ return new CommonHandler (annotationMirror , element , target ).writeAttributes ();
380
436
}
381
437
382
438
@ Override
@@ -405,12 +461,12 @@ static class DecimalHandler extends CommonHandler {
405
461
DecimalHandler () {}
406
462
407
463
@ Override
408
- public String attributes (AnnotationMirror annotationMirror , Element element ) {
409
- return new DecimalHandler (annotationMirror , element ).writeAttributes ();
464
+ public String attributes (AnnotationMirror annotationMirror , Element element , Element target ) {
465
+ return new DecimalHandler (annotationMirror , element , target ).writeAttributes ();
410
466
}
411
467
412
- DecimalHandler (AnnotationMirror annotationMirror , Element element ) {
413
- super (annotationMirror , element );
468
+ DecimalHandler (AnnotationMirror annotationMirror , Element element , Element target ) {
469
+ super (annotationMirror , element , target );
414
470
}
415
471
416
472
@ Override
0 commit comments