Skip to content

Fix AOT code generation for autowired inner class constructor #34974

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -68,10 +68,10 @@ public CodeBlock generateCode(Class<?>[] parameterTypes, int startIndex, String
for (int i = startIndex; i < parameterTypes.length; i++) {
code.add(i > startIndex ? ", " : "");
if (!ambiguous) {
code.add("$L.get($L)", variableName, i - startIndex);
code.add("$L.get($L)", variableName, i);
}
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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -67,7 +67,7 @@ void generateCodeWhenMultipleArgumentsWithOffset() {
AutowiredArgumentsCodeGenerator generator = new AutowiredArgumentsCodeGenerator(
Outer.Nested.class, constructor);
assertThat(generator.generateCode(constructor.getParameterTypes(), 1))
.hasToString("args.get(0), args.get(1)");
.hasToString("args.get(1), args.get(2)");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -130,11 +130,13 @@ void generateWhenHasConstructorWithInnerClassAndDefaultConstructor() {
@Test
void generateWhenHasConstructorWithInnerClassAndParameter() {
BeanDefinition beanDefinition = new RootBeanDefinition(EnvironmentAwareComponent.class);
StandardEnvironment environment = new StandardEnvironment();
this.beanFactory.registerSingleton("configuration", new InnerComponentConfiguration());
this.beanFactory.registerSingleton("environment", new StandardEnvironment());
this.beanFactory.registerSingleton("environment", environment);
compile(beanDefinition, (instanceSupplier, compiled) -> {
Object bean = getBean(beanDefinition, instanceSupplier);
assertThat(bean).isInstanceOf(EnvironmentAwareComponent.class);
assertThat(bean).hasFieldOrPropertyWithValue("environment", environment);
assertThat(compiled.getSourceFile()).contains(
"getBeanFactory().getBean(InnerComponentConfiguration.class).new EnvironmentAwareComponent(");
});
Expand All @@ -158,11 +160,13 @@ void generateWhenHasNonPublicConstructorWithInnerClassAndDefaultConstructor() {
@Test
void generateWhenHasNonPublicConstructorWithInnerClassAndParameter() {
BeanDefinition beanDefinition = new RootBeanDefinition(EnvironmentAwareComponentWithoutPublicConstructor.class);
StandardEnvironment environment = new StandardEnvironment();
this.beanFactory.registerSingleton("configuration", new InnerComponentConfiguration());
this.beanFactory.registerSingleton("environment", new StandardEnvironment());
this.beanFactory.registerSingleton("environment", environment);
compile(beanDefinition, (instanceSupplier, compiled) -> {
Object bean = getBean(beanDefinition, instanceSupplier);
assertThat(bean).isInstanceOf(EnvironmentAwareComponentWithoutPublicConstructor.class);
assertThat(bean).hasFieldOrPropertyWithValue("environment", environment);
assertThat(compiled.getSourceFile()).doesNotContain(
"getBeanFactory().getBean(InnerComponentConfiguration.class)");
});
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,7 +28,10 @@ public NoDependencyComponent() {

public class EnvironmentAwareComponent {

private final Environment environment;

public EnvironmentAwareComponent(Environment environment) {
this.environment = environment;
}
}

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

public class EnvironmentAwareComponentWithoutPublicConstructor {

private final Environment environment;

EnvironmentAwareComponentWithoutPublicConstructor(Environment environment) {
this.environment = environment;
}
}

Expand Down
Loading