Skip to content

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

@wilkinsona

Description

@wilkinsona

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.

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)theme: aotAn issue related to Ahead-of-time processingtype: regressionA bug that is also a regression

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions