is it possible to provide custom name for internal delegated properties in kotlin?

Viewed 82

I have the following code snippet and I want to access scriptDsl property from Java internally from the same code base. I do not want to expose scriptDsl publically.

class ScriptDsl {
    internal val scriptDsl: JScriptDsl by lazy { //... }
}

when compiled, scriptDsl becomes public final JScriptDsl getScriptDsl$esw_ocs_dsl_kt() but I want to provide custom name here which is possible for non delegated properties using JvmName("scriptDsl"). How to do it for internal delegated properties?

I am fine if there are any other better solutions, my requirements are"

  • I want to call scriptDsl from java within the same module
  • I am calling it using reflection so need to know the name beforehand
  • ScriptDsl is public and I do not want property scriptDsl to be accessed outside module
  • scriptDsl has to be lazy because it depends on other properties which might not be avaibale while declaration

Note: I know internal in kotlin is public in java.

From kotlin docs:

internal declarations become public in Java. Members of internal classes go through name mangling, to make it harder to accidentally use them from Java and to allow overloading for members with the same signature that don't see each other according to Kotlin rules;

1 Answers

You can specify your annotation target to getter like this:

class ScriptDsl {
    @get:JvmName("scriptDsl")
    internal val scriptDsl: JScriptDsl by lazy { //... }
}

and then just call it with scriptDsl:

ScriptDsl scriptDsl = new ScriptDsl();
scriptDsl.scriptDsl();
Related