Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin Renaming Generated Getters and Setters

Is there a way to rename the default getters and setters in Kotlin? I have a property named in snake_case, but I still want the getters and setters to be named in camelCase.

The closest I've gotten is something like

private var property_name = Color.BLACK
private set
fun setPropertyName(c: Color) { property_name = c }
fun getPropertyName() = property_name

Is there a way to do this without hiding the getters and setters and defining new methods?

like image 421
Jeffmagma Avatar asked Jun 07 '17 20:06

Jeffmagma


2 Answers

If you just want to change the name and not the functionality, you can also scope the annotation like so:

@get:JvmName("getPropertyName")
@set:JvmName("setPropertyName")
var property_name = Color.BLACK
like image 52
Ruckus T-Boom Avatar answered Oct 14 '22 15:10

Ruckus T-Boom


It is described in the section on handling signature clashes: https://kotlinlang.org/docs/reference/java-to-kotlin-interop.html#handling-signature-clashes-with-jvmname

val x: Int
    @JvmName("getX_prop")
    get() = 15

So @JvmName("getPropertyName") get should work.

like image 42
Kiskae Avatar answered Oct 14 '22 14:10

Kiskae