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

KoltinModule cannot be created using createInstance() #291

Closed
angryziber opened this issue Jan 18, 2020 · 5 comments
Closed

KoltinModule cannot be created using createInstance() #291

angryziber opened this issue Jan 18, 2020 · 5 comments
Assignees

Comments

@angryziber
Copy link

KotlinModule::class.createInstance()

Fails in runtime with:

Exception in thread "main" java.lang.IllegalArgumentException: Class should have a single no-arg constructor: class com.fasterxml.jackson.module.kotlin.KotlinModule

If I create exactly the same class declaration in my own code (with default parameters), then my class works with KClass.createInstance(). It's probably an issue with some non-default compiler options or version of Kotlin compiler that you are using?

Tested with 2.10.2 and Kotlin 1.3.61

@angryziber
Copy link
Author

Also, version 2.10.10, specified in README, doesn't actually exist.

@viartemev
Copy link
Member

@angryziber what is your use case for createInstance()?
The thing is KotlinModule has 2 constructors with default arguments, one of them is @Deprecated at DeprecationLevel.HIDDEN (this constructor is not accessible from code, but used for ABI compatibility).
Method createInstance() can't deal with 2 constructor with default arguments.
Additional constructor was added because version 2.10.1 contained backward-incompatible changes and 2.10.2 fixed the issue.
Original issue: #279

@angryziber
Copy link
Author

The use case is Dependency Injection, or more specifically, auto-creation of dependencies

@viartemev
Copy link
Member

@angryziber I do not think we can remove the second constructor in upcoming versions (it's necessary for ABI compatibility), maybe in 3.+ release.
If you have an option to create an instance of KotlinModule using another method (not createInstance()), I suggest to instantiate it using the primary constructor:

class Github291 {

    fun <T : Any> KClass<T>.createInstanceByPrimaryConstructor(): T {
        val allArgsInDefConstructorAreOptional = primaryConstructor?.parameters?.all(KParameter::isOptional) ?: false
        if (!allArgsInDefConstructorAreOptional) throw IllegalArgumentException("There is no primary constructor with default arguments: $this")
        return primaryConstructor?.callBy(emptyMap())
                ?: throw IllegalArgumentException("There is no primary constructor: $this")
    }
    
    @Test
    fun createInstanceOfKotlinModule() {
        val kotlinModule = KotlinModule::class.createInstanceByPrimaryConstructor()
        assertNotNull(kotlinModule)
    }

}

Will it suit your case?

@dinomite
Copy link
Member

dinomite commented Mar 3, 2021

Closing for inactivity

@dinomite dinomite closed this as completed Mar 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants