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.
BeanUtils.instantiateClassis 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.
0forintandfalseforboolean). With Kotlin, you have to passnullas the parameter may have a default value that we shouldn't be overriding.Consider the following example:
Let's assume we have a value for the
valueparameter and no information for thecounterorflag.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
nullto 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.