Skip to content

Commit a21b27e

Browse files
committed
Use isAssignableValue instead of isInstanceOf for argument resolution
Includes related nullability refinements. Closes gh-28727
1 parent 14fd260 commit a21b27e

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/aot/AutowiredArguments.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import org.springframework.lang.Nullable;
2020
import org.springframework.util.Assert;
21+
import org.springframework.util.ClassUtils;
2122

2223
/**
2324
* Resolved arguments to be autowired.
@@ -38,11 +39,14 @@ public interface AutowiredArguments {
3839
* @param requiredType the required argument type
3940
* @return the argument
4041
*/
41-
@Nullable
4242
@SuppressWarnings("unchecked")
43+
@Nullable
4344
default <T> T get(int index, Class<T> requiredType) {
44-
Object value = get(index);
45-
Assert.isInstanceOf(requiredType, value);
45+
Object value = getObject(index);
46+
if (!ClassUtils.isAssignableValue(requiredType, value)) {
47+
throw new IllegalArgumentException("Argument type mismatch: expected '" +
48+
ClassUtils.getQualifiedName(requiredType) + "' for value [" + value + "]");
49+
}
4650
return (T) value;
4751
}
4852

@@ -52,17 +56,18 @@ default <T> T get(int index, Class<T> requiredType) {
5256
* @param index the argument index
5357
* @return the argument
5458
*/
55-
@Nullable
5659
@SuppressWarnings("unchecked")
60+
@Nullable
5761
default <T> T get(int index) {
58-
return (T) toArray()[index];
62+
return (T) getObject(index);
5963
}
6064

6165
/**
6266
* Return the resolved argument at the specified index.
6367
* @param index the argument index
6468
* @return the argument
6569
*/
70+
@Nullable
6671
default Object getObject(int index) {
6772
return toArray()[index];
6873
}

spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,6 +1215,7 @@ protected BeanWrapper obtainFromSupplier(Supplier<?> supplier, String beanName)
12151215
return bw;
12161216
}
12171217

1218+
@Nullable
12181219
private Object obtainInstanceFromSupplier(Supplier<?> supplier, String beanName) {
12191220
String outerBean = this.currentlyCreatedBean.get();
12201221
this.currentlyCreatedBean.set(beanName);

0 commit comments

Comments
 (0)