Skip to content

Kotlin: XML and Annotation constructor mapper does not work #3560

@VitaliiLebedynskyi

Description

@VitaliiLebedynskyi

About the Bug...

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of MyBatis.

  • I have confirmed this bug reproduces without 3rd party extensions (e.g. mybatis-plus).

Database Version

MySql 8

JDBC Driver Version

Maria driver 3.5.6

Issue Description

I am using kotlin and MyBatis for my project.
I found that i am not able to use constructor mapping. Issue mostly related to Kotlin Data classes support.

Inside the class

public class DefaultObjectFactory implements ObjectFactory, Serializable {
    private <T> T instantiateClass(Class<T> type, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) 
}

MyBatis calls type.getDeclaredConstructor(constructorArgTypes.toArray(new Class[0])) and it throws Exception in Kotlin.

I did a debug. I checked - The types and values are correct and their count is correct!

Applogize for pasted image but it is to complex to explain:
Image

So we see that we have args, arg types and their count is fine.

The issue is that - it missatches constructor signature and constructorArgTypes.toArray(new Class[0])
If i cal type.getDeclaredConstructors() it returns a valid constructor. You can see it in screenshot.

And here is my class: (Same issue for BOTH. Data class and just a class)

class DBQuestTemplate(
    val pk: Int?,
    val uuid: String,
    val name: String,
    val startTime: LocalDateTime?,
    val endTime: LocalDateTime?,
    val refreshType: QuestRefreshType,
    val target: QuestTarget,
    val reward: List<QuestReward>,
    val image: String?,
    val enabled: Boolean,
    val restricted: Boolean,
    val questOrder: Int,
    val questGroup: Int,
)

The mapper is

<constructor>
            <idArg column="q_pk" javaType="int"/>
            <arg column="q_uuid" javaType="string"/>
            <arg column="q_name" javaType="string"/>
            <arg column="q_start_time" javaType="java.time.LocalDateTime"/>
            <arg column="q_end_time" javaType="java.time.LocalDateTime"/>
            <arg column="q_refresh_type" javaType="com.simple.games.data.model.QuestRefreshType"/>
            <arg column="q_target"
                 javaType="com.simple.games.data.model.QuestTarget"
                 typeHandler="com.simple.games.data.handler.QuestTargetHandler"/>
            <arg column="q_reward"
                 javaType="java.util.List"
                 typeHandler="com.simple.games.data.handler.QuestRewardListHandler"/>
            <arg column="q_image" javaType="string"/>
            <arg column="q_enabled" javaType="boolean"/>
            <arg column="q_restricted" javaType="boolean"/>
            <arg column="q_quest_order" javaType="int"/>
            <arg column="q_quest_group" javaType="int"/>
        </constructor>

And the error is:

java.lang.NoSuchMethodException: com.simple.games.data.model.DBQuestTemplate.<init>(java.lang.Integer,java.lang.String,java.lang.String,java.time.LocalDateTime,java.time.LocalDateTime,com.simple.games.data.model.QuestRefreshType,com.simple.games.data.model.QuestTarget,java.util.List,java.lang.String,java.lang.Boolean,java.lang.Boolean,java.lang.Integer,java.lang.Integer)

About your report...

  • I did not use images 🖼️ for showing text information (code, error, etc.).

  • I checked the Preview and my report looks awesome! 👍

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions