Skip to content

You are trying to set "Companion" which is a final field! when using Kotlin classes in NS #1178

@tailsu

Description

@tailsu

Environment
Provide version numbers for the following components (information can be retrieved by running tns info in your project folder or by inspecting the package.json of the project:

  • CLI: 4.2.4
  • Cross-platform modules:
    "nativescript-theme-core": "~1.0.4",
    "tns-core-modules": "~4.2.0"
  • Android Runtime: 4.2.0
  • iOS Runtime (if applicable):
  • Plugin(s):

Describe the bug
Calling new SomeClass() fails with Error: You are trying to set "Companion" which is a final field! Final fields can only be read. if SomeClass is a Kotlin class with a companion object.

To Reproduce
kotlinplugin.zip

Add (tns plugin add) the attached plugin into a new {N} project. Import and execute:

var test = require("nativescript-kotlintest");
test.doTest();

index.js of NS plugin:

module.exports = {
    doTest: function() {
        new com.example.stedra.myapplication.ScanbotSDKInitializer(); // <-- this crashes
    }
};

ScanbotSDKInitializer.kt:

package com.example.stedra.myapplication

class ScanbotSDKInitializer {
    companion object {
        private var initialized = false

        @JvmStatic
        fun isInitialized() = initialized
    }

    fun initialize() {
        initialized = true
    }
}

Expected behavior
To not crash.

Additional context
Kotlin companion objects are implemented using a nested class SomeClass$Companion and a field named Companion of that same type in SomeClass. This doesn't seem to be possible with Java itself, but the JVM seems OK with it. The clash of type and field names or the non-standard implementation of the nested class might be the reason for this buggy behavior.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions