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

Add AOT support for Kotlin constructors with optional parameters #29820

Closed
DarkAtra opened this issue Jan 14, 2023 · 2 comments
Closed

Add AOT support for Kotlin constructors with optional parameters #29820

DarkAtra opened this issue Jan 14, 2023 · 2 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) theme: aot An issue related to Ahead-of-time processing theme: kotlin An issue related to Kotlin support type: enhancement A general enhancement
Milestone

Comments

@DarkAtra
Copy link

DarkAtra commented Jan 14, 2023

Take the following kotlin @Service:

@Service
class Service(
    private val applicationContext: ApplicationContext,
    private val client: Client = Client()
)

When running on JVM the code behaves as expected: only the constructor parameter without a default value is autowired.
When running the native image the application does not start and throws the following exception indicating that spring
attempts to autowire all constructor parameters (even the one with a default value):

java.lang.NullPointerException: Parameter specified as non-null is null: method de.darkatra.kotlindiissue.Service.<init>, parameter client

Reproducer with details: https://github.com/DarkAtra/spring-native-kotlin-di-issue
Affected Spring Framework versions: 6.0.2, 6.0.3, 6.0.4 (verified by building the current spring-boot main branch locally)

@DarkAtra DarkAtra changed the title NullPointerException when injecting dependencies into a bean that uses constructor parameters with kotlins default values in native-image NullPointerException when injecting dependencies into a bean that uses constructor with default values in native-image Jan 14, 2023
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jan 14, 2023
@sdeleuze sdeleuze added theme: aot An issue related to Ahead-of-time processing theme: kotlin An issue related to Kotlin support labels Jan 19, 2023
@sdeleuze sdeleuze self-assigned this Jan 19, 2023
@sdeleuze sdeleuze added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jan 19, 2023
@sdeleuze sdeleuze added this to the 6.0.5 milestone Jan 19, 2023
@sdeleuze
Copy link
Contributor

Tricky one!

In Kotlin interoperability documentation, we can read:

Normally, if you write a Kotlin function with default parameter values, it will be visible in Java only as a full signature, with all parameters present. If you wish to expose multiple overloads to Java callers, you can use the @JvmOverloads annotation.

So I think our best (only ?) chance is to invoke BeanUtils#instantiateClass(java.lang.reflect.Constructor<T>, java.lang.Object...) and add invoke hint for the constructor for Kotlin constructors with default values.

@sdeleuze sdeleuze modified the milestones: 6.0.5, 6.0.6 Feb 14, 2023
@jhoeller jhoeller added the in: core Issues in core modules (aop, beans, core, context, expression) label Feb 28, 2023
@sdeleuze sdeleuze modified the milestones: 6.0.6, 6.0.7 Mar 1, 2023
@sdeleuze
Copy link
Contributor

sdeleuze commented Mar 6, 2023

Regrouping default parameter value in Spring Framework 6.1 along to #21139.

@sdeleuze sdeleuze modified the milestones: 6.0.7, 6.1.0-M1 Mar 6, 2023
@sdeleuze sdeleuze modified the milestones: 6.1.0-M1, 6.1.0-M2 Jun 12, 2023
@sdeleuze sdeleuze changed the title NullPointerException when injecting dependencies into a bean that uses constructor with default values in native-image Add AOT support for Kotlin constructors with optional parameters Jul 7, 2023
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 theme: kotlin An issue related to Kotlin support type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

4 participants