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
This commit is contained in:
parent
a9b98cada5
commit
f27bdcb737
|
@ -27,7 +27,6 @@ import javax.lang.model.element.TypeElement;
|
||||||
import javax.lang.model.type.DeclaredType;
|
import javax.lang.model.type.DeclaredType;
|
||||||
import javax.lang.model.type.TypeKind;
|
import javax.lang.model.type.TypeKind;
|
||||||
import javax.lang.model.type.TypeMirror;
|
import javax.lang.model.type.TypeMirror;
|
||||||
import javax.lang.model.type.WildcardType;
|
|
||||||
import javax.lang.model.util.Types;
|
import javax.lang.model.util.Types;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -73,12 +72,25 @@ class TypeUtils {
|
||||||
TypeUtils(ProcessingEnvironment env) {
|
TypeUtils(ProcessingEnvironment env) {
|
||||||
this.env = env;
|
this.env = env;
|
||||||
Types types = env.getTypeUtils();
|
Types types = env.getTypeUtils();
|
||||||
WildcardType wc = types.getWildcardType(null, null);
|
this.collectionType = getDeclaredType(types, Collection.class, 1);
|
||||||
this.collectionType = types.getDeclaredType(
|
this.mapType = getDeclaredType(types, Map.class, 2);
|
||||||
this.env.getElementUtils().getTypeElement(Collection.class.getName()),
|
}
|
||||||
wc);
|
|
||||||
this.mapType = types.getDeclaredType(
|
private TypeMirror getDeclaredType(Types types, Class<?> typeClass,
|
||||||
this.env.getElementUtils().getTypeElement(Map.class.getName()), wc, wc);
|
int numberOfTypeArgs) {
|
||||||
|
TypeMirror[] typeArgs = new TypeMirror[numberOfTypeArgs];
|
||||||
|
for (int i = 0; i < typeArgs.length; i++) {
|
||||||
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getType(Element element) {
|
public String getType(Element element) {
|
||||||
|
|
Loading…
Reference in New Issue