|
2 | 2 |
|
3 | 3 | import static io.avaje.validation.generator.APContext.typeElement;
|
4 | 4 | import static io.avaje.validation.generator.PrimitiveUtil.isPrimitiveValidationAnnotations;
|
5 |
| -import static java.util.stream.Collectors.toMap; |
| 5 | +import static java.util.stream.Collectors.toList; |
6 | 6 |
|
7 |
| -import java.util.HashMap; |
| 7 | +import java.util.ArrayList; |
8 | 8 | import java.util.List;
|
9 | 9 | import java.util.Map;
|
| 10 | +import java.util.Map.Entry; |
10 | 11 | import java.util.Objects;
|
11 | 12 | import java.util.Optional;
|
12 | 13 | import java.util.Set;
|
| 14 | +import java.util.stream.Stream; |
13 | 15 |
|
14 | 16 | import javax.lang.model.element.AnnotationMirror;
|
15 | 17 | import javax.lang.model.element.Element;
|
16 | 18 | import javax.lang.model.element.ExecutableElement;
|
17 |
| -import javax.lang.model.element.VariableElement; |
18 | 19 |
|
19 | 20 | record ElementAnnotationContainer(
|
20 | 21 | UType genericType,
|
21 | 22 | boolean hasValid,
|
22 |
| - Map<UType, String> annotations, |
23 |
| - Map<UType, String> typeUse1, |
24 |
| - Map<UType, String> typeUse2, |
25 |
| - Map<UType, String> crossParam) { |
| 23 | + List<Entry<UType, String>> annotations, |
| 24 | + List<Entry<UType, String>> typeUse1, |
| 25 | + List<Entry<UType, String>> typeUse2, |
| 26 | + List<Entry<UType, String>> crossParam) { |
26 | 27 |
|
27 | 28 | static ElementAnnotationContainer create(Element element) {
|
28 |
| - final var hasValid = ValidPrism.isPresent(element); |
29 |
| - Map<UType, String> typeUse1; |
30 |
| - Map<UType, String> typeUse2; |
31 |
| - final Map<UType, String> crossParam = new HashMap<>(); |
32 | 29 | UType uType;
|
33 | 30 | if (element instanceof final ExecutableElement executableElement) {
|
34 | 31 | uType = UType.parse(executableElement.getReturnType());
|
35 | 32 | } else {
|
36 | 33 | uType = UType.parse(element.asType());
|
37 | 34 | }
|
38 | 35 |
|
39 |
| - typeUse1 = |
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))); |
47 |
| - |
48 |
| - typeUse2 = |
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))); |
56 |
| - |
57 |
| - final var annotations = |
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))); |
| 36 | + final var hasValid = |
| 37 | + ValidPrism.isPresent(element) |
| 38 | + || uType.annotations().stream().anyMatch(ValidPrism::isInstance); |
| 39 | + |
| 40 | + List<Entry<UType, String>> typeUse1 = typeUseFor(uType.param0(), element); |
| 41 | + List<Entry<UType, String>> typeUse2 = typeUseFor(uType.param1(), element); |
| 42 | + |
| 43 | + final List<Entry<UType, String>> crossParam = new ArrayList<>(); |
| 44 | + final var annotations = annotations(element, uType, crossParam); |
75 | 45 |
|
76 | 46 | if (Util.isNonNullable(element)) {
|
77 | 47 | var nonNull = UType.parse(APContext.typeElement(NonNullPrism.PRISM_TYPE).asType());
|
78 |
| - annotations.put(nonNull, "Map.of(\"message\",\"{avaje.NotNull.message}\")"); |
| 48 | + annotations.add(Map.entry(nonNull, "Map.of(\"message\",\"{avaje.NotNull.message}\")")); |
79 | 49 | }
|
80 | 50 |
|
81 | 51 | return new ElementAnnotationContainer(uType, hasValid, annotations, typeUse1, typeUse2, crossParam);
|
82 | 52 | }
|
83 | 53 |
|
| 54 | + private static List<Entry<UType, String>> annotations(Element element, UType uType, List<Entry<UType, String>> crossParam) { |
| 55 | + return Stream.concat(element.getAnnotationMirrors().stream(), uType.annotations().stream()) |
| 56 | + .filter(m -> !ValidPrism.isInstance(m)) |
| 57 | + .filter(ElementAnnotationContainer::hasMetaConstraintAnnotation) |
| 58 | + .map(a -> { |
| 59 | + if (CrossParamConstraintPrism.isPresent(a.getAnnotationType().asElement())) { |
| 60 | + crossParam.add( |
| 61 | + Map.entry( |
| 62 | + UType.parse(a.getAnnotationType()), |
| 63 | + AnnotationUtil.annotationAttributeMap(a, element))); |
| 64 | + return null; |
| 65 | + } |
| 66 | + return a; |
| 67 | + }) |
| 68 | + .filter(Objects::nonNull) |
| 69 | + .map(a -> |
| 70 | + Map.entry( |
| 71 | + UType.parse(a.getAnnotationType()), |
| 72 | + AnnotationUtil.annotationAttributeMap(a, element))) |
| 73 | + .distinct() |
| 74 | + .collect(toList()); |
| 75 | + } |
| 76 | + |
| 77 | + private static List<Entry<UType, String>> typeUseFor(UType uType, Element element) { |
| 78 | + return Optional.ofNullable(uType).map(UType::annotations).stream() |
| 79 | + .flatMap(List::stream) |
| 80 | + .filter(ElementAnnotationContainer::hasMetaConstraintAnnotation) |
| 81 | + .map(a -> |
| 82 | + Map.entry( |
| 83 | + UType.parse(a.getAnnotationType()), |
| 84 | + AnnotationUtil.annotationAttributeMap(a, element))) |
| 85 | + .toList(); |
| 86 | + } |
| 87 | + |
84 | 88 | static boolean hasMetaConstraintAnnotation(AnnotationMirror m) {
|
85 | 89 | return hasMetaConstraintAnnotation(m.getAnnotationType().asElement())
|
86 |
| - || ValidPrism.isInstance(m); |
| 90 | + || ValidPrism.isInstance(m); |
87 | 91 | }
|
88 | 92 |
|
89 | 93 | static boolean hasMetaConstraintAnnotation(Element element) {
|
90 | 94 | return ConstraintPrism.isPresent(element);
|
91 | 95 | }
|
92 | 96 |
|
93 |
| - // it seems we cannot directly retrieve mirrors from var elements, so var Elements needs special handling |
94 |
| - |
95 |
| - static ElementAnnotationContainer create(VariableElement varElement) { |
96 |
| - var uType = UType.parse(varElement.asType()); |
97 |
| - final var annotations = |
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 |
| - |
106 |
| - var typeUse1 = |
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))); |
114 |
| - |
115 |
| - var typeUse2 = |
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))); |
123 |
| - |
124 |
| - final boolean hasValid = uType.annotations().stream().anyMatch(ValidPrism::isInstance); |
125 |
| - |
126 |
| - if (Util.isNonNullable(varElement)) { |
127 |
| - var nonNull = UType.parse(APContext.typeElement(NonNullPrism.PRISM_TYPE).asType()); |
128 |
| - annotations.put(nonNull, "Map.of(\"message\",\"{avaje.NotNull.message}\")"); |
129 |
| - } |
130 |
| - |
131 |
| - return new ElementAnnotationContainer(uType, hasValid, annotations, typeUse1, typeUse2, Map.of()); |
132 |
| - } |
133 |
| - |
134 | 97 | public void addImports(Set<String> importTypes) {
|
135 | 98 | importTypes.addAll(genericType.importTypes());
|
136 |
| - annotations.keySet().forEach(t -> importTypes.addAll(t.importTypes())); |
137 |
| - typeUse1.keySet().forEach(t -> importTypes.addAll(t.importTypes())); |
138 |
| - typeUse2.keySet().forEach(t -> importTypes.addAll(t.importTypes())); |
| 99 | + annotations.forEach(t -> importTypes.addAll(t.getKey().importTypes())); |
| 100 | + typeUse1.forEach(t -> importTypes.addAll(t.getKey().importTypes())); |
| 101 | + typeUse2.forEach(t -> importTypes.addAll(t.getKey().importTypes())); |
139 | 102 | }
|
140 | 103 |
|
141 | 104 | boolean isEmpty() {
|
142 | 105 | return annotations.isEmpty() && typeUse1.isEmpty() && typeUse2.isEmpty();
|
143 | 106 | }
|
144 | 107 |
|
145 | 108 | boolean supportsPrimitiveValidation() {
|
146 |
| - for (final var validationAnnotation : annotations.keySet()) { |
| 109 | + for (final var entry : annotations) { |
| 110 | + var validationAnnotation = entry.getKey(); |
147 | 111 | ConstraintPrism.getOptionalOn(typeElement(validationAnnotation.full()))
|
148 | 112 | .ifPresent(p -> {
|
149 | 113 | if (p.unboxPrimitives()) {
|
|
0 commit comments