Skip to content
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

AOT fails when generating code for a component declared as an inner-class #33683

Closed
wilkinsona opened this issue Oct 11, 2024 · 1 comment
Closed
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) theme: aot An issue related to Ahead-of-time processing type: regression A bug that is also a regression
Milestone

Comments

@wilkinsona
Copy link
Member

Affects: 6.2.0-SNAPSHOT

This appears to be a regression from 6.2.0-RC1.

Compilation of AOT-generated code fails for this app:

package com.example.aot_regression;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;

@SpringBootApplication
public class AotRegressionApplication {

	public static void main(String[] args) {
		SpringApplication.run(AotRegressionApplication.class, args);
	}

	@Component
	class Foo {

	}

}

The failure is:

/Users/awilkinson/dev/temp/aot-regression/build/generated/aotSources/com/example/aot_regression/AotRegressionApplication__BeanDefinitions.java:35: error: an enclosing instance that contains AotRegressionApplication.Foo is require
              .withGenerator((registeredBean, args) -> new AotRegressionApplication.Foo(args.get(0)));

The generated source is:

package com.example.aot_regression;

import org.springframework.aot.generate.Generated;
import org.springframework.beans.factory.aot.BeanInstanceSupplier;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ConfigurationClassUtils;

/**
 * Bean definitions for {@link AotRegressionApplication}.
 */
@Generated
public class AotRegressionApplication__BeanDefinitions {
  /**
   * Get the bean definition for 'aotRegressionApplication'.
   */
  public static BeanDefinition getAotRegressionApplicationBeanDefinition() {
    RootBeanDefinition beanDefinition = new RootBeanDefinition(AotRegressionApplication$$SpringCGLIB$$0.class);
    beanDefinition.setTargetType(AotRegressionApplication.class);
    ConfigurationClassUtils.initializeConfigurationClass(AotRegressionApplication.class);
    beanDefinition.setInstanceSupplier(AotRegressionApplication$$SpringCGLIB$$0::new);
    return beanDefinition;
  }

  /**
   * Bean definitions for {@link AotRegressionApplication.Foo}.
   */
  @Generated
  public static class Foo {
    /**
     * Get the bean instance supplier for 'com.example.aot_regression.AotRegressionApplication$Foo'.
     */
    private static BeanInstanceSupplier<AotRegressionApplication.Foo> getFooInstanceSupplier() {
      return BeanInstanceSupplier.<AotRegressionApplication.Foo>forConstructor(AotRegressionApplication.class)
              .withGenerator((registeredBean, args) -> new AotRegressionApplication.Foo(args.get(0)));
    }

    /**
     * Get the bean definition for 'foo'.
     */
    public static BeanDefinition getFooBeanDefinition() {
      RootBeanDefinition beanDefinition = new RootBeanDefinition(AotRegressionApplication.Foo.class);
      beanDefinition.setInstanceSupplier(getFooInstanceSupplier());
      return beanDefinition;
    }
  }
}

Downgrading to Framework 6.2.0-RC1 fixes the problem as the generated source is then the following:

package com.example.aot_regression;

import org.springframework.aot.generate.Generated;
import org.springframework.beans.factory.aot.BeanInstanceSupplier;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ConfigurationClassUtils;

/**
 * Bean definitions for {@link AotRegressionApplication}.
 */
@Generated
public class AotRegressionApplication__BeanDefinitions {
  /**
   * Get the bean definition for 'aotRegressionApplication'.
   */
  public static BeanDefinition getAotRegressionApplicationBeanDefinition() {
    RootBeanDefinition beanDefinition = new RootBeanDefinition(AotRegressionApplication.class);
    beanDefinition.setTargetType(AotRegressionApplication.class);
    ConfigurationClassUtils.initializeConfigurationClass(AotRegressionApplication.class);
    beanDefinition.setInstanceSupplier(AotRegressionApplication$$SpringCGLIB$$0::new);
    return beanDefinition;
  }

  /**
   * Bean definitions for {@link AotRegressionApplication.Foo}.
   */
  @Generated
  public static class Foo {
    /**
     * Get the bean instance supplier for 'com.example.aot_regression.AotRegressionApplication$Foo'.
     */
    private static BeanInstanceSupplier<AotRegressionApplication.Foo> getFooInstanceSupplier() {
      return BeanInstanceSupplier.<AotRegressionApplication.Foo>forConstructor()
              .withGenerator((registeredBean, args) -> registeredBean.getBeanFactory().getBean(AotRegressionApplication.class).new Foo());
    }

    /**
     * Get the bean definition for 'foo'.
     */
    public static BeanDefinition getFooBeanDefinition() {
      RootBeanDefinition beanDefinition = new RootBeanDefinition(AotRegressionApplication.Foo.class);
      beanDefinition.setInstanceSupplier(getFooInstanceSupplier());
      return beanDefinition;
    }
  }
}

Note the change in getFooInstanceSupplier from new AotRegressionApplication.Foo(args.get(0)) which fails to compile as Foo is not static to registeredBean.getBeanFactory().getBean(AotRegressionApplication.class).new Foo()) which works.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Oct 11, 2024
@wilkinsona
Copy link
Member Author

I suspect d6e4bd7 may be the cause. Its commit message says "removes support for inner classes in alignment with standard core container behavior". The app above works fine without AOT with an instance of Foo being made available as a bean.

@jhoeller jhoeller added type: regression A bug that is also a regression in: core Issues in core modules (aop, beans, core, context, expression) theme: aot An issue related to Ahead-of-time processing and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Oct 11, 2024
@jhoeller jhoeller self-assigned this Oct 11, 2024
@jhoeller jhoeller added this to the 6.2.0-RC2 milestone Oct 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) theme: aot An issue related to Ahead-of-time processing type: regression A bug that is also a regression
Projects
None yet
Development

No branches or pull requests

3 participants