Skip to content

Commit 0454ccd

Browse files
committed
Optimized resolution of type variables in type hierarchies
1 parent d6de16d commit 0454ccd

File tree

1 file changed

+18
-21
lines changed

1 file changed

+18
-21
lines changed

engine/src/main/java/net/jqwik/engine/support/GenericsClassContext.java

+18-21
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public AnnotatedType[] getAnnotatedActualTypeArguments() {
102102
private TypeResolution resolveVariable(TypeResolution typeVariableResolution) {
103103
TypeResolution localResolution = resolveVariableLocally(typeVariableResolution);
104104
if (localResolution.isVariable()) {
105-
TypeResolution supertypeResolution = resolveVariableInSupertypes(localResolution);
105+
TypeResolution supertypeResolution = resolveVariableInSupertypesOf(localResolution, contextType);
106106
if (supertypeResolution.typeHasChanged()) {
107107
return resolveType(supertypeResolution);
108108
}
@@ -119,29 +119,25 @@ private TypeResolution resolveVariableLocally(TypeResolution typeResolution) {
119119
return resolutions.getOrDefault(variable, typeResolution.unchanged());
120120
}
121121

122-
private TypeResolution resolveVariableInSupertypes(TypeResolution typeResolution) {
123-
// TODO: Optimize so that only get supertypes of supertypes if necessary
124-
// i.e. it cannot be resolved in direct supertypes
125-
Set<TypeUsage> superTypes = getAllSupertypes(contextType);
126-
List<TypeResolution> typeResolutionList =
127-
superTypes.stream()
128-
.map(GenericsSupport::contextFor)
129-
.map(context -> context.resolveVariableLocally(typeResolution))
130-
.filter(TypeResolution::typeHasChanged)
131-
.collect(Collectors.toList());
132-
133-
return typeResolutionList.stream()
134-
.findFirst()
135-
.orElse(typeResolution.unchanged());
122+
private static TypeResolution resolveVariableInSupertypesOf(TypeResolution variableResolution, TypeUsage type) {
123+
return resolveVariableInTypes(variableResolution, type.getSuperTypes());
136124
}
137125

138-
private Set<TypeUsage> getAllSupertypes(TypeUsage contextType) {
139-
List<TypeUsage> directSupertypes = contextType.getSuperTypes();
140-
Set<TypeUsage> allSupertypes = new LinkedHashSet<>(directSupertypes);
141-
for (TypeUsage directSupertype : directSupertypes) {
142-
allSupertypes.addAll(getAllSupertypes(directSupertype));
126+
private static TypeResolution resolveVariableInTypes(TypeResolution variableResolution, Collection<TypeUsage> superTypes) {
127+
for (TypeUsage superType : superTypes) {
128+
GenericsClassContext context = GenericsSupport.contextFor(superType);
129+
TypeResolution resolved = context.resolveVariableLocally(variableResolution);
130+
if (resolved.typeHasChanged()) {
131+
return resolved;
132+
}
133+
}
134+
for (TypeUsage superType : superTypes) {
135+
TypeResolution typeResolution = resolveVariableInSupertypesOf(variableResolution, superType);
136+
if (typeResolution.typeHasChanged()) {
137+
return typeResolution;
138+
}
143139
}
144-
return allSupertypes;
140+
return variableResolution.unchanged();
145141
}
146142

147143
private static class LookupTypeVariable {
@@ -243,6 +239,7 @@ public Annotation[] getDeclaredAnnotations() {
243239

244240
// For compatibility with JDK >= 9. A breaking change in the JDK :-(
245241
// @Override
242+
@SuppressWarnings("Since15")
246243
public AnnotatedType getAnnotatedOwnerType() {
247244
// TODO: Return annotatedType.getAnnotatedOwnerType() as soon as Java >= 9 is being used
248245
return null;

0 commit comments

Comments
 (0)