Skip to content

Commit

Permalink
Restore lenient match against unresolvable wildcard
Browse files Browse the repository at this point in the history
Closes gh-33982
  • Loading branch information
jhoeller committed Dec 5, 2024
1 parent e618f92 commit b5dd0a6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -334,19 +334,19 @@ private boolean isAssignableFrom(ResolvableType other, boolean strict,

// Deal with wildcard bounds
WildcardBounds ourBounds = WildcardBounds.get(this);
WildcardBounds typeBounds = WildcardBounds.get(other);
WildcardBounds otherBounds = WildcardBounds.get(other);

// In the form X is assignable to <? extends Number>
if (typeBounds != null) {
if (otherBounds != null) {
if (ourBounds != null) {
return (ourBounds.isSameKind(typeBounds) &&
ourBounds.isAssignableFrom(typeBounds.getBounds(), matchedBefore));
return (ourBounds.isSameKind(otherBounds) &&
ourBounds.isAssignableFrom(otherBounds.getBounds(), matchedBefore));
}
else if (upUntilUnresolvable) {
return typeBounds.isAssignableFrom(this, matchedBefore);
return otherBounds.isAssignableFrom(this, matchedBefore);
}
else if (!exactMatch) {
return typeBounds.isAssignableTo(this, matchedBefore);
return otherBounds.isAssignableTo(this, matchedBefore);
}
else {
return false;
Expand Down Expand Up @@ -400,8 +400,8 @@ else if (!exactMatch) {
if (checkGenerics) {
// Recursively check each generic
ResolvableType[] ourGenerics = getGenerics();
ResolvableType[] typeGenerics = other.as(ourResolved).getGenerics();
if (ourGenerics.length != typeGenerics.length) {
ResolvableType[] otherGenerics = other.as(ourResolved).getGenerics();
if (ourGenerics.length != otherGenerics.length) {
return false;
}
if (ourGenerics.length > 0) {
Expand All @@ -410,7 +410,8 @@ else if (!exactMatch) {
}
matchedBefore.put(this.type, other.type);
for (int i = 0; i < ourGenerics.length; i++) {
if (!ourGenerics[i].isAssignableFrom(typeGenerics[i], true, matchedBefore, upUntilUnresolvable)) {
if (!ourGenerics[i].isAssignableFrom(otherGenerics[i],
!other.hasUnresolvableGenerics(), matchedBefore, upUntilUnresolvable)) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,26 @@ void isAssignableFromForComplexWildcards() throws Exception {
assertThatResolvableType(complex4).isNotAssignableFrom(complex3);
}

@Test
void isAssignableFromForUnresolvedWildcards() {
ResolvableType wildcard = ResolvableType.forInstance(new Wildcard<>());
ResolvableType wildcardFixed = ResolvableType.forInstance(new WildcardFixed());
ResolvableType wildcardConcrete = ResolvableType.forClassWithGenerics(Wildcard.class, Number.class);

assertThat(wildcard.isAssignableFrom(wildcardFixed)).isTrue();
assertThat(wildcard.isAssignableFromResolvedPart(wildcardFixed)).isTrue();
assertThat(wildcard.isAssignableFrom(wildcardConcrete)).isTrue();
assertThat(wildcard.isAssignableFromResolvedPart(wildcardConcrete)).isTrue();
assertThat(wildcardFixed.isAssignableFrom(wildcard)).isFalse();
assertThat(wildcardFixed.isAssignableFromResolvedPart(wildcard)).isFalse();
assertThat(wildcardFixed.isAssignableFrom(wildcardConcrete)).isFalse();
assertThat(wildcardFixed.isAssignableFromResolvedPart(wildcardConcrete)).isFalse();
assertThat(wildcardConcrete.isAssignableFrom(wildcard)).isTrue();
assertThat(wildcardConcrete.isAssignableFromResolvedPart(wildcard)).isTrue();
assertThat(wildcardConcrete.isAssignableFrom(wildcardFixed)).isFalse();
assertThat(wildcardConcrete.isAssignableFromResolvedPart(wildcardFixed)).isFalse();
}

@Test
void identifyTypeVariable() throws Exception {
Method method = ClassArguments.class.getMethod("typedArgumentFirst", Class.class, Class.class, Class.class);
Expand Down Expand Up @@ -1685,7 +1705,6 @@ public ResolvableType getResolvableType() {
}
}


public class MySimpleInterfaceType implements MyInterfaceType<String> {
}

Expand All @@ -1695,28 +1714,24 @@ public abstract class MySimpleInterfaceTypeWithImplementsRaw implements MyInterf
public abstract class ExtendsMySimpleInterfaceTypeWithImplementsRaw extends MySimpleInterfaceTypeWithImplementsRaw {
}


public class MyCollectionInterfaceType implements MyInterfaceType<Collection<String>> {
}


public abstract class MySuperclassType<T> {
}


public class MySimpleSuperclassType extends MySuperclassType<String> {
}


public class MyCollectionSuperclassType extends MySuperclassType<Collection<String>> {
}


interface Wildcard<T extends Number> extends List<T> {
public class Wildcard<T extends Number> {
}


interface RawExtendsWildcard extends Wildcard {
public class WildcardFixed extends Wildcard<Integer> {
}


Expand Down

0 comments on commit b5dd0a6

Please sign in to comment.