Skip to content

Commit f27bdcb

Browse files
committed
Prevent APT crashes on older Java versions
Update TypeUtils to guard against the use of older Java versions. Both `Collection` and `Map` type lookups now fallback to generic free versions of the classes. Prior to this commit using `xmlbeans-maven-plugin` in combination with Spring Boot's annotation processor could result in `IllegalArgumentException: Incorrect number of type arguments`. Fixes gh-6122
1 parent a9b98ca commit f27bdcb

File tree

1 file changed

+19
-7
lines changed
  • spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor

1 file changed

+19
-7
lines changed

spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/TypeUtils.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import javax.lang.model.type.DeclaredType;
2828
import javax.lang.model.type.TypeKind;
2929
import javax.lang.model.type.TypeMirror;
30-
import javax.lang.model.type.WildcardType;
3130
import javax.lang.model.util.Types;
3231

3332
/**
@@ -73,12 +72,25 @@ class TypeUtils {
7372
TypeUtils(ProcessingEnvironment env) {
7473
this.env = env;
7574
Types types = env.getTypeUtils();
76-
WildcardType wc = types.getWildcardType(null, null);
77-
this.collectionType = types.getDeclaredType(
78-
this.env.getElementUtils().getTypeElement(Collection.class.getName()),
79-
wc);
80-
this.mapType = types.getDeclaredType(
81-
this.env.getElementUtils().getTypeElement(Map.class.getName()), wc, wc);
75+
this.collectionType = getDeclaredType(types, Collection.class, 1);
76+
this.mapType = getDeclaredType(types, Map.class, 2);
77+
}
78+
79+
private TypeMirror getDeclaredType(Types types, Class<?> typeClass,
80+
int numberOfTypeArgs) {
81+
TypeMirror[] typeArgs = new TypeMirror[numberOfTypeArgs];
82+
for (int i = 0; i < typeArgs.length; i++) {
83+
typeArgs[i] = types.getWildcardType(null, null);
84+
}
85+
TypeElement typeElement = this.env.getElementUtils()
86+
.getTypeElement(typeClass.getName());
87+
try {
88+
return types.getDeclaredType(typeElement, typeArgs);
89+
}
90+
catch (IllegalArgumentException ex) {
91+
// Try again without generics for older Java versions
92+
return types.getDeclaredType(typeElement);
93+
}
8294
}
8395

8496
public String getType(Element element) {

0 commit comments

Comments
 (0)