-
Notifications
You must be signed in to change notification settings - Fork 38.4k
Add support for Kotlin inline/value classes in BeanUtils
#28638
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
Comments
Thanks for the report. The problem can be reproduced without Spring Boot: package com.example.gh31398
import org.springframework.beans.BeanUtils
class Gh31398Application
fun main(args: Array<String>) {
val constructor = PredicateConfiguration::class.java.constructors[0]
val args = arrayOf(Character.valueOf('c'))
BeanUtils.instantiateClass(constructor, *args)
}
@JvmInline
value class Predicate(val value: Char)
data class PredicateConfiguration(val predicate: Predicate)
I'm not sure if this is something that can be addressed in Framework's support for instantiating Kotlin types or if it's a bug in Kotlin's reflection support. We'll transfer this to the Framework team so that they can take a look. |
I don't think we support Kotlin inline classes yet in Spring portfolio as such support is not straightforward from a JVM bytecode reflection POV and we lack a global abstraction for Kotlin reflection. We had related discussion with the Spring Data team that shown it can be tricky topic even if this specific case is maybe doable without too much trouble. Maybe we should at least document at Framework level it is not supported yet. I will create a related documentation issue. |
BeanUtils
Hello team - can support for Kotlin inline classes be expected in the near future? :) |
No promise, but let's try to tackle that as part of 6.1.0-M3. See related spring-projects/spring-data-commons#2866 Spring Data PR. |
See related comments from @udalov in spring-projects/spring-data-commons#2866 (review). |
This commit adds more Kotlin value classes tests with primitive types to ensure testing unwrapped use case. See spring-projectsgh-28638
Does this enhancement mean Kotlin value classes can be used in 6.1+ to disambiguate For example: @JvmInline
value class LogsPath(val value: Path)
@JvmInline
value class PrefsPath(val value: Path)
@Configuration
class ApplicationConfiguration {
@Bean
fun logsPathSupplier(): Supplier<LogsPath> = ...
@Bean
fun prefsPathSupplier() : Supplier<PrefsPath> = ...
@Bean
fun logService(logsPath: Supplier<LogsPath>) = ...
@Bean
fun prefsService(prefsPath: Supplier<PrefsPath>) = ...
} If not, I'd file that as an additional feature request. I'm currently using Also, just a heads up regarding some docs that may need to be adjusted for this change, https://docs.spring.io/spring-framework/reference/6.1/languages/kotlin/requirements.html currently has a blanket warning that, "Kotlin inline classes are not yet supported." |
I tested it, and it looks like the answer to my above question is yes: sdkotlin/sd-kotlin-spring-talks@b6fef06 I would foresee disambiguating multiple beans of the same type with value classes instead of bean names in most cases. Thanks! |
Oh nice, been waiting for this one, thanks for testing. |
I stand corrected. It works for me with value classes as generic type parameters or function type return types (e.g. I filed a separate issue with reproducer: #31372. |
This issue has been closed, but I have tried using Andy Wilkinson's reproducer and it failed for me exactly with the message mentioned by Andy. I've tried it with:
|
Kotlin value classes currently cannot be used in configuration bindings in Spring Boot 2.6.6.
This is a repro case:
It fails with the following error message:
The text was updated successfully, but these errors were encountered: