Use isAssignableValue instead of isInstanceOf for argument resolution

Includes related nullability refinements.

Closes gh-28727
This commit is contained in:
Juergen Hoeller 2022-06-29 17:01:04 +02:00
parent 14fd2605a3
commit a21b27e6d9
2 changed files with 11 additions and 5 deletions

View File

@ -18,6 +18,7 @@ package org.springframework.beans.factory.aot;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
/**
* Resolved arguments to be autowired.
@ -38,11 +39,14 @@ public interface AutowiredArguments {
* @param requiredType the required argument type
* @return the argument
*/
@Nullable
@SuppressWarnings("unchecked")
@Nullable
default <T> T get(int index, Class<T> requiredType) {
Object value = get(index);
Assert.isInstanceOf(requiredType, value);
Object value = getObject(index);
if (!ClassUtils.isAssignableValue(requiredType, value)) {
throw new IllegalArgumentException("Argument type mismatch: expected '" +
ClassUtils.getQualifiedName(requiredType) + "' for value [" + value + "]");
}
return (T) value;
}
@ -52,10 +56,10 @@ public interface AutowiredArguments {
* @param index the argument index
* @return the argument
*/
@Nullable
@SuppressWarnings("unchecked")
@Nullable
default <T> T get(int index) {
return (T) toArray()[index];
return (T) getObject(index);
}
/**
@ -63,6 +67,7 @@ public interface AutowiredArguments {
* @param index the argument index
* @return the argument
*/
@Nullable
default Object getObject(int index) {
return toArray()[index];
}

View File

@ -1215,6 +1215,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
return bw;
}
@Nullable
private Object obtainInstanceFromSupplier(Supplier<?> supplier, String beanName) {
String outerBean = this.currentlyCreatedBean.get();
this.currentlyCreatedBean.set(beanName);