Fix AOT code generation for autowired inner class constructor
Backport Bot / build (push) Waiting to run Details
Build and Deploy Snapshot / Build and Deploy Snapshot (push) Waiting to run Details
Build and Deploy Snapshot / Verify (push) Blocked by required conditions Details
CI / ${{ matrix.os.name}} | Java ${{ matrix.java.version}} (map[toolchain:false version:17], map[id:ubuntu-latest name:Linux]) (push) Waiting to run Details
CI / ${{ matrix.os.name}} | Java ${{ matrix.java.version}} (map[toolchain:true version:21], map[id:ubuntu-latest name:Linux]) (push) Waiting to run Details
CI / ${{ matrix.os.name}} | Java ${{ matrix.java.version}} (map[toolchain:true version:23], map[id:ubuntu-latest name:Linux]) (push) Waiting to run Details
Deploy Docs / Dispatch docs deployment (push) Waiting to run Details

Prior to this commit, argument index handling in
AutowiredArgumentsCodeGenerator suffered from an off-by-one error when
generating code for an autowired inner class constructor.

Since the startIndex is already properly calculated for an inner class in
InstanceSupplierCodeGenerator.buildGetInstanceMethodForConstructor(...),
there is no need to adjust the argument indexes within
AutowiredArgumentsCodeGenerator.generateCode(...).

Closes gh-34974

Signed-off-by: Dmytro Nosan <dimanosan@gmail.com>
This commit is contained in:
Dmytro Nosan 2025-05-31 16:25:24 +03:00 committed by Sam Brannen
parent f207c0ed5c
commit c2d678879f
4 changed files with 19 additions and 9 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2024 the original author or authors. * Copyright 2002-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -68,10 +68,10 @@ public class AutowiredArgumentsCodeGenerator {
for (int i = startIndex; i < parameterTypes.length; i++) { for (int i = startIndex; i < parameterTypes.length; i++) {
code.add(i > startIndex ? ", " : ""); code.add(i > startIndex ? ", " : "");
if (!ambiguous) { if (!ambiguous) {
code.add("$L.get($L)", variableName, i - startIndex); code.add("$L.get($L)", variableName, i);
} }
else { else {
code.add("$L.get($L, $T.class)", variableName, i - startIndex, parameterTypes[i]); code.add("$L.get($L, $T.class)", variableName, i, parameterTypes[i]);
} }
} }
return code.build(); return code.build();

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2022 the original author or authors. * Copyright 2002-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -67,7 +67,7 @@ class AutowiredArgumentsCodeGeneratorTests {
AutowiredArgumentsCodeGenerator generator = new AutowiredArgumentsCodeGenerator( AutowiredArgumentsCodeGenerator generator = new AutowiredArgumentsCodeGenerator(
Outer.Nested.class, constructor); Outer.Nested.class, constructor);
assertThat(generator.generateCode(constructor.getParameterTypes(), 1)) assertThat(generator.generateCode(constructor.getParameterTypes(), 1))
.hasToString("args.get(0), args.get(1)"); .hasToString("args.get(1), args.get(2)");
} }
@Test @Test

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2024 the original author or authors. * Copyright 2002-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -133,11 +133,13 @@ class InstanceSupplierCodeGeneratorTests {
@Test @Test
void generateWhenHasConstructorWithInnerClassAndParameter() { void generateWhenHasConstructorWithInnerClassAndParameter() {
BeanDefinition beanDefinition = new RootBeanDefinition(EnvironmentAwareComponent.class); BeanDefinition beanDefinition = new RootBeanDefinition(EnvironmentAwareComponent.class);
StandardEnvironment environment = new StandardEnvironment();
this.beanFactory.registerSingleton("configuration", new InnerComponentConfiguration()); this.beanFactory.registerSingleton("configuration", new InnerComponentConfiguration());
this.beanFactory.registerSingleton("environment", new StandardEnvironment()); this.beanFactory.registerSingleton("environment", environment);
compile(beanDefinition, (instanceSupplier, compiled) -> { compile(beanDefinition, (instanceSupplier, compiled) -> {
Object bean = getBean(beanDefinition, instanceSupplier); Object bean = getBean(beanDefinition, instanceSupplier);
assertThat(bean).isInstanceOf(EnvironmentAwareComponent.class); assertThat(bean).isInstanceOf(EnvironmentAwareComponent.class);
assertThat(bean).hasFieldOrPropertyWithValue("environment", environment);
assertThat(compiled.getSourceFile()).contains( assertThat(compiled.getSourceFile()).contains(
"getBeanFactory().getBean(InnerComponentConfiguration.class).new EnvironmentAwareComponent("); "getBeanFactory().getBean(InnerComponentConfiguration.class).new EnvironmentAwareComponent(");
}); });
@ -162,11 +164,13 @@ class InstanceSupplierCodeGeneratorTests {
@Test @Test
void generateWhenHasNonPublicConstructorWithInnerClassAndParameter() { void generateWhenHasNonPublicConstructorWithInnerClassAndParameter() {
BeanDefinition beanDefinition = new RootBeanDefinition(EnvironmentAwareComponentWithoutPublicConstructor.class); BeanDefinition beanDefinition = new RootBeanDefinition(EnvironmentAwareComponentWithoutPublicConstructor.class);
StandardEnvironment environment = new StandardEnvironment();
this.beanFactory.registerSingleton("configuration", new InnerComponentConfiguration()); this.beanFactory.registerSingleton("configuration", new InnerComponentConfiguration());
this.beanFactory.registerSingleton("environment", new StandardEnvironment()); this.beanFactory.registerSingleton("environment", environment);
compile(beanDefinition, (instanceSupplier, compiled) -> { compile(beanDefinition, (instanceSupplier, compiled) -> {
Object bean = getBean(beanDefinition, instanceSupplier); Object bean = getBean(beanDefinition, instanceSupplier);
assertThat(bean).isInstanceOf(EnvironmentAwareComponentWithoutPublicConstructor.class); assertThat(bean).isInstanceOf(EnvironmentAwareComponentWithoutPublicConstructor.class);
assertThat(bean).hasFieldOrPropertyWithValue("environment", environment);
assertThat(compiled.getSourceFile()).doesNotContain( assertThat(compiled.getSourceFile()).doesNotContain(
"getBeanFactory().getBean(InnerComponentConfiguration.class)"); "getBeanFactory().getBean(InnerComponentConfiguration.class)");
}); });

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2024 the original author or authors. * Copyright 2002-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -28,7 +28,10 @@ public class InnerComponentConfiguration {
public class EnvironmentAwareComponent { public class EnvironmentAwareComponent {
final Environment environment;
public EnvironmentAwareComponent(Environment environment) { public EnvironmentAwareComponent(Environment environment) {
this.environment = environment;
} }
} }
@ -40,7 +43,10 @@ public class InnerComponentConfiguration {
public class EnvironmentAwareComponentWithoutPublicConstructor { public class EnvironmentAwareComponentWithoutPublicConstructor {
final Environment environment;
EnvironmentAwareComponentWithoutPublicConstructor(Environment environment) { EnvironmentAwareComponentWithoutPublicConstructor(Environment environment) {
this.environment = environment;
} }
} }