/
TypeUtils.java
430 lines (373 loc) · 13.9 KB
/
TypeUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationprocessor;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.RecordComponentElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.ArrayType;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.PrimitiveType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.type.TypeVariable;
import javax.lang.model.util.SimpleTypeVisitor8;
import javax.lang.model.util.Types;
/**
* Type Utilities.
*
* @author Stephane Nicoll
* @author Phillip Webb
* @author Pavel Anisimov
*/
class TypeUtils {
private static final Map<TypeKind, Class<?>> PRIMITIVE_WRAPPERS;
static {
Map<TypeKind, Class<?>> wrappers = new EnumMap<>(TypeKind.class);
wrappers.put(TypeKind.BOOLEAN, Boolean.class);
wrappers.put(TypeKind.BYTE, Byte.class);
wrappers.put(TypeKind.CHAR, Character.class);
wrappers.put(TypeKind.DOUBLE, Double.class);
wrappers.put(TypeKind.FLOAT, Float.class);
wrappers.put(TypeKind.INT, Integer.class);
wrappers.put(TypeKind.LONG, Long.class);
wrappers.put(TypeKind.SHORT, Short.class);
PRIMITIVE_WRAPPERS = Collections.unmodifiableMap(wrappers);
}
private static final Map<String, TypeKind> WRAPPER_TO_PRIMITIVE;
static {
Map<String, TypeKind> primitives = new HashMap<>();
PRIMITIVE_WRAPPERS.forEach((kind, wrapperClass) -> primitives.put(wrapperClass.getName(), kind));
WRAPPER_TO_PRIMITIVE = primitives;
}
private final ProcessingEnvironment env;
private final Types types;
private final TypeExtractor typeExtractor;
private final TypeMirror collectionType;
private final TypeMirror mapType;
private final Map<TypeElement, TypeDescriptor> typeDescriptors = new HashMap<>();
TypeUtils(ProcessingEnvironment env) {
this.env = env;
this.types = env.getTypeUtils();
this.typeExtractor = new TypeExtractor(this.types);
this.collectionType = getDeclaredType(this.types, Collection.class, 1);
this.mapType = getDeclaredType(this.types, Map.class, 2);
}
private TypeMirror getDeclaredType(Types types, Class<?> typeClass, int numberOfTypeArgs) {
TypeMirror[] typeArgs = new TypeMirror[numberOfTypeArgs];
Arrays.setAll(typeArgs, (i) -> types.getWildcardType(null, null));
TypeElement typeElement = this.env.getElementUtils().getTypeElement(typeClass.getName());
try {
return types.getDeclaredType(typeElement, typeArgs);
}
catch (IllegalArgumentException ex) {
// Try again without generics for older Java versions
return types.getDeclaredType(typeElement);
}
}
boolean isSameType(TypeMirror t1, TypeMirror t2) {
return this.types.isSameType(t1, t2);
}
Element asElement(TypeMirror type) {
return this.types.asElement(type);
}
/**
* Return the qualified name of the specified element.
* @param element the element to handle
* @return the fully qualified name of the element, suitable for a call to
* {@link Class#forName(String)}
*/
String getQualifiedName(Element element) {
return this.typeExtractor.getQualifiedName(element);
}
/**
* Return the type of the specified {@link TypeMirror} including all its generic
* information.
* @param element the {@link TypeElement} in which this {@code type} is declared
* @param type the type to handle
* @return a representation of the type including all its generic information
*/
String getType(TypeElement element, TypeMirror type) {
if (type == null) {
return null;
}
return type.accept(this.typeExtractor, createTypeDescriptor(element));
}
/**
* Extract the target element type from the specified container type or {@code null}
* if no element type was found.
* @param type a type, potentially wrapping an element type
* @return the element type or {@code null} if no specific type was found
*/
TypeMirror extractElementType(TypeMirror type) {
if (!this.env.getTypeUtils().isAssignable(type, this.collectionType)) {
return null;
}
return getCollectionElementType(type);
}
private TypeMirror getCollectionElementType(TypeMirror type) {
if (((TypeElement) this.types.asElement(type)).getQualifiedName().contentEquals(Collection.class.getName())) {
DeclaredType declaredType = (DeclaredType) type;
// raw type, just "Collection"
if (declaredType.getTypeArguments().isEmpty()) {
return this.types.getDeclaredType(this.env.getElementUtils().getTypeElement(Object.class.getName()));
}
// return type argument to Collection<...>
return declaredType.getTypeArguments().get(0);
}
// recursively walk the supertypes, looking for Collection<...>
for (TypeMirror superType : this.env.getTypeUtils().directSupertypes(type)) {
if (this.types.isAssignable(superType, this.collectionType)) {
return getCollectionElementType(superType);
}
}
return null;
}
boolean isCollectionOrMap(TypeMirror type) {
return this.env.getTypeUtils().isAssignable(type, this.collectionType)
|| this.env.getTypeUtils().isAssignable(type, this.mapType);
}
String getJavaDoc(Element element) {
if (element instanceof RecordComponentElement) {
return getJavaDoc((RecordComponentElement) element);
}
String javadoc = (element != null) ? this.env.getElementUtils().getDocComment(element) : null;
javadoc = (javadoc != null) ? cleanUpJavaDoc(javadoc) : null;
return (javadoc == null || javadoc.isEmpty()) ? null : javadoc;
}
/**
* Return the {@link PrimitiveType} of the specified type or {@code null} if the type
* does not represent a valid wrapper type.
* @param typeMirror a type
* @return the primitive type or {@code null} if the type is not a wrapper type
*/
PrimitiveType getPrimitiveType(TypeMirror typeMirror) {
if (getPrimitiveFor(typeMirror) != null) {
return this.types.unboxedType(typeMirror);
}
return null;
}
TypeMirror getWrapperOrPrimitiveFor(TypeMirror typeMirror) {
Class<?> candidate = getWrapperFor(typeMirror);
if (candidate != null) {
return this.env.getElementUtils().getTypeElement(candidate.getName()).asType();
}
TypeKind primitiveKind = getPrimitiveFor(typeMirror);
if (primitiveKind != null) {
return this.env.getTypeUtils().getPrimitiveType(primitiveKind);
}
return null;
}
private Class<?> getWrapperFor(TypeMirror type) {
return PRIMITIVE_WRAPPERS.get(type.getKind());
}
private TypeKind getPrimitiveFor(TypeMirror type) {
return WRAPPER_TO_PRIMITIVE.get(type.toString());
}
TypeDescriptor resolveTypeDescriptor(TypeElement element) {
if (this.typeDescriptors.containsKey(element)) {
return this.typeDescriptors.get(element);
}
return createTypeDescriptor(element);
}
private TypeDescriptor createTypeDescriptor(TypeElement element) {
TypeDescriptor descriptor = new TypeDescriptor();
process(descriptor, element.asType());
this.typeDescriptors.put(element, descriptor);
return descriptor;
}
private void process(TypeDescriptor descriptor, TypeMirror type) {
if (type.getKind() == TypeKind.DECLARED) {
DeclaredType declaredType = (DeclaredType) type;
DeclaredType freshType = (DeclaredType) this.env.getElementUtils()
.getTypeElement(this.types.asElement(type).toString())
.asType();
List<? extends TypeMirror> arguments = declaredType.getTypeArguments();
for (int i = 0; i < arguments.size(); i++) {
TypeMirror specificType = arguments.get(i);
TypeMirror signatureType = freshType.getTypeArguments().get(i);
descriptor.registerIfNecessary(signatureType, specificType);
}
TypeElement element = (TypeElement) this.types.asElement(type);
process(descriptor, element.getSuperclass());
}
}
private String getJavaDoc(RecordComponentElement recordComponent) {
String recordJavadoc = this.env.getElementUtils().getDocComment(recordComponent.getEnclosingElement());
if (recordJavadoc != null) {
Pattern paramJavadocPattern = paramJavadocPattern(recordComponent.getSimpleName().toString());
Matcher paramJavadocMatcher = paramJavadocPattern.matcher(recordJavadoc);
if (paramJavadocMatcher.find()) {
String paramJavadoc = cleanUpJavaDoc(paramJavadocMatcher.group());
return paramJavadoc.isEmpty() ? null : paramJavadoc;
}
}
return null;
}
private Pattern paramJavadocPattern(String paramName) {
String pattern = String.format("(?<=@param +%s).*?(?=([\r\n]+ *@)|$)", paramName);
return Pattern.compile(pattern, Pattern.DOTALL);
}
private String cleanUpJavaDoc(String javadoc) {
StringBuilder result = new StringBuilder(javadoc.length());
char lastChar = '.';
for (int i = 0; i < javadoc.length(); i++) {
char ch = javadoc.charAt(i);
boolean repeatedSpace = ch == ' ' && lastChar == ' ';
if (ch != '\r' && ch != '\n' && !repeatedSpace) {
result.append(ch);
lastChar = ch;
}
}
return result.toString().trim();
}
/**
* A visitor that extracts the fully qualified name of a type, including generic
* information.
*/
private static class TypeExtractor extends SimpleTypeVisitor8<String, TypeDescriptor> {
private final Types types;
TypeExtractor(Types types) {
this.types = types;
}
@Override
public String visitDeclared(DeclaredType type, TypeDescriptor descriptor) {
TypeElement enclosingElement = getEnclosingTypeElement(type);
String qualifiedName = determineQualifiedName(type, enclosingElement);
if (type.getTypeArguments().isEmpty()) {
return qualifiedName;
}
StringBuilder name = new StringBuilder();
name.append(qualifiedName);
name.append("<")
.append(type.getTypeArguments()
.stream()
.map((t) -> visit(t, descriptor))
.collect(Collectors.joining(",")))
.append(">");
return name.toString();
}
private String determineQualifiedName(DeclaredType type, TypeElement enclosingElement) {
if (enclosingElement != null) {
return getQualifiedName(enclosingElement) + "$" + type.asElement().getSimpleName();
}
return getQualifiedName(type.asElement());
}
@Override
public String visitTypeVariable(TypeVariable t, TypeDescriptor descriptor) {
TypeMirror typeMirror = descriptor.resolveGeneric(t);
if (typeMirror != null) {
if (typeMirror instanceof TypeVariable typeVariable) {
// Still unresolved, let's use the upper bound, checking first if
// a cycle may exist
if (!hasCycle(typeVariable)) {
return visit(typeVariable.getUpperBound(), descriptor);
}
}
else {
return visit(typeMirror, descriptor);
}
}
// Fallback to simple representation of the upper bound
return defaultAction(t.getUpperBound(), descriptor);
}
private boolean hasCycle(TypeVariable variable) {
TypeMirror upperBound = variable.getUpperBound();
if (upperBound instanceof DeclaredType declaredType) {
return declaredType.getTypeArguments().stream().anyMatch((candidate) -> candidate.equals(variable));
}
return false;
}
@Override
public String visitArray(ArrayType t, TypeDescriptor descriptor) {
return t.getComponentType().accept(this, descriptor) + "[]";
}
@Override
public String visitPrimitive(PrimitiveType t, TypeDescriptor descriptor) {
return this.types.boxedClass(t).getQualifiedName().toString();
}
@Override
protected String defaultAction(TypeMirror t, TypeDescriptor descriptor) {
return t.toString();
}
String getQualifiedName(Element element) {
if (element == null) {
return null;
}
TypeElement enclosingElement = getEnclosingTypeElement(element.asType());
if (enclosingElement != null) {
return getQualifiedName(enclosingElement) + "$"
+ ((DeclaredType) element.asType()).asElement().getSimpleName();
}
if (element instanceof TypeElement typeElement) {
return typeElement.getQualifiedName().toString();
}
throw new IllegalStateException("Could not extract qualified name from " + element);
}
private TypeElement getEnclosingTypeElement(TypeMirror type) {
if (type instanceof DeclaredType declaredType) {
Element enclosingElement = declaredType.asElement().getEnclosingElement();
if (enclosingElement instanceof TypeElement typeElement) {
return typeElement;
}
}
return null;
}
}
/**
* Descriptor for a given type.
*/
static class TypeDescriptor {
private final Map<TypeVariable, TypeMirror> generics = new HashMap<>();
Map<TypeVariable, TypeMirror> getGenerics() {
return Collections.unmodifiableMap(this.generics);
}
TypeMirror resolveGeneric(TypeVariable typeVariable) {
return resolveGeneric(getParameterName(typeVariable));
}
TypeMirror resolveGeneric(String parameterName) {
return this.generics.entrySet()
.stream()
.filter((e) -> getParameterName(e.getKey()).equals(parameterName))
.findFirst()
.map(Entry::getValue)
.orElse(null);
}
private void registerIfNecessary(TypeMirror variable, TypeMirror resolution) {
if (variable instanceof TypeVariable typeVariable) {
if (this.generics.keySet()
.stream()
.noneMatch((candidate) -> getParameterName(candidate).equals(getParameterName(typeVariable)))) {
this.generics.put(typeVariable, resolution);
}
}
}
private String getParameterName(TypeVariable typeVariable) {
return typeVariable.asElement().getSimpleName().toString();
}
}
}