private static TypeMirror findCollectionStrippedOfExtensions(TypeMirror typeMirror) {
TypeMirror found = null;
if (typeMirror instanceof DeclaredType) {
TypeDeclaration decl = ((DeclaredType) typeMirror).getDeclaration();
if (decl != null) {
String qn = decl.getQualifiedName();
if (List.class.getName().equals(qn) || Collection.class.getName().equals(qn)) {
return typeMirror;
}
else {
for (InterfaceType si : decl.getSuperinterfaces()) {
found = findCollectionStrippedOfExtensions(si);
if (found != null) {
break;
}
}
if (found == null && decl instanceof ClassDeclaration) {
found = findCollectionStrippedOfExtensions(((ClassDeclaration) decl).getSuperclass());
}
if (found != null) {
TypeParameterDeclaration typeParam = null;
for (TypeMirror typeArg : ((DeclaredType) found).getActualTypeArguments()) {
if (typeArg instanceof TypeVariable) {
typeParam = ((TypeVariable) typeArg).getDeclaration();
break;
}
}
if (typeParam != null) {
int typeArgIndex = -1;
for (TypeParameterDeclaration typeParamDeclaration : decl.getFormalTypeParameters()) {
typeArgIndex++;
if (typeParam.getSimpleName().equals(typeParamDeclaration.getSimpleName())) {
Iterator<TypeMirror> resolvingTypeArgs = ((DeclaredType) typeMirror).getActualTypeArguments().iterator();
TypeMirror resolved = null;
for (int resolvingTypeIndex = 0; resolvingTypeIndex <= typeArgIndex && resolvingTypeArgs.hasNext(); resolvingTypeIndex++) {
resolved = resolvingTypeArgs.next();
}
if (resolved != null) {
//got the resolved type mirror, create a new type mirror with the resolved argument instead.
TypeDeclaration foundDecl = ((DeclaredType) found).getDeclaration();
while (foundDecl instanceof DecoratedTypeDeclaration) {
foundDecl = (TypeDeclaration) ((DecoratedTypeDeclaration) foundDecl).getDelegate();
}
while (resolved instanceof DecoratedTypeMirror) {