samedi 30 mars 2019

How to fix wrong constructor parameters in kotlin

I have an interesting error with reflections in kotlin.

So, im using 'argTypes' method for getting all parameter type of args.

private fun argTypes(vararg args: Any): Array<Class<*>> {
        val argTypes = ArrayList<Class<*>>()
        args.forEach { argTypes.add(it::class.java) }
        return argTypes.toTypedArray()
    }

Im using it with like that:

fun <T> newInstance(clazz: Class<*>, argTypes: Array<Class<*>>, vararg args: Any): T {
        return clazz.getDeclaredConstructor(*argTypes).newInstance(*args) as T
    }

In the end:

ReflectionUtil.instance.newInstance<IBossBar>(
                PacketBossBar1_13_R2::class.java,
                TextComponent("asd"),Color.BLUE,Style.PROGRESS,100F)

I use a float parameters thats '100F'.

When i use that method, the type is going to be java.lang.Float but my 'PacketBossBar1_13_R2' constructor has a float parameters like that:

constructor(
            message: TextComponent,
            color: Color,
            style: Style,
            progress: Float
    ): this(ComponentSerializer.toString(message), color, style, progress)

When i getting the constructor as a manual, its return

public io.github.utsukushihito.utsutil.nms.v1_13_R2.PacketBossBar1_13_R2(net.md_5.bungee.api.chat.TextComponent,io.github.utsukushihito.utsutil.api.bossbar.enums.Color,io.github.utsukushihito.utsutil.api.bossbar.enums.Style,float)

When i use automatic way its returning NoSucMethodException like that:

java.lang.NoSuchMethodException: io.github.utsukushihito.utsutil.nms.v1_13_R2.PacketBossBar1_13_R2.<init>(net.md_5.bungee.api.chat.TextComponent, io.github.utsukushihito.utsutil.api.bossbar.enums.Color, io.github.utsukushihito.utsutil.api.bossbar.enums.Style, java.lang.Float)





Aucun commentaire:

Enregistrer un commentaire