-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Calling introspected methods / constructors with default arguments in Kotlin #4379
Comments
Maybe answer to my question on SO can be helpful, maybe not. I will add it here for reference. |
Bad thing about default values in kotlin is that they are not stored somewhere and can't be obtained from Metadata annotations. |
Just for information for others wanting this feature, I resolved this by disabling Jackson introspection module: jackson:
bean-introspection-module: false For native image to work properly, adding Kotlin metadata resources and some reflection configuration solved it nicely. reflect-config.json [
{
"name": "java.util.List"
},
{
"name": "java.util.Set"
},
{
"name": "java.util.Map"
},
{
"name": "java.lang.String"
},
{
"name": "kotlin.Metadata",
"allDeclaredMethods": true
},
{
"name": "kotlin.KotlinVersion",
"allDeclaredFields": true,
"allDeclaredMethods": true,
"allDeclaredConstructors": true
},
{
"name": "kotlin.KotlinVersion[]"
},
{
"name": "kotlin.KotlinVersion$Companion"
},
{
"name": "kotlin.KotlinVersion$Companion[]"
},
{
"name": "kotlin.jvm.internal.DefaultConstructorMarker"
},
{
"name": "kotlin.reflect.jvm.internal.ReflectionFactoryImpl",
"allDeclaredConstructors": true
}
] resource-config.json {
"resources": {
"includes": [
{
"pattern": "META-INF/.*.kotlin_module$"
},
{
"pattern": ".*.kotlin_builtins"
}
]
}
} Source here: oracle/graal#2306 (comment) It is now integrated to Spring, see comment below. Can we make this contribution to |
Sure PRs, welcome. Might want to combine it with micronaut-projects/micronaut-kotlin#163 (comment) |
Thank you @graemerocher, will provide PR. I have another questions, if you like to help me with answer. Now I combine this with already provided Would you think it is ok (useless classes) or shall we create another annotation, which will only do reflection registration and not introspection? |
probably it makes sense to allow |
Note that the real solution is to support default argument values in the AST but I am not sure if that is possible with KAPT |
Default argument values are not presented in kotlin reflection metadata (see @Flassie comment), and can contain references to themselves (see example), so I think it is not doable this way. But I will be happy if I am wrong. Example: data class Test(
val first: Int = 8,
val second: String = "I contain $first",
) |
Not only. It can contain anything:
Kotlin generates only stubs for that and there is nothing interesting inside them
So yes. I think it is not doable using kapt |
Hi, I just found little bit annoying to workaround missing support for kotlin default parameters in binding query parameters.
Instead of using this:
I have to define auxiliary getters like:
It gets very complicated with many arguments and little bit unreadable.
I have read that default arguments are not available for reflection, because they can be arbitrary expressions.
But I thought that maybe it can be solved differently using this approach I have found on SO:
https://stackoverflow.com/questions/44792787/reflectively-calling-function-and-using-default-parameters
What do you think? Will it work in native image? Maybe with some relflection configuration...
It is just an idea to discuss, if you have another less verbose alternative it would be awesome.
EDIT: Kotlin module for Jackson is using the same approach:
https://github.com/FasterXML/jackson-module-kotlin/blob/130f0944be72d5dc3d3c88cd76abddb6dc9359a7/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinValueInstantiator.kt#L171
EDIT 2: When using default values in body deserialization (JSON), it works only if
@Introspected
annotation is not used, othewise it throws following exception:Failed to convert argument [body] for value [null] due to: Missing required creator property
.The text was updated successfully, but these errors were encountered: