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

Kotlin constructor parameters with default break the transformer #374

Open
giambeppo opened this issue Dec 21, 2022 · 0 comments
Open

Kotlin constructor parameters with default break the transformer #374

giambeppo opened this issue Dec 21, 2022 · 0 comments
Labels
good first issue Good for newcomers

Comments

@giambeppo
Copy link

Sample code:

import com.expediagroup.beans.BeanUtils

data class BeanFrom(
    val foo: String
)

data class BeanTo(
    val foo: String = "bar"
)

val beanFrom = BeanFrom("bar")
val beanTo: BeanTo = BeanUtils().transformer.transform(beanFrom, BeanTo::class.java)

This throws:

com.expediagroup.transformer.error.InvalidBeanException: Constructor's parameters name have been removed from the compiled code. This caused a problems during the: BeanTo injection. Consider to use: @ConstructorArg annotation: https://github.com/ExpediaGroup/bull#different-field-names-defining-constructor-args or add the property: <parameters>true</parameters> to your maven-compiler configuration
	at com.expediagroup.beans.transformer.TransformerImpl.handleInjectionException(TransformerImpl.java:201)
	at com.expediagroup.beans.transformer.TransformerImpl.injectValues(TransformerImpl.java:164)
	at com.expediagroup.beans.transformer.TransformerImpl.handleInjectionException(TransformerImpl.java:188)
	at com.expediagroup.beans.transformer.TransformerImpl.injectValues(TransformerImpl.java:164)
	at com.expediagroup.beans.transformer.TransformerImpl.injectValues(TransformerImpl.java:134)
	at com.expediagroup.beans.transformer.TransformerImpl.transform(TransformerImpl.java:69)
	at com.expediagroup.beans.transformer.AbstractBeanTransformer.transform(AbstractBeanTransformer.java:124)
	at Line_4.<init>(Line_4.kts:1)
Caused by: com.expediagroup.transformer.error.InstanceCreationException
	at com.expediagroup.transformer.utils.ClassUtils.getInstance(ClassUtils.java:548)
	at com.expediagroup.beans.transformer.TransformerImpl.injectValues(TransformerImpl.java:162)
Caused by: java.lang.reflect.InvocationTargetException
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
	at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
	at com.expediagroup.transformer.utils.ClassUtils.getInstance(ClassUtils.java:546)
Caused by: java.lang.NullPointerException: Parameter specified as non-null is null: method Line_2$BeanTo.<init>, parameter foo
	at Line_2$BeanTo.<init>(Line_2.kts)
	at Line_2$BeanTo.<init>(Line_2.kts:1)

The initial message is probably misleading. What happens here is that Kotlin creates an additional constructor with two more parameters (an int and a kotlin.jvm.internal.DefaultConstructorMarker), and BULL assumes that's the "all args" one.
That fails due to receiving the wrong number of arguments, and so BULL falls back to using the no args constructor and injecting fields.
But the no args constructor cannot be used, because the class expects a non-null value for its only field, and that yields the last exception you can see in the stack trace.

@fborriello fborriello added the good first issue Good for newcomers label Dec 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants