16
16
import javax .lang .model .element .ExecutableElement ;
17
17
import javax .lang .model .element .VariableElement ;
18
18
19
- public record ElementAnnotationContainer (
19
+ record ElementAnnotationContainer (
20
20
UType genericType ,
21
21
boolean hasValid ,
22
22
Map <UType , String > annotations ,
@@ -32,97 +32,94 @@ static ElementAnnotationContainer create(Element element) {
32
32
UType uType ;
33
33
if (element instanceof final ExecutableElement executableElement ) {
34
34
uType = UType .parse (executableElement .getReturnType ());
35
-
36
35
} else {
37
36
uType = UType .parse (element .asType ());
38
37
}
39
38
40
39
typeUse1 =
41
- Optional .ofNullable (uType .param0 ()).map (UType ::annotations ).stream ()
42
- .flatMap (List ::stream )
43
- .filter (ElementAnnotationContainer ::hasMetaConstraintAnnotation )
44
- .collect (
45
- toMap (
46
- a -> UType .parse (a .getAnnotationType ()),
47
- a -> AnnotationUtil .annotationAttributeMap (a , element )));
40
+ Optional .ofNullable (uType .param0 ()).map (UType ::annotations ).stream ()
41
+ .flatMap (List ::stream )
42
+ .filter (ElementAnnotationContainer ::hasMetaConstraintAnnotation )
43
+ .collect (
44
+ toMap (
45
+ a -> UType .parse (a .getAnnotationType ()),
46
+ a -> AnnotationUtil .annotationAttributeMap (a , element )));
48
47
49
48
typeUse2 =
50
- Optional .ofNullable (uType .param1 ()).map (UType ::annotations ).stream ()
51
- .flatMap (List ::stream )
52
- .filter (ElementAnnotationContainer ::hasMetaConstraintAnnotation )
53
- .collect (
54
- toMap (
55
- a -> UType .parse (a .getAnnotationType ()),
56
- a -> AnnotationUtil .annotationAttributeMap (a , element )));
49
+ Optional .ofNullable (uType .param1 ()).map (UType ::annotations ).stream ()
50
+ .flatMap (List ::stream )
51
+ .filter (ElementAnnotationContainer ::hasMetaConstraintAnnotation )
52
+ .collect (
53
+ toMap (
54
+ a -> UType .parse (a .getAnnotationType ()),
55
+ a -> AnnotationUtil .annotationAttributeMap (a , element )));
57
56
58
57
final var annotations =
59
- element .getAnnotationMirrors ().stream ()
60
- .filter (m -> !ValidPrism .isInstance (m ))
61
- .filter (ElementAnnotationContainer ::hasMetaConstraintAnnotation )
62
- .map (
63
- a -> {
64
- if (CrossParamConstraintPrism .isPresent (a .getAnnotationType ().asElement ())) {
65
- crossParam .put (
66
- UType .parse (a .getAnnotationType ()),
67
- AnnotationUtil .annotationAttributeMap (a , element ));
68
- return null ;
69
- }
70
- return a ;
71
- })
72
- .filter (Objects ::nonNull )
73
- .collect (
74
- toMap (
75
- a -> UType .parse (a .getAnnotationType ()),
76
- a -> AnnotationUtil .annotationAttributeMap (a , element )));
58
+ element .getAnnotationMirrors ().stream ()
59
+ .filter (m -> !ValidPrism .isInstance (m ))
60
+ .filter (ElementAnnotationContainer ::hasMetaConstraintAnnotation )
61
+ .map (a -> {
62
+ if (CrossParamConstraintPrism .isPresent (a .getAnnotationType ().asElement ())) {
63
+ crossParam .put (
64
+ UType .parse (a .getAnnotationType ()),
65
+ AnnotationUtil .annotationAttributeMap (a , element ));
66
+ return null ;
67
+ }
68
+ return a ;
69
+ })
70
+ .filter (Objects ::nonNull )
71
+ .collect (
72
+ toMap (
73
+ a -> UType .parse (a .getAnnotationType ()),
74
+ a -> AnnotationUtil .annotationAttributeMap (a , element )));
77
75
78
76
if (Util .isNonNullable (element )) {
79
77
var nonNull = UType .parse (APContext .typeElement (NonNullPrism .PRISM_TYPE ).asType ());
80
78
annotations .put (nonNull , "Map.of(\" message\" ,\" {avaje.NotNull.message}\" )" );
81
79
}
82
80
83
- return new ElementAnnotationContainer (
84
- uType , hasValid , annotations , typeUse1 , typeUse2 , crossParam );
81
+ return new ElementAnnotationContainer (uType , hasValid , annotations , typeUse1 , typeUse2 , crossParam );
85
82
}
86
83
87
84
static boolean hasMetaConstraintAnnotation (AnnotationMirror m ) {
88
85
return hasMetaConstraintAnnotation (m .getAnnotationType ().asElement ())
89
- || ValidPrism .isInstance (m );
86
+ || ValidPrism .isInstance (m );
90
87
}
91
88
92
89
static boolean hasMetaConstraintAnnotation (Element element ) {
93
90
return ConstraintPrism .isPresent (element );
94
91
}
95
92
96
- // it seems we cannot directly retrieve mirrors from var elements, so var Elements needs special
97
- // handling
93
+ // it seems we cannot directly retrieve mirrors from var elements, so var Elements needs special handling
98
94
99
95
static ElementAnnotationContainer create (VariableElement varElement ) {
100
96
var uType = UType .parse (varElement .asType ());
101
97
final var annotations =
102
- uType .annotations ().stream ()
103
- .filter (m -> !ValidPrism .isInstance (m ))
104
- .filter (ElementAnnotationContainer ::hasMetaConstraintAnnotation )
105
- .collect (
106
- toMap (
107
- a -> UType .parse (a .getAnnotationType ()),
108
- a -> AnnotationUtil .annotationAttributeMap (a , varElement )));
98
+ uType .annotations ().stream ()
99
+ .filter (m -> !ValidPrism .isInstance (m ))
100
+ .filter (ElementAnnotationContainer ::hasMetaConstraintAnnotation )
101
+ .collect (
102
+ toMap (
103
+ a -> UType .parse (a .getAnnotationType ()),
104
+ a -> AnnotationUtil .annotationAttributeMap (a , varElement )));
105
+
109
106
var typeUse1 =
110
- Optional .ofNullable (uType .param0 ()).map (UType ::annotations ).stream ()
111
- .flatMap (List ::stream )
112
- .filter (ElementAnnotationContainer ::hasMetaConstraintAnnotation )
113
- .collect (
114
- toMap (
115
- a -> UType .parse (a .getAnnotationType ()),
116
- a -> AnnotationUtil .annotationAttributeMap (a , varElement )));
107
+ Optional .ofNullable (uType .param0 ()).map (UType ::annotations ).stream ()
108
+ .flatMap (List ::stream )
109
+ .filter (ElementAnnotationContainer ::hasMetaConstraintAnnotation )
110
+ .collect (
111
+ toMap (
112
+ a -> UType .parse (a .getAnnotationType ()),
113
+ a -> AnnotationUtil .annotationAttributeMap (a , varElement )));
117
114
118
115
var typeUse2 =
119
- Optional .ofNullable (uType .param1 ()).map (UType ::annotations ).stream ()
120
- .flatMap (List ::stream )
121
- .filter (ElementAnnotationContainer ::hasMetaConstraintAnnotation )
122
- .collect (
123
- toMap (
124
- a -> UType .parse (a .getAnnotationType ()),
125
- a -> AnnotationUtil .annotationAttributeMap (a , varElement )));
116
+ Optional .ofNullable (uType .param1 ()).map (UType ::annotations ).stream ()
117
+ .flatMap (List ::stream )
118
+ .filter (ElementAnnotationContainer ::hasMetaConstraintAnnotation )
119
+ .collect (
120
+ toMap (
121
+ a -> UType .parse (a .getAnnotationType ()),
122
+ a -> AnnotationUtil .annotationAttributeMap (a , varElement )));
126
123
127
124
final boolean hasValid = uType .annotations ().stream ().anyMatch (ValidPrism ::isInstance );
128
125
@@ -131,8 +128,7 @@ static ElementAnnotationContainer create(VariableElement varElement) {
131
128
annotations .put (nonNull , "Map.of(\" message\" ,\" {avaje.NotNull.message}\" )" );
132
129
}
133
130
134
- return new ElementAnnotationContainer (
135
- uType , hasValid , annotations , typeUse1 , typeUse2 , Map .of ());
131
+ return new ElementAnnotationContainer (uType , hasValid , annotations , typeUse1 , typeUse2 , Map .of ());
136
132
}
137
133
138
134
public void addImports (Set <String > importTypes ) {
@@ -149,14 +145,13 @@ boolean isEmpty() {
149
145
boolean supportsPrimitiveValidation () {
150
146
for (final var validationAnnotation : annotations .keySet ()) {
151
147
ConstraintPrism .getOptionalOn (typeElement (validationAnnotation .full ()))
152
- .ifPresent (
153
- p -> {
154
- if (p .unboxPrimitives ()) {
155
- validationAnnotation
156
- .shortType ()
157
- .transform (PrimitiveUtil ::addPrimitiveValidationAnnotation );
158
- }
159
- });
148
+ .ifPresent (p -> {
149
+ if (p .unboxPrimitives ()) {
150
+ validationAnnotation
151
+ .shortType ()
152
+ .transform (PrimitiveUtil ::addPrimitiveValidationAnnotation );
153
+ }
154
+ });
160
155
161
156
if (!isPrimitiveValidationAnnotations (validationAnnotation .shortType ())) {
162
157
return false ;
0 commit comments