Skip to content

Commit

Permalink
Prevent APT crashes on older Java versions
Browse files Browse the repository at this point in the history
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 spring-projectsgh-6122
  • Loading branch information
philwebb committed Jun 11, 2016
1 parent a9b98ca commit f27bdcb
Showing 1 changed file with 19 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.type.WildcardType;
import javax.lang.model.util.Types;

/**
Expand Down Expand Up @@ -73,12 +72,25 @@ class TypeUtils {
TypeUtils(ProcessingEnvironment env) {
this.env = env;
Types types = env.getTypeUtils();
WildcardType wc = types.getWildcardType(null, null);
this.collectionType = types.getDeclaredType(
this.env.getElementUtils().getTypeElement(Collection.class.getName()),
wc);
this.mapType = types.getDeclaredType(
this.env.getElementUtils().getTypeElement(Map.class.getName()), wc, wc);
this.collectionType = getDeclaredType(types, Collection.class, 1);
this.mapType = getDeclaredType(types, Map.class, 2);
}

private TypeMirror getDeclaredType(Types types, Class<?> typeClass,
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) {
Expand Down

0 comments on commit f27bdcb

Please sign in to comment.