Skip to content
Advertisement

In Kotlin, how does GSON.fromJson(…) manage to mutate a read-only field of an existing object?

In Kotlin, how can an instance’s read-only val field be mutated?

In the following Kotlin code, gson.fromJson(...) gets a Thing(0) from the InstanceCreator and somehow manages to mutate value from 0 to 1, then return that object. How?

JavaScript

I verified that the object returned by gson.fromJson(...) is the same object provided by the InstanceCreator, so it’s not creating a new instance based on the one provided by the InstanceCreator.

I also tried setting value using reflection, but didn’t find a way. There was no setter available on the val field.

Advertisement

Answer

I also tried setting value using reflection

Well, you can do it if you use Java’s reflection API, rather than Kotlin’s.

Kotlin val properties translates to a private Java field with only a public getter. You can set the private field using reflection. For example:

JavaScript

This is probably also what Gson does under the hood.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement