Skip to content

Commit

Permalink
Unit test for match against unresolvable wildcard
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller committed Dec 5, 2024
1 parent 4fbca99 commit 3571952
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -309,12 +309,12 @@ private boolean isAssignableFrom(ResolvableType other, boolean strict, @Nullable

// 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) {
return (ourBounds != null && ourBounds.isSameKind(typeBounds) &&
ourBounds.isAssignableFrom(typeBounds.getBounds()));
if (otherBounds != null) {
return (ourBounds != null && ourBounds.isSameKind(otherBounds) &&
ourBounds.isAssignableFrom(otherBounds.getBounds()));
}

// In the form <? extends Number> is assignable to X...
Expand Down Expand Up @@ -365,8 +365,8 @@ private boolean isAssignableFrom(ResolvableType other, boolean strict, @Nullable
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 @@ -375,7 +375,7 @@ private boolean isAssignableFrom(ResolvableType other, boolean strict, @Nullable
}
matchedBefore.put(this.type, other.type);
for (int i = 0; i < ourGenerics.length; i++) {
if (!ourGenerics[i].isAssignableFrom(typeGenerics[i], true, matchedBefore)) {
if (!ourGenerics[i].isAssignableFrom(otherGenerics[i], true, matchedBefore)) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -169,7 +169,7 @@ void forInstanceProvider() {

@Test
void forInstanceProviderNull() {
ResolvableType type = ResolvableType.forInstance(new MyGenericInterfaceType<String>(null));
ResolvableType type = ResolvableType.forInstance(new MyGenericInterfaceType<>(null));
assertThat(type.getType()).isEqualTo(MyGenericInterfaceType.class);
assertThat(type.resolve()).isEqualTo(MyGenericInterfaceType.class);
}
Expand Down Expand Up @@ -1177,6 +1177,20 @@ 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.isAssignableFrom(wildcardConcrete)).isTrue();
assertThat(wildcardFixed.isAssignableFrom(wildcard)).isFalse();
assertThat(wildcardFixed.isAssignableFrom(wildcardConcrete)).isFalse();
assertThat(wildcardConcrete.isAssignableFrom(wildcard)).isTrue();
assertThat(wildcardConcrete.isAssignableFrom(wildcardFixed)).isFalse();
}

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


public class MySimpleInterfaceType implements MyInterfaceType<String> {
}

Expand All @@ -1584,28 +1597,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 3571952

Please sign in to comment.