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

Revisit BeanUtils.instantiateClass to work consistently with Kotlin and Java classes #22531

Closed
snicoll opened this issue Mar 6, 2019 · 3 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Milestone

Comments

@snicoll
Copy link
Member

snicoll commented Mar 6, 2019

BeanUtils.instantiateClass is an interesting way to create a class as it takes care of kotlin features behind the scenes for you. There is a slightly mismatch with Java though.

If a parameter is not available for a primitive type, we need to specify the default value for the primitive type (i.e. 0 for int and false for boolean). With Kotlin, you have to pass null as the parameter may have a default value that we shouldn't be overriding.

Consider the following example:

class JavaExample {

  JavaExample(int counter, boolean flag, String value) { ... }
}
class KotlinExample(val counter : Int = 0, val flag: Boolean = false, val value : String?) { ... }

Let's assume we have a value for the value parameter and no information for the counter or flag.

Java requires us to provide [0, false, "my value"]
Kotlin requires us to provide [null, ,null, "my value"]

An improvement would be to provide only the latter and let the internal implementation translates null to the proper default value if the parameter is primitive.

This will benefit spring-projects/spring-boot#8762 where we're currently computing the array differently depending on the fact the target type is kotlin or not.

@jhoeller jhoeller added type: enhancement A general enhancement in: core Issues in core modules (aop, beans, core, context, expression) labels Mar 6, 2019
@jhoeller jhoeller added this to the 5.2 M1 milestone Mar 6, 2019
@sdeleuze
Copy link
Contributor

sdeleuze commented Mar 7, 2019

We can probably get an even more consistent behavior between Java and Kotlin by leveraging KParameter.isOptional to set the default value of Kotlin equivalent of Java's primitive types when no default value is provided.

@snicoll
Copy link
Member Author

snicoll commented Mar 7, 2019

Indeed, thanks for following-up. In the case of my example, this would mean that the following declaration works consistently:

class KotlinExample(val counter : Int, val flag: Boolean, val value : String?) { ... }

@sdeleuze
Copy link
Contributor

Fixed on master, KParameter.isOptional was already used on Kotlin side so I just added support for Java primitive types.

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) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants