Polish 'Handle generics with identical names in different positions'
See gh-45011
This commit is contained in:
@@ -46,6 +46,7 @@ import javax.lang.model.util.Types;
|
||||
* @author Stephane Nicoll
|
||||
* @author Phillip Webb
|
||||
* @author Pavel Anisimov
|
||||
* @author Dmytro Nosan
|
||||
*/
|
||||
class TypeUtils {
|
||||
|
||||
@@ -217,7 +218,7 @@ class TypeUtils {
|
||||
return WRAPPER_TO_PRIMITIVE.get(type.toString());
|
||||
}
|
||||
|
||||
TypeDescriptor resolveTypeDescriptor(TypeElement element) {
|
||||
private TypeDescriptor resolveTypeDescriptor(TypeElement element) {
|
||||
if (this.typeDescriptors.containsKey(element)) {
|
||||
return this.typeDescriptors.get(element);
|
||||
}
|
||||
@@ -318,22 +319,22 @@ class TypeUtils {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitTypeVariable(TypeVariable t, TypeDescriptor descriptor) {
|
||||
TypeMirror typeMirror = descriptor.resolveGeneric(t);
|
||||
if (typeMirror != null) {
|
||||
if (typeMirror instanceof TypeVariable typeVariable) {
|
||||
public String visitTypeVariable(TypeVariable typeVariable, TypeDescriptor descriptor) {
|
||||
TypeMirror resolvedGeneric = descriptor.resolveGeneric(typeVariable);
|
||||
if (resolvedGeneric != null) {
|
||||
if (resolvedGeneric instanceof TypeVariable resolveTypeVariable) {
|
||||
// Still unresolved, let's use the upper bound, checking first if
|
||||
// a cycle may exist
|
||||
if (!hasCycle(typeVariable)) {
|
||||
return visit(typeVariable.getUpperBound(), descriptor);
|
||||
if (!hasCycle(resolveTypeVariable)) {
|
||||
return visit(resolveTypeVariable.getUpperBound(), descriptor);
|
||||
}
|
||||
}
|
||||
else {
|
||||
return visit(typeMirror, descriptor);
|
||||
return visit(resolvedGeneric, descriptor);
|
||||
}
|
||||
}
|
||||
// Fallback to simple representation of the upper bound
|
||||
return defaultAction(t.getUpperBound(), descriptor);
|
||||
return defaultAction(typeVariable.getUpperBound(), descriptor);
|
||||
}
|
||||
|
||||
private boolean hasCycle(TypeVariable variable) {
|
||||
@@ -394,20 +395,11 @@ class TypeUtils {
|
||||
private final Map<TypeVariable, TypeMirror> generics = new HashMap<>();
|
||||
|
||||
TypeMirror resolveGeneric(TypeVariable typeVariable) {
|
||||
if (this.generics.containsKey(typeVariable)) {
|
||||
TypeMirror resolvedType = this.generics.get(typeVariable);
|
||||
// Unresolved <T> -> <T>
|
||||
if (resolvedType == typeVariable) {
|
||||
return resolvedType;
|
||||
}
|
||||
// <T> -> <T1> -> <T2>
|
||||
if (resolvedType instanceof TypeVariable) {
|
||||
return resolveGeneric((TypeVariable) resolvedType);
|
||||
}
|
||||
// Resolved e.g. java.lang.String
|
||||
return resolvedType;
|
||||
TypeMirror resolved = this.generics.get(typeVariable);
|
||||
if (resolved != typeVariable && resolved instanceof TypeVariable resolvedTypeVariable) {
|
||||
return resolveGeneric(resolvedTypeVariable);
|
||||
}
|
||||
return null;
|
||||
return resolved;
|
||||
}
|
||||
|
||||
private void registerIfNecessary(TypeMirror variable, TypeMirror resolution) {
|
||||
|
||||
@@ -54,7 +54,6 @@ class TypeUtilsTests {
|
||||
assertThat(getTypeOfField(typeUtils, typeElement, "name")).hasToString(String.class.getName());
|
||||
assertThat(getTypeOfField(typeUtils, typeElement, "mappings"))
|
||||
.hasToString(constructMapType(Integer.class, Duration.class));
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user