8000 Fix argument index handling in AutowiredArgumentsCodeGenerator · spring-projects/spring-framework@990218b · GitHub
[go: up one dir, main page]

Skip to content

Commit 990218b

Browse files
committed
Fix argument index handling in AutowiredArgumentsCodeGenerator
Signed-off-by: Dmytro Nosan <dimanosan@gmail.com>
1 parent 5e9f90f commit 990218b

File tree

4 files changed

+19
-9
lines changed

4 files changed

+19
-9
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -68,10 +68,10 @@ public CodeBlock generateCode(Class<?>[] parameterTypes, int startIndex, String
6868
for (int i = startIndex; i < parameterTypes.length; i++) {
6969
code.add(i > startIndex ? ", " : "");
7070
if (!ambiguous) {
71-
code.add("$L.get($L)", variableName, i - startIndex);
71+
code.add("$L.get($L)", variableName, i);
7272
}
7373
else {
74-
code.add("$L.get($L, $T.class)", variableName, i - startIndex, parameterTypes[i]);
74+
code.add("$L.get($L, $T.class)", variableName, i, parameterTypes[i]);
7575
}
7676
}
7777
return code.build();

spring-beans/src/test/java/org/springframework/beans/factory/aot/AutowiredArgumentsCodeGeneratorTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -67,7 +67,7 @@ void generateCodeWhenMultipleArgumentsWithOffset() {
6767
AutowiredArgumentsCodeGenerator generator = new AutowiredArgumentsCodeGenerator(
6868
Outer.Nested.class, constructor);
6969
assertThat(generator.generateCode(constructor.getParameterTypes(), 1))
70-
.hasToString("args.get(0), args.get(1)");
70+
.hasToString("args.get(1), args.get(2)");
7171
}
7272

7373
@Test

spring-beans/src/test/java/org/springframework/beans/factory/aot/InstanceSupplierCodeGeneratorTests.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -130,11 +130,13 @@ void generateWhenHasConstructorWithInnerClassAndDefaultConstructor() {
130130
@Test
131131
void generateWhenHasConstructorWithInnerClassAndParameter() {
132132
BeanDefinition beanDefinition = new RootBeanDefinition(EnvironmentAwareComponent.class);
133+
StandardEnvironment environment = new StandardEnvironment();
133134
this.beanFactory.registerSingleton("configuration", new InnerComponentConfiguration());
134-
this.beanFactory.registerSingleton("environment", new StandardEnvironment());
135+
this.beanFactory.registerSingleton("environment", environment);
135136
compile(beanDefinition, (instanceSupplier, compiled) -> {
136137
Object bean = getBean(beanDefinition, instanceSupplier);
137138
assertThat(bean).isInstanceOf(EnvironmentAwareComponent.class);
139+
assertThat(bean).hasFieldOrPropertyWithValue("environment", environment);
138140
assertThat(compiled.getSourceFile()).contains(
139141
"getBeanFactory().getBean(InnerComponentConfiguration.class).new EnvironmentAwareComponent(");
140142
});
@@ -158,11 +160,13 @@ void generateWhenHasNonPublicConstructorWithInnerClassAndDefaultConstructor() {
158160
@Test
159161
void generateWhenHasNonPublicConstructorWithInnerClassAndParameter() {
160162
BeanDefinition beanDefinition = new RootBeanDefinition(EnvironmentAwareComponentWithoutPublicConstructor.class);
163+
StandardEnvironment environment = new StandardEnvironment();
161164
this.beanFactory.registerSingleton("configuration", new InnerComponentConfiguration());
162-
this.beanFactory.registerSingleton("environment", new StandardEnvironment());
165+
this.beanFactory.registerSingleton("environment", environment);
163166
compile(beanDefinition, (instanceSupplier, compiled) -> {
164167
Object bean = getBean(beanDefinition, instanceSupplier);
165168
assertThat(bean).isInstanceOf(EnvironmentAwareComponentWithoutPublicConstructor.class);
169+
assertThat(bean).hasFieldOrPropertyWithValue("environment", environment);
166170
assertThat(compiled.getSourceFile()).doesNotContain(
167171
"getBeanFactory().getBean(InnerComponentConfiguration.class)");
168172
});
8000

spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/factory/generator/InnerComponentConfiguration.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,7 +28,10 @@ public NoDependencyComponent() {
2828

2929
public class EnvironmentAwareComponent {
3030

31+
private final Environment environment;
32+
3133
public EnvironmentAwareComponent(Environment environment) {
34+
this.environment = environment;
3235
}
3336
}
3437

@@ -40,7 +43,10 @@ public class NoDependencyComponentWithoutPublicConstructor {
4043

4144
public class EnvironmentAwareComponentWithoutPublicConstructor {
4245

46+
private final Environment environment;
47+
4348
EnvironmentAwareComponentWithoutPublicConstructor(Environment environment) {
49+
this.environment = environment;
4450
}
4551
}
4652

0 commit comments

Comments
 (0)
0