spring-projects/spring-framework · error · IllegalArgumentException
Argument type mismatch: expected '
Error message
Argument type mismatch: expected '
What it means
Thrown by AutowiredArguments.get(int index, Class<T> requiredType) when the resolved argument at the given index is not assignable to the requested type. AutowiredArguments is part of the AOT (Ahead-of-Time) processing infrastructure (since Spring 6.0); it represents pre-resolved constructor or method arguments used by BeanInstanceSupplier in native/GraalVM scenarios. The type check at line 46 uses ClassUtils.isAssignableValue().
Source
Thrown at spring-beans/src/main/java/org/springframework/beans/factory/aot/AutowiredArguments.java:47
* @since 6.0
* @see BeanInstanceSupplier
* @see AutowiredMethodArgumentsResolver
*/
@FunctionalInterface
public interface AutowiredArguments {
/**
* Return the resolved argument at the specified index.
* @param <T> the type of the argument
* @param index the argument index
* @param requiredType the required argument type
* @return the argument
*/
@SuppressWarnings("unchecked")
default <T> @Nullable T get(int index, Class<T> requiredType) {
Object value = getObject(index);
if (!ClassUtils.isAssignableValue(requiredType, value)) {
throw new IllegalArgumentException("Argument type mismatch: expected '" +
requiredType.getTypeName() + "' for value [" + value + "]");
}
return (T) value;
}
/**
* Return the resolved argument at the specified index.
* @param <T> the type of the argument
* @param index the argument index
* @return the argument
*/
@SuppressWarnings("unchecked")
default <T> @Nullable T get(int index) {
return (T) getObject(index);
}
/**
* Return the resolved argument at the specified index.View on GitHub (pinned to 69bf83ad71)
Solutions
- Ensure the AutowiredArguments array passed to BeanInstanceSupplier.of() contains values whose types match the constructor/method parameter types in order.
- Use the type-unsafe get(int index) overload if you are certain of the type and want to avoid the check, then cast manually.
- Regenerate AOT artifacts (clean and rebuild) if the bean signatures have changed to eliminate stale generated code.
Example fix
// before
AutowiredArguments args = AutowiredArguments.of(
new Object[]{ "not-a-number" });
Integer value = args.get(0, Integer.class); // throws
// after
AutowiredArguments args = AutowiredArguments.of(
new Object[]{ 42 });
Integer value = args.get(0, Integer.class); // ok Defensive patterns
Strategy: type-guard
Validate before calling
// Before calling get(index, type), check assignability
Object raw = args.toArray()[index];
if (raw != null && !requiredType.isAssignableFrom(raw.getClass())) {
throw new IllegalStateException(
"Argument at index " + index + " is " + raw.getClass()
+ " not " + requiredType);
}
T result = requiredType.cast(raw); Type guard
// Type guard for AutowiredArguments
static <T> boolean isArgumentType(
AutowiredArguments args, int index, Class<T> requiredType) {
Object value = args.getObject(index);
return value == null || requiredType.isAssignableFrom(value.getClass());
} Try / catch
try {
T value = args.get(index, requiredType);
} catch (IllegalArgumentException ex) {
// message: "Argument type mismatch: expected '...'"
Object actual = args.getObject(index);
// handle: regenerate AOT code, fix argument resolution, or cast manually
} Prevention
- Ensure AutowiredArguments arrays are constructed with values whose types match declared parameters.
- Regenerate AOT artifacts (mvn clean + spring-boot:process-aot) after changing bean constructors.
- Use the type-unsafe get(int index) overload with manual casting if types are known and stable.
When it happens
Trigger: Calling autowiredArguments.get(0, SomeType.class) where the actual resolved argument value's runtime type is not assignable to SomeType. This is typically triggered by AOT-generated code or BeanInstanceSupplier where the pre-resolved arguments array was constructed with values whose types don't match the declared parameter types.
Common situations: AOT processing mismatch where the generated BeanInstanceSupplier passes arguments of the wrong type. Custom BeanRegistrationAotProcessor producing incorrect AutowiredArguments. Spring Boot native image compilation where bean definitions changed between AOT and runtime. Generic type erasure causing an assignability check to fail at runtime.
Related errors
- Invalid value [{}] for custom qualifier type: needs to be Cl
- Specified field type [
- Specified parameter type [
- Unsatisfied dependency expressed through field
- Unsatisfied dependency expressed through parameter
AI-assisted analysis of spring-projects/spring-framework@69bf83ad71 (2026-08-09).
Data as JSON: /api/errors/7f11ec4794ab53f0.
Report an issue: GitHub.